View Full Version : Moving Objects in direction of an angle
hannes
17-06-2004, 01:33 PM
Hi!
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Ź???
X:=x+cos(Angle);
Y:=Y+sin(Angle);
hannes
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..
Paulius
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.
hannes
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..
Paulius
17-06-2004, 02:43 PM
Post yore angle changing code
hannes
17-06-2004, 02:47 PM
ok here we go:
if (links) AND (angle < 360) then angle := angle +1;
if (rechts) AND (angle > 0) then angle := angle -1;
if (links) AND (angle = 360) then angle := 0;
if (rechts) AND (angle = 0) then angle := 360;
links & rechts are checked in OnKeyDown links is true if VK_LEFT is pressed and rechts if VK_RIGHT..
(Undone in OnKeyUp)
Paulius
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).
hannes
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 :=xko - Cos(360-angle*(pi/180))*Speed;
yko :=yko + Sin(360-angle*(pi/180))*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)
http://www.siedla-design.de/space.zip
Please have a short look..
Paulius
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.
hannes
17-06-2004, 04:34 PM
Thanks :)
Now it actually works!
Here it is:
http://www.siedla-design.de/space.zip
I am currently developing a little Worms Clone in DelphiX which you can find here, if you are interested:
http://www.siedla-design.de/worms.rar
Now that i have learned more about OpenGL, I think about porting it to OGL..
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.