Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Trouble with 2D speed vectoring.

4: Adsense

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
3 replies to this topic

#1 xzbobzx   Members   

120
Like
0Likes
Like

Posted 08 November 2012 - 11:20 AM

So I'm working on a 2D, top-down racer-ish game and I devised an interesting algorithm using cosines and angles to calculate the speed vector. Too bad for me, it doesn't work like it's supposed to.

if( Rotation > 0 && Rotation < 90 )
{
  float Section_A = Speed*cos(90-Rotation);
  Velocity_Y = Speed*cos(Rotation);
  Velocity_X = Section_A - Section_A - Section_A;
}
else if( Rotation > 90 && Rotation < 180)
{
  float Section_B = Speed*cos(180-Rotation);
  float Section_C = Speed*cos(Rotation-90);
  Velocity_Y = Section_B - Section_B - Section_B;
  Velocity_X = Section_C - Section_C - Section_C;
}
else if( Rotation > 180 && Rotation < 270)
{
  float Section_D = Speed*cos(Rotation-180);
  Velocity_Y = Section_D - Section_D - Section_D;
  Velocity_X = Speed*cos(270-Rotation);
}
else if( Rotation > 270 && Rotation < 360)
{
  Velocity_Y = Speed*cos(360-Rotation);
  Velocity_X = Speed*cos(Rotation-270);
}
else if( Rotation < 1 )
{ Velocity_Y = Speed; Velocity_X = 0; }
else if( Rotation > 89 && Rotation < 91 )
{ Velocity_Y = 0; Velocity_X = Speed - Speed - Speed; }
else if( Rotation > 179 && Rotation < 181 )
{ Velocity_Y = Speed - Speed - Speed; Velocity_X = 0; }
else if( Rotation > 269 && Rotation < 271 )
{ Velocity_Y = 0; Velocity_X = Speed; }
Position_Y = Position_Y + Velocity_Y;
Position_X = Position_X + Velocity_X;

(0 rotation means the car is facing down, 90 it's facing left, 180 up, 270 right.
At >359 rotation goes insta 0.)

All that this does, is spaz out.
In my theory it should work, but apparently it does not.
Can anyone help?

#2 Álvaro   Members   

20922
Like
3Likes
Like

Posted 08 November 2012 - 12:03 PM

I think this is all you need:
double const pi = atan(1.0)*4.0;
double const degrees = pi/180.0;

Velocity_X = -Speed * sin(Rotation * degrees);
Velocity_Y = Speed * cos(Rotation * degrees);

You need to make sure your angles are in radians before you pass them to cos() or sin().

#3 Cornstalks   Members   

7030
Like
1Likes
Like

Posted 08 November 2012 - 12:07 PM

Wild guess: most trig functions (sin, cos, etc.) work with radians, not degrees. Try converting your degrees to radians (radians = degrees * 3.14159265359f / 180.0f) before doing sin/cos.

[edit]

Aaaaaand ninja'd

Edited by Cornstalks, 08 November 2012 - 12:07 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#4 xzbobzx   Members   

120
Like
0Likes
Like

Posted 08 November 2012 - 01:14 PM

I think this is all you need:

double const pi = atan(1.0)*4.0;
double const degrees = pi/180.0;

Velocity_X = -Speed * sin(Rotation * degrees);
Velocity_Y = Speed * cos(Rotation * degrees);

You need to make sure your angles are in radians before you pass them to cos() or sin().


That works great!
Thanks man, massive kudos.




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.