Rotate Texture in function of player position

前端 未结 2 1834
北恋
北恋 2021-01-28 10:14

I\'m making a 2d game that consists in a plane fighting against spaceships trough missiles, i want the spaceships to rotate in function of the player position, so that they are

2条回答
  •  走了就别回头了
    2021-01-28 10:43

    The @dwn's answer is mathematically correct, but when you deal with LibGDX framework it's better to use native tools.

    If you have positions of enemy and player storing in Vector2s then you may perform computations a bit convenient. For example, to determine angle between specific enemy and player:

    Vector2 enemyPos;
    Vector2 playerPos;
    Vector2 temp;
    
    float angle = temp
                    .set(playerPos)
                    .sub(enemyPos)
                    .angle();
    

    This will give you exactly what you want. Of course it's better to keep temp vector in somewhere in public space to use it in such numerous calculations. You better to check other convenient Vector2methods, it will seriously easy your life.

    Good luck.

提交回复
热议问题