Finding velocity vector based on angle and speed

Tohmas picture Tohmas · Dec 30, 2012 · Viewed 13.6k times · Source

I am programming some movement for AI for a game based on angle and speed.Its a 2D program based on x,y axis. I currently have a position vector as-well as a velocity vector which accounts for speed and current direction of the AI.Every time I move the AI I just add the velocity vector to the current position to get my new position.At the moment the movement is 8-directional.What I wanted to implement was angle based movement (more realistic type of movement) and wondered if it is possible to calculate the velocity vector from an angle and speed constant.

Thanks in advance!

Answer

Mikhail picture Mikhail · Dec 30, 2012

This better be possible.

This is how I envision your program works

position_X += velocity_X
position_Y += velocity_Y

Through trigonometry you can do

velocity_X = velocity*cos(angle)
velocity_Y = velocity*sin(angle)

enter image description here

A few things to keep in mind include

  1. Most likely Y increases as you go from top to bottom
  2. The inverse problem has two solutions, you should look into a 2 input arctangent function. See https://gamedev.stackexchange.com/questions/14602/what-are-atan-and-atan2-used-for-in-games