Jump to content
  • Advertisement
Sign in to follow this  
DMonaghan

Movement between 2 points

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

I have a basic 2d isometric sytle game, I want to move my character between the center of the current tile to the center of the destination tile. I have the global XY and global destination XY; How do i move smoothly along the line beteen these 2 points. this is the method im using below for each cycle, how the character just moves to the right and no other direction desipte feeding in different values, obviously i have it wrong, andone with ideas? if(hasDestination) { ..... float xFP,yFP // Float version of xY int dispX, dispY; dispX = destGlobalX - currentX; dispY = destGlobalY - currentY; setX(MathFP.toInt(MathFP.add( xFP, MathFP.cos(dispX) ))); setY( MathFP.toInt(MathFP.add( yFP, -MathFP.sin(dispY) ))); ........ }

Share this post


Link to post
Share on other sites
Advertisement
If you want to move between the two point linearly (on a line), the simply use the Bresenham algorythm (if you can move only between the midpoints of the tiles).
Iy not, the use a simple displacement, calculated from the desired displacement vector:
your two points: A and B (2 2D vectors)
you displacement vector is D = B-A. You want to add it to the starting point (A) to get to (B). of yours you dont want to "jump" from A to B in one step, so you have to divide this displacement. Let u be the stepsize, the distance, your object can move in one movement. In this case, you have to move your object n = abs(D)/u times where abs(D) is the distance between A and B, and you have to step with the vector: (S = D/n) Now you need a loop:
for(int i=0;i<n;i++)
{
Myobject.PositionVector +=S; //displyce wour object
totalDisplacement+=S //was set to 0 before the loop
AnimationDelay() //to make changes visible. of course you dont need this anyway
}
Finally you have to add D-totalDisplacement to the PositionVector becouse of the rounding error (when calculated n, it was truncated);

i hope you understand my "tut" :)

Share this post


Link to post
Share on other sites
Moving along a line, given 2 Points of a line is:
pos(t)=p1+t*(p2-p1)

more detailed:
pos(t).x=p1.x+t*(p2.x-p1.x) and pos(t).y=p1.y+t*(p2.y-p1.y) with t in [0;1]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!