Domenik.B Domenik.B - 4 months ago 20
Java Question

Libgdx: transform vector2 around z-axis

I´m trying to port a tower defence game from xna (c#) to libgdx (java).

But now I have a problem, there is a function which rotates the bullets from the towers around the z-axis in direction to the enemies.

public void SetRotation(float value){
rotation = value;
velocity = Vector2.Transform(new Vector2(0, -speed), Matrix.CreateRotationZ(rotation));
}


This is the code in c# using xna. Does anybody know how to do this in libgdx?

Here ist the link form the tutorial side from the tutorial: http://xnatd.blogspot.de/2010/10/tutorial-7-firepower.html.

I hope there is someone who can help me..

Answer

Vector2#rotate rotate the vector around the Z axis. Note that it needs the angle in degrees. If the angle you have is in radians then use the rotateRad method.

velocity.set(0, -speed).rotate(value);
//or
velocity.set(0, -speed).rotateRad(value);