Slick2D faster movements at higher FPS

Hello everyone,

I got a problem with slick2D. The movements are faster at higher FPS rates, although I multiply with the delta given in Update();

Example:


@Override
public void update(GameContainer gc, int delta) throws SlickException {
myUpdate(delta);
}

public void myUpdate(int delta) {				
tmpY = body.getBounds().getY();
tmpY += gravity.y * body.acceleration * delta;
body.getBounds().setY(tmpY);
}

Aft 300 FPS the movement is much faster than 60 FPS. This is very frustrating since I am doing such stuff for weeks and now it won’t work. Where is the problem?

Am I using delta wrongly?

delta is the time since last call, so:


public void render(float delta) {		
		System.out.println(delta);

For me outputs:
0.05116384
0.001541041
0.001429381
0.009548214
0.016011044
0.016953558

Multiplying delta by any amount will increase its value 0.001541041 * 300 = 0.4623123

Maybe you want a new variable of time and after each tick + delta to it


float time = 0;

public void render(float delta) {		
  time += delta;
  System.out.println("Time Passed: " + time);

Output

Time Passed: 0.053496115

Time Passed: 1.0913002

Time Passed: 1.2415092

Now try something like:
tmpY += gravity.y * body.acceleration * time;

I should have just done this, I must learn to stop multi posting!


float time = 0;

@Override
public void update(GameContainer gc, int delta) throws SlickException {
myUpdate(delta);
}

public void myUpdate(int delta) {    
time  += delta;        
tmpY = body.getBounds().getY();
// set a max speed
tmpY += gravity.y * body.acceleration * time;
body.getBounds().setY(tmpY);
}

That is incorrect. Multiplying by absolute time just means that the Y velocity will increase over time. I.e. at time=100 your Y position will change 100 times as fast as at time=1. Which I doubt is the intended effect.

I’m not so good at math like this, but I think you can solve this by giving your bodies a velocity. Now you both need to multiply changes to velocity and the addition of velocity to the position with the time delta. Maybe something like:

// Determine acceleration using the forces working on the body (i.e. gravity)
body.acceleration = gravity.y;
// Update velocity using the acceleration (multiplied by delta)
body.velocity.y += body.acceleration * delta;
// Update position using the velocity (multiplied by delta)
body.position.y += body.velocity.y * delta;

Also, your issues may be caused by not having a fixed timestep which may have unpredictable results for high time deltas (especially with acceleration involved). Try reading this on fixing your timestep. In slick2d you may try this using app.setTargetFrameRate(int frameRate) (where app is your AppGameContainer).

Thanks for your answers. I always tohught this way:

lower FPS -> higher Delta
x = velocity * delta;

higher FPS -> lower Delta
x = velocity * delta;

x = x ??

Or is the problem at the acceleration, because First I accelerate without multiplying with delta, and use the accelerated value for my movement…?

On the other hand setTargetFPS(60) seems to be a good way to get rid of it, but it’s not the best way I think.

Are there other suggestions? Thanks

[quote=“Kronos,post:5,topic:41480”]
That is correct, unless the velocity changes due to acceleration within the time period indicated by delta. The mathematically ‘correct’ way to handle such situations is quite complex (at least for people like me who are not overly mathematically inclined) involving integration and other stuff to avoid different deltas having different effect. Even then, collision detection brings additional difficulties.

By far the simplest way to do this is simply fix your timestep. If delta is a constant, small value, you wont have to bother with all the mathematical complexities. Take a look at the game loop ra4king posted earlier here, it’s simple and it works. I use a loop based on this one, with some minor adjustments.