Sign in to follow this  

Movement between 2 points

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
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:

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this