Advertisement

Point to Point movement

Started by September 21, 2001 08:11 AM
8 comments, last by Van Hostman 23 years, 4 months ago
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?
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.
/Mankind gave birth to God.
Advertisement
Thanks for the reply.

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
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)
tvar += tadd
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
/Mankind gave birth to God.
Thank you!

This helped me a lot.
God bless you
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
Advertisement
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
/Mankind gave birth to God.
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.
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.
Boring maths/formulas ahead!

      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
/Mankind gave birth to God.
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

This topic is closed to new replies.

Advertisement