# 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.

## 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 on other sites
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 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 on other sites
You don't need to use sine and cosine if you already have the x and y displacements.

##### Share on other sites
you'll NEVER need sine and cosine in linear motion :)

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001841
×