Jump to content
• Advertisement

#### Archived

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

# Getting a vector from an angle + speed

This topic is 5545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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

##### Share on other sites
Advertisement
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

##### Share on other sites
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

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

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

#### Share this post

##### Share on other sites
Thanks everyone for the replies.

It''s sorted now.

#### Share this post

##### Share on other sites
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

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Now

• 14
• 13
• 23
• 10
• 19
• Advertisement
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!