Sign in to follow this  
Crovea

Rotating vector, need help!

Recommended Posts

Crovea    124
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:

[CODE]
xVel = xVel * (float)cos(addingAngle) - yVel * (float)sin(addingAngle);
yVel = yVel * (float)sin(addingAngle) + yVel * (float)cos(addingAngle);
[/CODE]
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 this post


Link to post
Share on other sites
the_edd    2109
You've copied something a little carelessly, I think. You want:

[code]

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;
[/code]

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 this post


Link to post
Share on other sites
Crovea    124
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 this post


Link to post
Share on other sites
the_edd    2109
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 this post


Link to post
Share on other sites
alvaro    21266
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 this post


Link to post
Share on other sites
alvaro    21266
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:[list]
[*]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).
[/list]
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 this post


Link to post
Share on other sites
the_edd    2109
[quote name='alvaro' timestamp='1339290080' post='4947793']
The code in the link did have the mistake of using y instead of x in the first term of the second formula.
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this