Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Slick2D faster movements at higher FPS  (Read 1996 times)
0 Members and 1 Guest are viewing this topic.
Offline Kronos

Junior Devvie


Medals: 1



« Posted 2013-04-09 19:25:13 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
@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?
Offline dime26

Senior Devvie


Medals: 2
Projects: 3
Exp: 5 years


Should traffic wardens be armed?


« Reply #1 - Posted 2013-04-11 00:52:54 »

delta is the time since last call, so:

1  
2  
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

1  
2  
3  
4  
5  
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;





 
Offline dime26

Senior Devvie


Medals: 2
Projects: 3
Exp: 5 years


Should traffic wardens be armed?


« Reply #2 - Posted 2013-04-11 00:56:09 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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);
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Grunnt

JGO Kernel


Medals: 95
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #3 - Posted 2013-04-11 08:14:01 »

1  
tmpY += gravity.y * body.acceleration * time;

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:

1  
2  
3  
4  
5  
6  
// 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).

Offline Kronos

Junior Devvie


Medals: 1



« Reply #4 - Posted 2013-04-12 11:20:58 »

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
Offline Grunnt

JGO Kernel


Medals: 95
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #5 - Posted 2013-04-12 11:48:11 »

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

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

x = x ??

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (60 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!