Archived

This topic is now archived and is closed to further replies.

CoiN

Getting a vector from an angle + speed

Recommended Posts

CoiN    122
Lo All, Couldn''t think of a better post title. This is for my car game, and I want the car to travel in the direction it is facing (always a good feature for a car game), I can work out the angle it is facing but I suck at trigonometry and can''t get the direction vector. If I have an angle of 24 degrees, and a speed of 2 pixels per frame, could anyone tell me how I would calculate the vector? Hope this makes sense. Thanks... CoiN

Share this post


Link to post
Share on other sites
Leffe    301
I think this might be right ^_^;; But then it might not be right...:


(2 * cos(24o), 2 * sin(24o))


Make sure that 24o gets converted to radians if neccasary :/

I''m really not sure about the 2 there...

Share this post


Link to post
Share on other sites
ga    126
if it''s only 2 pixels you should store the position as floating point else the result will always be (1,0) or (2,1) (depends on rounding mode) for the velocity vector.

Share this post


Link to post
Share on other sites
Xsist10    122
exactly what Leffe said but i''ve just added the radian to degree conversion.


#define PI 3.141592654

x = 2 * cos(24*PI/180);
y = 2 * sin(24*PI/180);

Share this post


Link to post
Share on other sites
CoiN    122
Lo Again,

I''ve got the car travelling in the right direction now, but theres one prob...

It seems to be have a slight wobble, like a judder, it''s really noticable when it''s travelling in a straight line.

You can get my test for here:

www.chizief.co.uk/test.rar

If anyone wouldn''t mind taking a look to see what I mean, its only 195k.

Ignore all the other crap going on, on the screen. The car is control by the left and right arrow keys and it just has a constant velocity.

The code I''m using to direct the car in the right angle is:


  

// Get car direction

angle = frame * ((PI * 2) / sprite2.num_of_frames);

// Get the direction vector and multiply by the cars speed

sprite2.vx = cosf(angle) * 5;
sprite2.vy = sinf(angle) * 5;

// Update Cars position

sprite2.sprite_x += sprite2.vx;
sprite2.sprite_y += sprite2.vy;



The frame is the frame number from the cars sprite map.

Thanks...

CoiN


Share this post


Link to post
Share on other sites