Moving from point A to point B?
ok I am making a 2d game and I want to move from where the player is to the mouse when I click and I want it to go at a constant speed. How would I do this?
if(Bx!=x) if(Bx<x) x--; else x++;if(By!=y) if(By<y) y--; else y++;
JoeMont001@aol.com www.polarisoft.n3.net
Edited by - Julio on November 18, 2000 4:41:53 PM
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
No that wont work... it needs to go directly to the point and that doesn''t go directly to it. If the player is at 500,300 and is moving to 0,0 it goes to 200,0 then to 0,0 and that''s no good for what I want. thanks anyways
well, it worked for Andre LaMothe, guess you can''t please everybody.
JoeMont001@aol.com www.polarisoft.n3.net
JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
I''d suggest using the slope of the line between the two points. In your example, if the player is at 500, 300, and is moving to 0,0, your slope would be (y2-y1)/(x2-x1), which is (0-300)/(0-500), which gives you a -300/-500=-3/-5. I''m gonna leave them negative for this. Then each time you move the player, at it''s corresponding slope value to it''s current position. So the next frame, add -5 to the players x location, and -3 to it''s y location. This will make him move 3 to the left and 5 up each time you update him. And he will end up at 0, 0. Hope this helps you.
"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka
"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka
I could''ve just said take the second coordinate and subtract it from the second coordinate, but by mentioning the slope, it aids in understanding, plus, you need your values reduced, that''s why I set it up as a fraction.
"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka
"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka
You have to calculate the difference between the old and the new coordinates (substract current position from new position). Lets call the results DX and DY. If you want to go from the old position to the new position in 10 steps, you just have to add DX/10 and DY/10 to the coordinates.
But you want to get a constant speed, so you have to calculate the number of steps. This can be done by calculating the length of the line between the coordinates (length = sqrt(DX * DX + DY * DY), which also is the number of steps you have to take. You can change the speed by multiplying the number of steps by another (say 0.5 = double speed, 1 = normal speed, 2 = half speed).
There''s only one problem...the number of steps you have to take may be a broken number. Just get rid of the numbers after the decimal point (don''t round it, just typecast it or else you may end up with too many steps) and you have the number of steps you have to take. However, DX and DY should still be divided by the real length of the line.
After your loop has finished, the position you got might not be the same as the destination-position (due to the fact that the length of the line could be a broken number), so just copy the coordinates of the destination-position to the new position and you''re ready.
Some pseudo-code:
Hope this helps!
But you want to get a constant speed, so you have to calculate the number of steps. This can be done by calculating the length of the line between the coordinates (length = sqrt(DX * DX + DY * DY), which also is the number of steps you have to take. You can change the speed by multiplying the number of steps by another (say 0.5 = double speed, 1 = normal speed, 2 = half speed).
There''s only one problem...the number of steps you have to take may be a broken number. Just get rid of the numbers after the decimal point (don''t round it, just typecast it or else you may end up with too many steps) and you have the number of steps you have to take. However, DX and DY should still be divided by the real length of the line.
After your loop has finished, the position you got might not be the same as the destination-position (due to the fact that the length of the line could be a broken number), so just copy the coordinates of the destination-position to the new position and you''re ready.
Some pseudo-code:
SX = start XSY = start YEX = end XEY = end YDX = EX - SXDY = EY - SYLENGTH = sqr(DX * DX + DY * DY)STEPS = (int)LENGTHFOR STEP = 0 TO STEPS SX = SX + DX/LENGTH SY = SY + DY/LENGTHSX = EXSY = EY
Hope this helps!
--------------------------Programmers don't byte, they nibble a bit. Unknown Person-------------------------
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement