rotating a 2D vector

I’m pretty new to vector math, I’ve just learned the basics (adding, subtracting, dot product, normalizing), and I’ve decided that I want entities in my game engine to store a direction vector and a single movement speed. Before this I had always stored seperate x/y velocities, x/y acceleration, etc. Every example I had seen up until recently seemed to use this pattern as well.

What I’m wondering is how I can rotate this direction vector. I have a variable that stores a rotation value in radians, and I rotate my sprite to this value, but I need to alter the entity’s direction vector to match it so that I can give its direction vector to projectiles that is spawns. If you want the specifics, its a gun that I’m rotating and I want to give the gun’s direction vector to its bullets when it fires. Right now I just have a value in radians, but I’m not sure how to alter the direction vector to match this.

thanks for any help or even a good link :slight_smile:

I think I may have solved it, but please let me know if this is right!

seems to me like the x value of my direction vector would be the sine of the rotation value I’m storing (in degrees though?), and the y value would be the cosine of that same angle. I was also thinking that this would give me an already normalized vector, but I’m not sure if thats true… or if what I suggested is even correct.

Yup. That’s correct. The resulting vector is normalized!

Edit:
You’ll have to convert degrees to radians to call Math.sin and Math.cos. To do this, simply multiply the angle in degrees by (float)Math.PI/180.0f.

I’ve come across another problem already, acceleration. I’m before using vectors I would do something like:

xVol += xAcceldeltaSeconds;
yVol += yAccel
deltaSeconds;

x += xVoldeltaSeconds;
y += yVol
deltaSeconds;

but since I am now using:

x += direction.xspeeddeltaSeconds;
y += direction.yspeeddeltaSeconds;

I’m not sure how to handle acceleration. It seems like I will need another vector to represent the direction of the acceleration, and perhaps yet another variable to represent the magnitude of the acceleration. But I’m not sure if I should just use the un-normalized acceleration-direction vector instead. Then would acceleration look like this?:

direction.x += accelDirection.xdeltaSeconds;
direction.y += accelDireciton.y
deltaSeconds;

speed += accelDirection.length()*deltaSeconds;

will this work and is it the best way?

The problem is you’re using speed and not velocity. Your speed should also be an tuple (i.e. x and y). Then you just add your direction multiplied by some thrust modifier to it when the user thrust in the current direction.

Kev