How, to make a steady movement

Started by
11 comments, last by TomasH 19 years, 8 months ago
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?
Advertisement
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;
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...
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.
I said... I allready have a steady execution solution!
My problem is: how do make it move in a strait line towards a point with an adjustable, controlable speed.
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]
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?
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]
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.
Is it possible in 3D too?

This topic is closed to new replies.

Advertisement