如何旋转box2d机身以面向它的行进方向?

时间:2015-08-06 03:49:51

标签: java android libgdx box2d

我使用libgdx / box2d并且我有一个船舶对象。我想设置船舶body的旋转,以便始终指向它行进的方向。

我对此采取的当前方法(如果您知道更好的方法,请告诉我)是比较前一帧与当前帧之间的船舶(x,y)位置,然后找到两者之间的角度。

但是,我当前的位置似乎没有正确设置。以下代码在render()方法中发生。如您所见,我设置previousPosition = currentPosition,然后更新currentPosition

System.out.println("Current ONE: " + currentShipPosition.x + ", " + currentShipPosition.y);
System.out.println("Previous ONE: " + previousShipPosition.x + ", " + previousShipPosition.y);

float x = currentShipPosition.x;
float y = currentShipPosition.y;
previousShipPosition = new Vector2(x, y);

System.out.println("Current TWO: " + currentShipPosition.x + ", " + currentShipPosition.y);
System.out.println("Previous TWO: " + previousShipPosition.x + ", " + previousShipPosition.y);

currentShipPosition = ship.bodyWrapper.body.getWorldCenter();

System.out.println("Current THREE: " + currentShipPosition.x + ", " + currentShipPosition.y);
System.out.println("Previous THREE: " + previousShipPosition.x + ", " + previousShipPosition.y);

ship.setSpriteRotation(previousShipPosition, currentShipPosition);

此时我希望Current ONEPrevious ONE不同,Current TWOPrevious TWO相同,然后Current THREE和{{ 1}}再次变得不同。以下是输出示例:

Previous THREE

为什么第三个上的当前值和上一个值检查相同,并且在再次调用Current ONE: -9.371556, 10.199657 Previous ONE: 7.4040437, 21.537455 Current TWO: -9.371556, 10.199657 Previous TWO: -9.371556, 10.199657 Current THREE: -9.371556, 10.199657 Previous THREE: -9.371556, 10.199657 Current ONE: 1.3700135, -0.12703675 Previous ONE: -9.371556, 10.199657 Current TWO: 1.3700135, -0.12703675 Previous TWO: 1.3700135, -0.12703675 Current THREE: 1.3700135, -0.12703675 Previous THREE: 1.3700135, -0.12703675 之前不会更新?我希望能够按时更新。

如果您需要更多代码或有任何建议(与问题或其他相关),请与我们联系。感谢。

编辑:好的,我现在正在使用它,更简单: render() float angle = (float) Math.atan2((double) body.getLinearVelocity().y, (double) body.getLinearVelocity().x);

0 个答案:

没有答案