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
I think this should work. Set..
v = centerPosOfShip - centerPosOfUFO
Normalize...
v /= sqrt( v.x * v.x + v.y * v.y )
So now we can say v = [ cos(theta) sin(theta) ]; the rotation matrix is...
[ cos(theta) -sin(theta) ]
[ sin(theta) cos(theta) ]
So we can write in terms of v-components...
[ v.x -v.y centerPosOfUFO.x ][x]
[ v.y v.x centerPosOfUFO.y ][y]
[ 0 0 1 ][1]
where [x y] is any point on the UFO relative to its center.
Hope that makes sense
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 Vector2
s 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 Vector2
methods, it will seriously easy your life.
Good luck.