# Rotating vector, need help!

This topic is 2228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey!

I making a 2d top-down shooter game and i'm currently in the process of creating weapons.

When a soldier fires a projectile, i find the unit vector between the soldiers center and mouse position ( the target )
and use that as a basis for the velocity vector for the projectile which works fine!

But i've run into problems creating a shotgun, which is supposed to spray bullets.
What i want to do here is spread 5 bullets out evenly in a 25 degree angle centered around the target vector.

My vector math is a few too many years behind me but from what i've been able to find online, the formula i need and have been using is:

 xVel = xVel * (float)cos(addingAngle) - yVel * (float)sin(addingAngle); yVel = yVel * (float)sin(addingAngle) + yVel * (float)cos(addingAngle); 
Where xVel and yVel is the coordinates of the unit vector i found between shooter and target. addingAngle is the angle im trying to add ( or substract by being negative).

This is however, giving me some wierd results with bullets flying in irregulair angles.
I'm probably mixing up radians and degrees, but that doesnt explain why my bullets appear to be choosing such apparently random angles (however conistent)

So can anyone point me in the right direction?
I'm going to have to read up on vectors eventually, but i'd rather wait until i have more time on my hands.

##### Share on other sites
You've copied something a little carelessly, I think. You want:

 xVel_new = xVel * (float)cos(addingAngle) - yVel * (float)sin(addingAngle); yVel_new = xVel * (float)sin(addingAngle) + yVel * (float)cos(addingAngle); xVel = xVel_new; yVel = yVel_new; 

In addition to having to use separate variables (as the original values of both xVel and yVel are needed in each line of the calculation), there was an error in the first term on the second line.

##### Share on other sites
Thanks alot, this works perfectly it seems!

I copied it right from the formula i found:
http://stackoverflow.com/questions/6006400/modifying-the-angle-of-a-unit-vector

Which is kinda strange i think? I guess that could have something to do with the y-axis being inverted or something

I have no idea how i missed that the second line of code was using the first variable, which obviously causes errors xD

##### Share on other sites
Sorry, what's strange? What I put is a direct translation of those forumulas. Note that they assign to x' and y' (corresponding to xVel_new and yVel_new), not to x and y.

##### Share on other sites
The code in the link did have the mistake of using y instead of x in the first term of the second formula. The mistake of using the same variables (which many of us have made before) was introduced by the OP.

##### Share on other sites
One more thing: If instead of specifying a rotation as an angle you used a unit-length vector (cos(angle), sin(angle)), your code would be simpler in general. In the particular case of rotating a vector, the code would look like the multiplication of complex numbers. But the main advantages of using unit-length vectors instead of angles are:

• Fewer special cases (like 360 becoming 0)
• More natural conversion to working in 3D (where rotations are represented with unit-length quaternions, instead of unit-length complex numbers).

This might be a bit too mathematically sophisticated for you at this stage, but it's good to keep in mind that it's an option, in case you feel like exploring it at some point. Edited by alvaro

##### Share on other sites

The code in the link did have the mistake of using y instead of x in the first term of the second formula.

Ah I see it now, I was only looking at the initial snippet of mathematics. My apologies.

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631722
• Total Posts
3001890
×