Public Group

#### Archived

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

# moving an line the way its pointing

This topic is 5921 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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]) {
}

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 on other sites
Since you already know what the start and end points of the line are you could also try this:

DeltaX=X2-X1DeltaY=Y2-Y1Length=Sqrt(Sqr(DeltaX)+Sqr(DeltaY))X1+=DeltaX/Length*SpeedX2+=DeltaX/Length*SpeedY1+=DeltaY/Length*SpeedY2+=DeltaY/Length*Speed

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

##### Share on other sites
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 on other sites
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 ...

so

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 on other sites
k thanx...

ill go try out those 2 methods now then

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631781
• Total Posts
3002316
×