Movement algorithm

Put your problem here if it does not fit any of the other categories.

Movement algorithm

Postby DavidRon » Wed Jan 25, 2012 11:53 am

I'm trying to make my sprite move smoothly between two points

for every iteration in run() i run this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void move()
  2.         {
  3.             double conX = 0;
  4.             double conY = 0;
  5.             int nX = 0;
  6.             int nY = 0;
  7.  
  8.                 int speed = 2;
  9.                
  10.                 //int h = mBitmap.getHeight()/2;
  11.                 //int w = mBitmap.getWidth()/2;
  12.  
  13.                 if(mX != targetX && mY != targetY)
  14.                 {
  15.                         int diffX = targetX - mX ;
  16.                         int diffY = targetY - mY ;
  17.                         if(diffX != 0 && diffY != 0)
  18.                         {
  19.                                 if(mX <= targetX)
  20.                                 {                      
  21.                                         nX = mX+speed;
  22.                                         conX = diffY/(double)diffX ;
  23.                                         conY = mY ;
  24.                                         nX+=speed;
  25.                                         mX = nX;
  26.                                         conY += conX ;
  27.                                         mY = (int)conY;
  28.                                 }
  29.                                 else if(mX > targetX)
  30.                                 {
  31.                                         nX = mX-speed;
  32.                                         conX = diffY/(double)diffX ;
  33.                                         conY = mY ;
  34.                                         nX-=speed;
  35.                                         mX = nX;
  36.                                         conY -= conX ;
  37.                                         mY = (int)conY;
  38.                                 }
  39.                                 if(mY <= targetY)
  40.                                 {
  41.                                         nY = mY+speed;
  42.                                         conY = diffX/(double)diffY ;
  43.                                         conX = mX ;
  44.                                         nY+=speed;
  45.                                         mY = nY;
  46.                                         conX += conY ;
  47.                                         mX = (int)conX;
  48.                                 }
  49.                                 else if(mY > targetY)
  50.                                 {
  51.                                         nY = mY-speed;
  52.                                         conY = diffX/(double)diffY ;
  53.                                         conX = mX ;
  54.                                         nY-=speed;
  55.                                         mY = nY;
  56.                                         conX -= conY ;
  57.                                         mX = (int)conX;
  58.                                 }
  59.                         }
  60.                 }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


targetX and targetY changes all the time.

The problem is that it makes a lot of jumps, it seems to be doing bigger jumps the closer mX and mY are to the edges.

mX and mY are the current x,y coords for where the sprites is and targetX and targetY is the target coord.

Bigger mY seems to mean bigger jumps, but im not totally sure.

Anyone got a clue?
DavidRon
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 23, 2012 5:06 pm

Top

Re: Movement algorithm

Postby Phyll » Wed Jan 25, 2012 2:46 pm

Hi DavidRon,

I think you should keep your current locations and targets and all partials that you save from one time to the next as doubles or floats. Storing them as ints may be causing it to kind of jump when it has accumulated enough iterations to jump to the next int position. Change to ints when you use them if you need ints.

The time thru your loop is undoubtedly not the same for each iteration and causes an animation to be jumpy. You might want to consider multiplying the speed times the time from the previous iteration to level that out.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Movement algorithm

Postby BUKsAPPs » Wed Jan 25, 2012 4:31 pm

To get smooth movement you are going to have to do what Phyll suggested, and make your movement a function of the amount of time that passes since the last time move() was called. Unless you are going to force it to be called at a constant rate.

But I am also unsure about your movement algroythm.
Code: Select all
if(mX <= targetX)
{                     
   nX = mX+speed;
   conX = diffY/(double)diffX ;
   conY = mY ;
   nX+=speed;
   mX = nX;
   conY += conX ;
   mY = (int)conY;
}

I think is the same as:
Code: Select all
if(mX <= targetX)
{
   mX += speed+speed;
   mY += (int)(diffY/(double)diffX);
}


If my algebra is right that is half of your movement, and then the complement is done for diffY. But it looks like you are changing mY in your diffX logic, and not only that, you are moving it not a function of your speed. So if diffY is 100 pixels, and diffX is 1 pixel, (you are straight below the target) won't this code move your sprite (100/1) in the y-direction?
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests