hello , Im creating a top down game. Id like to know what is the formula for moving X and Y values N pixels in a specified direction (0-360) ?

int x=0;
int y=0;
int speed=3; // "n" 
int direction=45; 

//some formula
...

//output

x=3;
y=3;

Edited 6 Years Ago by wwsoft: n/a

There is no single formula. In general you will find the amount of change in X and the amount of change in Y and add it to the current values of X and Y. Sounds like you could use trigonometry and then you'd have to decide whether to use radians or degrees to describe the angles.

Decide what 0 degrees, 90 degrees, 180 degrees, and 270 degrees are. Normally they are east, north, west, and south respectively (or right, up, left, down). See chart here:

http://en.wikipedia.org/wiki/Polar_coordinate_system

Moving west makes x decrease. Moving east makes x increase. Moving north makes y decrease. Moving south makes y increase. In normal life, we think of that going up INCREASES y, but in Computer Science, (0,0) is the upper-left coordinate, not the bottom-left coordinate, so it's different from the Cartesian Coordinate system from algebra. Lay it out ahead of time so you don't get confused.

Using that starting point, basic trigonometry is going to tell you that if you start at (10,10) and move 5 units at a 15 degree bearing, you'll end up here:

(10 + 5cos15, 10 - 5sin15)

Check to make sure that you know exactly when sin and cosine give positive and negative answers and make sure it is the way you want it to be. If not, add instead of subtract or vice-versa. Also be cognizant that most likely you'll need to convert from degrees to radians.

Edited 6 Years Ago by VernonDozier: n/a

Could anyone just tell me a formula that uses degrees for changing x and y in c++ ?

also , I meant to place a - sign on y's example output

Edited 6 Years Ago by wwsoft: n/a

This article has been dead for over six months. Start a new discussion instead.