Angle between two points and calculating the rotation

好久不见. 提交于 2019-12-11 03:05:05

问题


so I'm currently programming a little game in Java with libGDX.

I want the players texture to directly rotate to the mouse position. Unfortunately my code doesn't work, and I have absolutely no idea why not.

So, I call this method in the Update method of the player:

private void CalculateRotation()
{
    rotation = Position.angle(input.getTranslatedMousePos());
}

The Position is just a Vector2 with the players position. I get the translatedMousePos like this:

public Vector2 getTranslatedMousePos()
{
    Vector3 v = camera.unproject(new Vector3(MousePos.x, MousePos.y, 0));

    return new Vector2(v.x, v.y);
}

while MousePos is determined by the following code:

@Override
public boolean mouseMoved(int screenX, int screenY) {
    MousePos = new Vector2(screenX, screenY);
    return false;
}

I draw the players sprite like this:

public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.draw(new TextureRegion(txt), Position.x, Position.y, (float)(size.x * 0.5), (float)(size.y * 0.5), size.x, size.y, 1.0f, 1.0f, rotation);
}

Unfortunately the sprite turns, but in no way pointing to the cursor.

Following things I figured out yet:

  • spriteBatch.draw(...,rotation) accepts degrees and Vector2.angle gives the angle in degrees

  • the translatedMousePos seems to be right, I figured that out by hovering over the players sprite (which is at (1000|-1000)) and "printing" the translatedMousePos, it was the same

Thank you for taking your time and helping me! :)

来源:https://stackoverflow.com/questions/29412383/angle-between-two-points-and-calculating-the-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!