Android - Math.atan2 的问题

发布于 2024-11-07 22:30:34 字数 1256 浏览 0 评论 0原文

因此,在我使用 andengine 制作的 Android 游戏中,我将其设置为当我触摸并拖动玩家精灵时,它会不断旋转,以便角色始终面向其行进的方向。

public class Player extends AnimatedSprite {
private float lastX = Game.centerX;
private float lastY = Game.centerY;
private static int angle = 0;

// ...

@Override
public boolean onAreaTouched(final TouchEvent sceneTouchEvent, final float touchAreaLocalX, final float touchAreaLocalY) {
    Body body = OrbCatch.physicsWorld.getPhysicsConnectorManager().findBodyByShape(this);
    if (sceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE) {
        float currentX = sceneTouchEvent.getX();
        float currentY = sceneTouchEvent.getY();
        angle = (int) (Math.atan2(currentY - lastY, currentX - lastX) * 180 / Math.PI);
        lastX = currentX;
        lastY = currentY;
    }
    body.setTransform(new Vector2(sceneTouchEvent.getX(),sceneTouchEvent.getY() )
    .mul(1/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT),angle);
    return true;
}

}

关键是:

angle = (int) (Math.atan2(currentY - lastY, currentX - lastX) * 180 / Math.PI)

它获取最后一个已知坐标和当前坐标,计算它们之间的角度,并将其从弧度转换为度数。好吧,昨天一切都工作正常,但尽管今天没有更改任何代码,但它的行为却很奇怪。精灵的方向变化不规则,没有明显的模式。如果我沿直线移动它,它会连续在 2 或 3 个明显不同的角度之间交替(通常其中一个是正确的角度)。

编辑:已解决,见下文

So in my android game I'm making with andengine, I have it set up so as I touch and drag the player sprite, it continously rotates so that the character is always facing the direction it is traveling.

public class Player extends AnimatedSprite {
private float lastX = Game.centerX;
private float lastY = Game.centerY;
private static int angle = 0;

// ...

@Override
public boolean onAreaTouched(final TouchEvent sceneTouchEvent, final float touchAreaLocalX, final float touchAreaLocalY) {
    Body body = OrbCatch.physicsWorld.getPhysicsConnectorManager().findBodyByShape(this);
    if (sceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE) {
        float currentX = sceneTouchEvent.getX();
        float currentY = sceneTouchEvent.getY();
        angle = (int) (Math.atan2(currentY - lastY, currentX - lastX) * 180 / Math.PI);
        lastX = currentX;
        lastY = currentY;
    }
    body.setTransform(new Vector2(sceneTouchEvent.getX(),sceneTouchEvent.getY() )
    .mul(1/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT),angle);
    return true;
}

}

The key line is this:

angle = (int) (Math.atan2(currentY - lastY, currentX - lastX) * 180 / Math.PI)

It takes the last known coordinates and the current coordinates, calculates the angle between them, and converts it from radians to degrees. Well, this was all working fine yesterday, but despite changing none of this code today it's behaving strangely. The sprite's orientation changes erratically, with no apparent pattern. If I move it in a straight path, it continuously alternates between 2 or 3 distinctly different angles(usually one of them is the correct one).

edit: solved, see below

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔岸观火 2024-11-14 22:30:34

问题是 Body.setTransform 角度参数采用弧度值,而不是度数。 Andengine 的记录如此之少......

The problem was that Body.setTransform angle parameter takes values in radians, not degrees. Andengine is so poorly documented...

深白境迁sunset 2024-11-14 22:30:34
math.todegree(math.atan(....))....

您应该使用toDegree()

math.todegree(math.atan(....))....

You should use toDegree().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文