问题
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