#### Archived

This topic is now archived and is closed to further replies.

# Point to Point movement

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

## Recommended Posts

Hello I have a sprite A at position x1,y1 This sprite needs to move directly to position B at x2,y2 Anyone an idea how to do that?

##### Share on other sites
Could you specify a bit more?
Just moving the sprite only requires a variable setting (xpos=x2). OR if you mean how to move the sprite linearly between the two coordinates, then you do something like this:

newx = x1*(1-t) + x2*(t)
newy = y1*(1-t) + y2*(t)

where t is time and ranges from 0 to +1.
(It''s called linear interpolation)

Hope that helps!

/Mankind gave birth to God.

##### Share on other sites

But I do not have the travel duration at the beginning.
The travel dures till the sprite reaches postion B.

But I could give the sprite a speed.
I mean a smooth movement.

Thanks

##### Share on other sites
This is one way to do it:

simple physics:
distance = velocity * time

to get the time needed to travel the distance:
distance = sqrt((x2-x1)^2 + (y2-y1)^2);
t = distance/velocity

Now you calc: tadd = 1/t

tvar = 0
main loop:
newx = x1*(1-tvar) + x2*(tvar)
newy = y1*(1-tvar) + y2*(tvar)
end loop

OK, so whenever you update your screen, you move the sprite by using the code above.

Sorry for my badly structured post.

Anyway, thats ONE way to do it

PS. I forgot, when tvar reaches +1 you're done with the movement!

/Mankind gave birth to God.

Edited by - silvren on September 21, 2001 1:18:49 PM

##### Share on other sites
Thank you!

This helped me a lot.
God bless you

##### Share on other sites
The sprite moves smoothly.
But the velocity is not constant,
although I use a constant value.

t = distance/constant velocity

the velocity-time graph seems to be something like this
v|||          *  *  *  *  *  *  *  *  *|      *                              *|    *                                  *|   *                                    *|  *                                      *|*                                          *0--------------------------------------------- t

I know, I've nothing to do...

Edited by - Van Hostman on September 22, 2001 10:47:31 AM

##### Share on other sites
Ok, (well, actually it's not ok

First thing that comes to my mind is whether you are using a constant screen refresh? I mean, do you sync the screen refresh so that it is constant no matter what calcs you're doing?

to get a constant frame rate:
record the time at the top of your main-loop, t1. At the end of the loop you record the time again, t2. Then you calc the difference t2-t1=t3.
Suppose you want to try to achieve a rate equal to r=30 fps (frames per second).
Then calc wait=1/r [unit=second per frame]
Now you compare t3 and wait. If t3 >= wait, then ignore any delay and return to top of loop. If t3 < wait then wait an amount a=wait-t3 before you return to top of the loop.

Got it?
IF this is not the problem, reply and I'll try to answer as soon as possible!

cheers

/Mankind gave birth to God.

Edited by - silvren on September 22, 2001 12:46:31 PM

##### Share on other sites
To get a constant velocity:
// initial position is at (x0,y0)// destination is (x1,y1)// desired velocity is (vx, vy)while( x0 != x1 && y0 != y1){  if( abs(x1 - x0) > abs(vx) ) x0 += vx;  else x0 = x1;  if( abs(y1 - y0) > abs(vy) ) y0 += vy;  else y0 = y1;}

The absolute difference comparison is to ensure we don''t overshoot our destination. This results in a constant velocity except for the final ''step'', but is less realistic than the solution silvren outlined.

##### Share on other sites
Yep,

That's another way of doing it!

The problem is that I don't think van Hostman has the
vector-components of the velocity-vector! I think he only has a "velocity value". Anyway, I'll give a solution to that problem.

  v is velocity valuedx = x1 - x0dy = y1 - y0if (dx == 0) { vx = 0 vy = v if (dy < 0) { vy = -vy }}else { vx = v / sqrt(1 + dy^2 / dx^2) vy = vx * dy / dx if (dx < 0) { vx = -vx vy = -vy }}

Test this!

cheers

/Mankind gave birth to God.

Edited by - silvren on September 22, 2001 3:25:56 PM

Edited by - silvren on September 22, 2001 5:50:59 PM

##### Share on other sites
Respect, it works!

Now the movement is smooth and constant and direct!

Actual I had only the velocity-value. But the separation in
x and y velocity worked out.

Btw., the FPS is constant. So no problem here.

Now I''ll learn some physics.

Thanks and Bye

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

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

• Total Topics
633270
• Total Posts
3011155
• ### Who's Online (See full list)

There are no registered users currently online

×