Projectiles
I have my base class for all projectiles (rockets, bullets) that contains an X,Y,Angle and Speed integers.
Now, what formula can I use so that if the angle is say, 45 degrees and the speed is ten, it would move the X 5 pixels and the Y -5 pixels. I had one once, but I lost it. Damn.
Any help?
Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
Convert your angle from degrees to radians, then do this:
x += sinf(angle) * speed;
y += cosf(angle) * speed;
or it might be:
x += cosf(angle) * speed;
y += sinf(angle) * speed;
[edited by - YodaTheCoda on February 22, 2003 12:12:13 AM]
x += sinf(angle) * speed;
y += cosf(angle) * speed;
or it might be:
x += cosf(angle) * speed;
y += sinf(angle) * speed;
[edited by - YodaTheCoda on February 22, 2003 12:12:13 AM]
Oh, Ok, I''ll go try that now.
Yo Tyler, I hear your mum''s goin out with... SQUEEK!
Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
and in case you havent got an accurate(ish) definition of PI...
#define PI 3.1415926535
#define PI 3.1415926535
Oh, yeah. I''ll probably need that.
Yo Tyler, I hear your mum''s goin out with... SQUEEK!
Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
Ok, that worked, but I just need to correct you (cause I don''t often get to)
I found that I needed to add a +90 to the angle. Don''t tell me I''m doing something wrong, cause this works so... yeah.
Thanx!!
Yo Tyler, I hear your mum''s goin out with... SQUEEK!
I found that I needed to add a +90 to the angle. Don''t tell me I''m doing something wrong, cause this works so... yeah.
Thanx!!
Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
you are doing it wrong. You must be doing
x=sin(angle)*speed
y=cos(angle)*speed
when it should be
x=cos(angle)*speed
y=sin(angle)*speed
and because sin(a+90)=cos(a), if you got the two reversed you would have to make the correction you made to get it to work.
x=sin(angle)*speed
y=cos(angle)*speed
when it should be
x=cos(angle)*speed
y=sin(angle)*speed
and because sin(a+90)=cos(a), if you got the two reversed you would have to make the correction you made to get it to work.
[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement