Jump to content
  • Advertisement

Archived

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

Van Hostman

Point to Point movement

This topic is 6174 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

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


Link to post
Share on other sites
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

Share this post


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

Share this post


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


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


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


Link to post
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.
Boring maths/formulas ahead!

      
v is velocity value

dx = x1 - x0
dy = y1 - y0

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


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!