# Moving from one point to another with a given speed

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

## Recommended Posts

Hi everyone I'm starting a project of a game, and in the engine I want to implement mouse clicking movement. So that ghne user clicks a spot and the object moves there. The problem is i can't figure out how to do it Let's say I have my object on X:0 , Y:0 and the user clicks on X:10 , Y:20 And the objects speed is 2 pixels every loop. How can i calculate the position for every loop? I'm really lost since i've never done This kind of ecuations though I love Maths Thanx!

##### Share on other sites
First you have to find the direction. To do that you need the vector between the two points and the length of vector.

Xd = X1-X0
Yd = Y1-Y0
l = sqrt(Xd^2 + Yd^2)

Then you need to normalize the vector:

Xd = Xd/l
Yd = Yd/l

Then multiply by 2:

Xd = Xd*2
Yd = Yd*2

You then add Xd,Yd to the position every frame

Edit: You must use floats for that to work

##### Share on other sites
Thanx a lot, but since I ams using Pixels, i think i cant use Floating numbers.. am i right?

How could i do this using INT since they are pixels?

##### Share on other sites
You can use floats internally and then when drawing it round off to pixels.

To get the same result without floats is possible but much more complicated. You can calculate the distance between the points, divide by the velocity (pixels per step) to get the total number of steps required, then keep track of what step you are on. Current position = start + ((goal - start) * step)/totalsteps. It's kind of messy because to really work right it needs to remember where the starting position was.

Actually if you don't mind walking faster diagonally than horizontally or vertically, you can just determine which direction to move each cycle separately for the x and y directions, and then just move in that direction, checking to see that you don't move past the goal (if you move past the goal just snap back to it). Either the current x position is less than, equal to, or greater than the goal position. Then you just add 2 (then if you're greater than the target x just set current x equal to target x), do nothing, or subtract 2 (and then if you're less than target x set curent x equal to target x). This will make you move faster diagonally than horizontally or vertically but that isn't necessarily a problem, it depends what you're trying to do.

##### Share on other sites
ok. I got half way to it

This is my code

			if ( ( SprPosX <= ( PosDestX + 2 ) | SprPosX >= ( PosDestX - 2 ) )				&& ( ( SprPosY <= PosDestY + 2 | SprPosY >= PosDestY - 2 ) ) )			{								float Xd = ( PosDestX - ( SprPosX + ( WIDTH_SPRITE / 2 ) ) );				float Yd = ( PosDestY - ( SprPosY + ( HEIGHT_SPRITE / 2 ) ) );				float length = sqrt( pow(Xd,2) + pow(Yd,2) );				Xd = ( Xd / length ) * 2;				Yd = ( Yd / length ) * 2;							if ( SprPosX != PosDestX )					SprPosX += (Xd);// + ( WIDTH_SPRITE / 2 ) );				if ( SprPosY != PosDestY )					SprPosY += (Yd);// + ( HEIGHT_SPRITE / 2 ) );			}

So thats what i have for now But it acts really strange. some times goes all the way to de other corner.

Maybe I'm messing with the collision detection With the Window Borders.

And sometimes just stops wherever it wants.

Any help?

##### Share on other sites
The condition in your if statement is very very strange. You seem to be using | instead of ||. In C++, || is logical or. | is bitwise or. I doubt that you mean to use bitwise or. In addition, if you were using ||, the condition as written would seem to always evaluate to true, since for number A and B it will always be the case that A is less than B+2 or A is greater than B-2. I think you have the signs wrong on the +2 and -2.

Also there should be no need to consider the size of the sprite in this part of the program. You can account for the size of the sprite when calculating the destination.

##### Share on other sites
You are right with the OR operator. LOL.

Im sorry, im just staring

And the thing with the width is that I want it to o exaclty where it was clicked.

So I calculate the middle point of the sprite.

It's a simple ball So the sprite is a square

the other probloem is which way is it going. if it goes UP LEFT it has to decrease, if it has to gow LEFT DOWN y has to increase.. so.. i cant' figure out how to check Wich way has to go but with that EXTREMELY STRANGE LONG UNSTABLE IF statement

LOL

Any reccomendations?

##### Share on other sites
This shouldn't be that hard.

Find the x distance from target, find the y distance. Do the tan inverse(y/x). This will give you the angle of the target from the nearest x axis, with the player being the origin.

Hopefully this triangle will help explain the next part:
       T      /|     / |    /  |   /   |  /    |P/_____|P = Player T = Target

You want the hypotenuse above to be 2, no matter what. So, to get the x movement, you do cos(Angle) = x / 2, so x = 2 * cos(Angle). Angle is the angle you found above with the inverse of (y/x). Then for the y movement, you do the same, it being y = 2 * sin(Angle). Now this will move you one step towards your target. To figure out how many steps you need, find the distance from your player to the target point. Then take that number, and continually move your character while that number is greater than 0, taking away 2 every time. So if you get 20.0 as the distance, you will have a loop like this:
for(int e=20; e>=0; e-=2){moveChar by xmoveChar by y}

And the last part, since you wanted it to be EXACTLY on the dot where they click, you can do this in your main loop:
for(int e=20; e>=0; e-=2){ if(e>=2) {  move character by x  move character by y } else {  move character by e * cos(Angle)  move character by e * sin(Angle) }}

There you have it! That'll put you exactly on the spot, wherever they clicked. You won't have loops exactly like that, rather you would only do that once per frame or whatever kind of time frame you have set up. If you just did that loop above for a frame, it would look like the guy is teleporting because it would loop until he arrived at the position. Good luck!

[Edited by - IgnisDeus on September 11, 2005 5:17:42 AM]

##### Share on other sites
Thanx. I'll try this one!!!

I'll let you know!

1. 1
Rutin
35
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633340
• Total Posts
3011422
• ### Who's Online (See full list)

There are no registered users currently online

×