This topic is now archived and is closed to further replies.


moving an line the way its pointing

Recommended Posts

stickman    122
ok... so right now i have a line and it rotates within a circle when i press the left and right keys... i want it to move is the direction that its pointing... so if the line goes from 20,80 to 50,50 and when i push my up arrow i want the line to move in the direction its pointing to... i was thinking of using the slope of the line and stuff... but i dunno... if u know how to do this plz help

Share this post

Link to post
Share on other sites
kenwi    122
Well.. It''s fairly easy..

the direction would be defined with the X and Y axis.. As I''ve understood the line rotates when you press right and left. I would call that the heading, right? Left and right arrowkey add and substract from the heading. we make a variable called heading. When you press forward you want it to go in the direction you''ve rotated in, this gives something like this.

if (keys[VK_UP]) {
position.x+= cos(heading*piover180)* movementspeed;
position.y+= sin(heading*piover180)* movementspeed;

and that''s about it.. piover180 is a constant used to translate radians into degrees cause sin and cos function only returns in radians.. you use movementspeed to increase the speed of movement because as we know cos and sin does only vary between -1 and 1.. Hope this help you a little on the way.
Btw: I''ve might be wrong in the usage of cos and sin on the x and y axis.. if I''ve done that you only swap them

Good Luck!

Kenneth Wilhelmsen
Try my little 3D Engine project, and mail me the FPS. WEngine
Or just visit my site
He who joyfully marches to music in rank and file has already earned my contempt. He has
been given a large brain by mistake, since for him the spinal cord would fully suffice. This
disgrace to civilization should be done away with at once. Heroism at command, senseless
brutality, deplorable love-of-country stance, how violently I hate all this, how despicable and ignoble war is; I would rather be torn to shreds than be a part of so base an action! It is my conviction that killing under the cloak of war is nothing but an act of murder

Share this post

Link to post
Share on other sites
Michalson    1657
Since you already know what the start and end points of the line are you could also try this:




This will move the line(X1,Y1,X2,Y2) in the direction of the second point(X2,Y2) at a speed of Speed.

Share this post

Link to post
Share on other sites
stickman    122
ok i haven''t tried implemeting that last one... but im asumming it works right now...

one thing i dont know how to deal with is if my speed is only 1 pixel... the distance moved will get lost in the rounding everytime...

like it my point would go from 50 to 49.8 ... how can i deal with this?

Share this post

Link to post
Share on other sites
Xai    1838
Store the numbers internally as float, then truncate to integer to draw ... this way ... if you are moving 0.25 pixels each frame you will be at 0, 0.25, 0.50, 0.75, 1.0 etc ... so your object would be drawn at 0, 0, 0, 0, 1 ... so even though you don''t see movement every frame, the world remembers the position accurately.

Also, if you''d rather round ... a quit way to round a float to an int is to add 0.5 before casting ...


float f1 = 7.45;
float f2 = 7.51;
int i1 = f1 + 0.5; // yields 7.95 which is 7
int i2 = f2 + 0.5; // yields 8.01 which is 8

Share this post

Link to post
Share on other sites