View Full Version : Moving Objects in direction of an angle

17-06-2004, 01:33 PM

I want to Move an Object (in 2D) in the direction of a specified angle.
In the past I used something like this:

x :=x + Round(Cos256(angle)*Speed);
y :=y + Round(Sin256(angle)*Speed);

(Cos/Sin256 are from DelphiX)
But this allows only an angle from 0 to 255.

Ho can I realize this for 0 to 360AŹ???

17-06-2004, 01:45 PM

17-06-2004, 01:52 PM
*g* I have already tried this before, but it doesn't work..
It leads to weird, random-like results..

17-06-2004, 02:10 PM
I'm just guessing, but you're probably doing the rounding in a wrong place, keep you're x and y as real numbers (singles preferably) and only use them rounded when drawing, or you're forgetting that the angle is in radians.

17-06-2004, 02:23 PM
Hm this doesn't fix the problem :(

With each increase oder decrease of the angle, the object moves in a totally different direction..

I know that there are more Cos/Sin function than just Cos()..
..maybe Cos256 is based on something different..

17-06-2004, 02:43 PM
Post yore angle changing code

17-06-2004, 02:47 PM
ok here we go:

if &#40;links&#41; AND &#40;angle < 360&#41; then angle &#58;= angle +1;
if &#40;rechts&#41; AND &#40;angle > 0&#41; then angle &#58;= angle -1;

if &#40;links&#41; AND &#40;angle = 360&#41; then angle &#58;= 0;
if &#40;rechts&#41; AND &#40;angle = 0&#41; then angle &#58;= 360;

links & rechts are checked in OnKeyDown links is true if VK_LEFT is pressed and rechts if VK_RIGHT..
(Undone in OnKeyUp)

17-06-2004, 02:56 PM
I know that there are more Cos/Sin function than just Cos()..
..maybe Cos256 is based on something different..
Nope, checked DelphiX?˘_Ts source, Cos256 simply uses a lookup table precalculated with
for i:=0 to 255 do
CosinTable[i] := Cos((i/256)*2*PI);

As I said the angle is in radians, so changing it by 1 is rotating by a almost 60 degrees, change the angle in much smaller values like one degree (that is pi/180).

17-06-2004, 03:26 PM
Ah ok thanks, now it is close to a "realistic" movement.

I'm doing It like this:

(Angle is increased as done before)

xko &#58;=xko - Cos&#40;360-angle*&#40;pi/180&#41;&#41;*Speed;
yko &#58;=yko + Sin&#40;360-angle*&#40;pi/180&#41;&#41;*Speed;

but the movement in a 90AŹ? angle is not totally correct, maybe 10AŹ? more than it should be..

I have uploaded the thing i am working on here:
(it is a modified delphiGL example)
Please have a short look..

17-06-2004, 03:45 PM
yore not converting 360 degrees to radians in 360-angle*(pi/180), it should be (360-angle)*(pi/180). To say the truth 360 is not needed at all. And keeping the angle variable in radians would be a tiny bit faster than converting from degrees every time you use trigonometry.

17-06-2004, 04:34 PM
Thanks :)

Now it actually works!

Here it is:

I am currently developing a little Worms Clone in DelphiX which you can find here, if you are interested:


Now that i have learned more about OpenGL, I think about porting it to OGL..