Sign in to follow this  

How, to make a steady movement

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 globals
double 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
Is it possible in 3D too?


Well of course it is (I assume you mean the last guys post).
You just add another calculation:

x += directionX * speed;
y += directionY * speed;
z += directionZ * speed;

Share this post


Link to post
Share on other sites
Normals? I assume you meant to ask how to normalize in 3D.
It's just the same thing. The length of the 3D vector (dx,dy,dz) is sqrt(dx*dx+dy*dy+dz*dz)
And you find the direction in the same way - just subtract the start position from the destination.
(dx,dy,dz)=(x2,y2,z2)-(x,y,z)=(x2-x,y2-y,z2-z)

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this