Slick Forums

Discuss the Slick 2D Library
It is currently Thu May 23, 2013 6:35 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Help with delta?
PostPosted: Wed May 02, 2012 6:43 pm 
Offline

Joined: Wed Apr 25, 2012 2:26 pm
Posts: 13
Ok so I'm trying to make my player movement frame rate independent how ever it's not going so well.

Under the impression delta's max value is 1 (for 1 second) and wanting my player to move at a speed of 8 pixels per second to test with I used:

Quote:
position.y -= (8 * delta);


However this throws the player like 16 times the distance. I did a little digging on the forums and tried adding this:

Code:
gc.setSmoothDeltas(true);


Still no difference, so I did a println of delta and its throwing 15s & 16s? Is anyone able to help me out?


Top
 Profile  
 
 Post subject: Re: Help with delta?
PostPosted: Wed May 02, 2012 7:02 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
Delta is in milliseconds. You'd use it like so:
Code:
final int PLAYER_MOVE_SPEED = 0.1f;

player.x += delta * PLAYER_MOVE_SPEED;


Experiment with different speeds to see what works for your game. I'd also suggest capping the delta with setMaximumLogicInterval, otherwise a single spike will have your player going wildly out of frame and have them missing boxes, etc. Slick will take into account the cap and play "catch up" by updating the scene multiple times before rendering it, if need be.


Top
 Profile  
 
 Post subject: Re: Help with delta?
PostPosted: Wed May 02, 2012 9:45 pm 
Offline

Joined: Wed Apr 25, 2012 2:26 pm
Posts: 13
Thanks for the suggestion I did try that beforehand but it didnt work. (It did if I Math.ceil it)

Im using a grid system, with a playerposition and destination variable. If a key is pressed it adds/substracts the tilewidth from the destination. If the playerpos != destination then we move.

Heres the barebones..

Update:
Code:
if (player.getDest().x == player.getPos().x && player.getDest().y == player.getPos().y) {
                CheckMovementInput(gc);
}
else {
                player.Move(delta);
}


CheckMovementInput:
Code:
if (gc.getInput().isKeyDown(Input.KEY_W)) {
            if (player.getPos().y != 0)
                player.setCoordY(-1);
}


player.Move:
Code:
public void Move(int d) {
        // speed = 0.01f;

        // Move our player on the X axis
        if (destination.x < position.x) {
            position.x -= Math.ceil(speed * d);
        }
        if (destination.x > position.x) {
            position.x += Math.ceil(speed * d);
        }
       
        // Move our player on the Y axis
        if (destination.y < position.y) {
            position.y -= Math.ceil(speed * d);
        }
        if (destination.y > position.y) {
            position.y += Math.ceil(speed * d);
        }
}


player.SetCoordX:
Code:
public void setCoordX(int value) {
        coords.x += value;
        destination.x += value * Map.tileWidth;
}


Ideally i'd like to be able to move a full tile or half a tile per second.


Top
 Profile  
 
 Post subject: Re: Help with delta?
PostPosted: Thu May 03, 2012 2:23 pm 
Offline

Joined: Sun Dec 25, 2011 5:48 pm
Posts: 34
What isn't working exactly?

Take a look at this piece of code:
Code:
  if (destination.x < position.x) {
       position.x -= Math.ceil(speed * d);
  }

The problem here is that you're using Math.ceil. Lets say your frame rate is 60 which means the delta will be 1000/60 ≈ 16.
You want your player to move exactly one grid cell (which is lets say 32 pixels). That means he have to move 32 pixels in one second = 32/1000 = 0.032 pixels per millisecond. Since your delta value is 16, the speed increase each frame will be 0.032 * 16 = 0.512.

Here lies the problem with Math.ceil. the 0.512 is getting rounded up to 1. So instead of moving 32 pixels per second, the player instead moves 60. And you can't just decrease the speed, since it will always round up to 1.

Math.ceil is bad in this case. Remove it. Instead, make your player position variables floats/doubles (preferably floats) instead of ints if you haven't already. Since you cant render something using floats, you'll have to convert them into ints before you do that. player.render((int)player.x, (int)player.y);.

Try changing the movement code to this instead:

Code:
// speed = tiles per seconds.
// gridSize = the size of each cell in the grid measured in pixels.

if (destination.x > player.x) {
   player.x += speed*gridSize*delta/1000f;
}


Using that, it's really easy to set the correct speed. Please note the 'f' in delta/1000f. It must be there to specify that the 1000 is a float and not an integer. If it was an integer, then delta would be divided using integer division and it would always be 0.

Try doing this and then report back if it still doesn't work. Good luck!


Top
 Profile  
 
 Post subject: Re: Help with delta?
PostPosted: Thu May 03, 2012 3:17 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
I'm trying a similar system (smooth movement between grid/tiles) but the player never lands directly on the exact desired position because it's adding floats and not integers. I've tried checking for when it gets close (tried using Math.floor and Math.round) and then snapping exactly with player.x = destination.x.

With this method it caused some weird issues where the player would get stuck for a little while before snapping, and sometimes would never get close enough to trigger the snap, or would skip over that point and get stuck.

I'm going to try now just getting the overall distance as a condition for snapping.


Top
 Profile  
 
 Post subject: Re: Help with delta?
PostPosted: Thu May 03, 2012 3:29 pm 
Offline

Joined: Sun Dec 25, 2011 5:48 pm
Posts: 34
Yeah I should've added that to my previous post.

Code:
if (destination.x > player.x && destination.x < player.x + speed*gridSize*delta/1000f ) {
   player.x = destination.x;
} else if (destination.x > player.x) {
   player.x += speed*gridSize*delta/1000f;
}


This will "snap" the player to the destination if he's close enough.


Top
 Profile  
 
 Post subject: Re: Help with delta?
PostPosted: Thu May 03, 2012 3:46 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
Epicbo wrote:
Yeah I should've added that to my previous post.

Code:
if (destination.x > player.x && destination.x < player.x + speed*gridSize*delta/1000f ) {
   player.x = destination.x;
} else if (destination.x > player.x) {
   player.x += speed*gridSize*delta/1000f;
}


This will "snap" the player to the destination if he's close enough.


This works perfectly for me. Thanks!


Top
 Profile  
 
 Post subject: Re: Help with delta?
PostPosted: Thu May 03, 2012 6:36 pm 
Offline

Joined: Wed Apr 25, 2012 2:26 pm
Posts: 13
Yeah this was absolutely brilliant, thanks for the help I understand where I went wrong now!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group