Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding velocity vector based on angle and speed [closed]

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!

like image 620
Tohmas Avatar asked Dec 29 '12 23:12

Tohmas


1 Answers

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
like image 114
Mikhail Avatar answered Sep 27 '22 18:09

Mikhail