Friday, February 9, 2007

Moving oriented objects in 2D

This is kids stuff for even the amateur games developer,
but for all those aspirants, heres how,

Let x and y be the horizontal and vertical coordinates of your object,
let its orientation be θ.

Assuming you want to displace the oriented object by r units in the direction it is
facing, then,

x = x + r * cos(θ);
y = y + r * sin(θ);

if your using a system in which y axis positive direction faces downwards, then the solution is, (which is the case with many 2D graphics systems). A good example for such a kind of API which uses this kind of 2D graphics system is Adobe Flash, formerly known as Macromedia Flash.

x = x + r * sin(θ);
y = y + r * cos(θ);

Hope this helps, till then, Happy coding!