Jump to content

  • Log In with Google      Sign In   
  • Create Account


Rotating vector, need help!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 crovea   Members   -  Reputation: 124

Like
0Likes
Like

Posted 09 June 2012 - 11:23 AM

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.

Sponsor:

#2 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 09 June 2012 - 11:54 AM

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.

#3 crovea   Members   -  Reputation: 124

Like
0Likes
Like

Posted 09 June 2012 - 12:11 PM

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

#4 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 09 June 2012 - 01:26 PM

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.

#5 Álvaro   Crossbones+   -  Reputation: 11861

Like
0Likes
Like

Posted 09 June 2012 - 07:01 PM

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.

#6 Álvaro   Crossbones+   -  Reputation: 11861

Like
0Likes
Like

Posted 09 June 2012 - 07:10 PM

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, 09 June 2012 - 07:10 PM.


#7 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 10 June 2012 - 05:48 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS