# How, to make a steady movement

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

## Recommended Posts

OK, here's the thing I want to do: an object (my little triangle) moving at a constant speed towards some coordinates in space (mouse coordinates). I fixed the mouse and object coordinates allready, and now I just need a way to move at a steady speed. I thought of this: x += (((x-x2)/100)*speed) where x is triangle.x and x2 is mouse world coordinates. Are there other, better ways to do it?

##### Share on other sites
get difference vector (position of target minus position of target),and normalize(divide by it's length). Then multiply by speed you want,and dt (time change,time between last frame and current frame). Add resulting vector to coordinates of triangle.
//in globalsdouble curTime,lastTime,dt;............lastTime=curTime;curTime=Double_TimeInSeconds();dt=curTime-lastTime;xd=(x2-x);yd=(y2-y);l=1/sqrt(xd*xd+yd*yd);x+=xd*l*dt;y+=yd*l*dt;

##### Share on other sites
Wow...
Ok, eh, can you repeat that without the timing things?
I allready have a executions per second system, so I don't need it.
And please give examples with understandable variables if you know what I mean...

##### Share on other sites
Multiply your desired movement by the number of miliseconds between the last frame and this. If you want to move at 10 units per second, then multiply 10 by dT, which acts as a scalar for time.

dT = thisTickTime - lastTickTime

Remember that milliseconds are often stored as a long, so you'll need to divide it by 1000 to get the actual seconds involved.

##### Share on other sites
My problem is: how do make it move in a strait line towards a point with an adjustable, controlable speed.

##### Share on other sites
Ok, the "problem" with the method you posted is that the speed is not constant. The object will slow down as it approaches the destination coordinates. Of course, this is only a problem if you really need a constant speed, because this can actually look good (the object won't stop abruptly, but smoothly slow down.)

Say that you want movement from (x,y) to (x2,y2). The first thing you need to do is to get the direction:
(dx,dy)=(x2,y2)-(x,y)=(x2-x,y2-y)
If we just multiply this with a speed, we will move faster the greater the distance is, so as Dmytry said we need to normalize it - i.e. make it have a length of one. To do so, we first calculate the length of the direction vector:
length=sqrt(dx*dx+dy*dy)
And then divide by it:
dx/=length
dy/=length
Then, if you have your speed in units per frame, you would just do:
x+=speed*dx;
y+=speed*dy;
And there you have it, a way to make the object move with constant speed [smile]

##### Share on other sites
I still don't really understand that... (sorry for this)
But what if you'd set your x-x2 before you start moving to the point and then not changing it untill you reach that point.
and to go to the point you use a sort of a percent system, like I showed in the first post.
Possible?

##### Share on other sites
Yes, you could do it like that - you would get a constant speed. With that method it would always take the same time to move from one point to another, the further apart the points are, the faster the movement would be.
It all depends on whether you want all movements to take the same time, or all movements to have the same speed. [smile]

##### Share on other sites
The snag with the x-x2 method is that its not consistant - you'll get faster speeds when the distance between the points is greater, which isn't what you want.

Imagine the x-x2 as creating an arrow from the start point. That gives the direction of the movement you want, but because its length is dependant on the distance between the points you can't use it directly - you have to normalise it first.

Normalisation is simple, you find the length of the vector/arrow and divide by it. Just as TomasH has already written.

If you've got a fixed number of updates per second, your movement becomes:
x += directionX * speed;
y += directionY * speed;

Where speed is some random number you've picked, and directionX/Y is your normalised direction.

##### Share on other sites
Is it possible in 3D too?

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631355
• Total Posts
2999522
×