Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
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  
  FPS dependant movement  (Read 1935 times)
0 Members and 1 Guest are viewing this topic.
Offline darcone

Junior Devvie




Size matters


« Posted 2003-03-31 20:06:57 »

I am getting 450-500 FPS in my game engine using LWJGL. Aware of the problem that machines will get very different FPS I used my code that I used with my old game engine (using fullscreen exclusive mode) to adjust the speed my vehicles/players can make every frame. Basically this is what I do:

I get the Time Delta/frameTime

I multiply every speed constant with that time delta

result should be same speed on all systems because high fps would lead to low speed/frame and low fps would lead to high speed/frame.

Somehow though, this won´t work when I use LWJGL. I always get faster movement when higher FPS. I am aware that I might have some error in my code that can cause this but there might as well be some logic problem that I am not aware of or limitation in java?

Here is my code (Chrono is my FPS capturing class, captFPS captures the fps and getFrameTime() gets frame time delta)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
/*
 * Vehicle.java
 *
 * All vehicles are to be handled by this class
 * Driver is Object Player player and null if vehicle is empty
 *
 * Created on den 26 mars 2003, 20:44
 */


/**
 *
 * @author  digizard
 */


public class Vehicle {
   
    private String Name = "Panzer";
    private org.lwjgl.opengl.GL gl;
    private float posX, posY, posZ;
    private Player player;
    private int texID, FPS;
    private Chrono chrono;
    private double currentSpeed, maxSpeed, slowdownForward, slowdownBackwards, accForward, accBackwards, angle, angleSpeed;
   
    public Vehicle(int texID) {
       
        this.gl = Schlug.gl;
        this.texID = texID;
       
        chrono = new Chrono();
       
        player = null;
        maxSpeed = 1;
        currentSpeed = 0;
        slowdownForward = 0.1;
        slowdownBackwards = 0.1;
        accForward = 0.01;
        accBackwards = 0.001;
        angle = 0;
        angleSpeed = 2;
    }
   
    public void draw() {
       
        posX += currentSpeed * java.lang.Math.cos(java.lang.Math.toRadians(angle));
        posY += currentSpeed * java.lang.Math.sin(java.lang.Math.toRadians(angle));
       
        if(player != null) player.setPos(posX, posY, posZ);
       
        if(currentSpeed > 0) currentSpeed -= java.lang.Math.abs(currentSpeed) * slowdownForward * chrono.getFrameTime();
        else if(currentSpeed < 0) currentSpeed += java.lang.Math.abs(currentSpeed) * slowdownBackwards * chrono.getFrameTime();
       
        gl.loadIdentity();

        if(player != null) gl.translatef(0.0f, 0.0f, posZ);
        else gl.translatef(posX, posY, posZ);
       
        gl.rotatef((float) angle, 0.0f, 0.0f, 1.0f);
       
        gl.bindTexture(org.lwjgl.opengl.GL.TEXTURE_2D, texID);
   
        gl.begin(org.lwjgl.opengl.GL.QUADS);
            gl.texCoord2f(0.0f, 1.0f);
            gl.vertex3f(-1.0f, -1.0f, 0.0f);
            gl.texCoord2f(1.0f, 1.0f);
            gl.vertex3f(1.0f, -1.0f, 0.0f);
            gl.texCoord2f(1.0f, 0.0f);
            gl.vertex3f(1.0f, 1.0f, 0.0f);
            gl.texCoord2f(0.0f, 0.0f);
            gl.vertex3f(-1.0f, 1.0f, 0.0f);
        gl.end();
    }
   
    public void setPos(float posX, float posY, float posZ) {
        this.posX = posX;
        this.posY = posY;
        this.posZ = posZ;        
    }
   
    public void setAngle (double angle) { this.angle = angle%360; }
   
    public double getAngle() { return angle; }  
   
    public double getAngleSpeed() { return angleSpeed * chrono.getFrameTime(); }
   
    public void accelerate(boolean forward) {
        if(java.lang.Math.abs(currentSpeed) < maxSpeed) {
            if(forward) currentSpeed += accForward * chrono.getFrameTime();
            else currentSpeed -= accBackwards * chrono.getFrameTime();
        }
    }
   
    public void setPlayer(Player player) { this.player = player; }
    public float getX() { return posX; }  
    public float getY() { return posY; }
}


Please suggest any changes if you see anything obvious.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2003-03-31 21:04:32 »

An educated guess would be that you're just using Sys.getTime(). But this is in *ticks* not seconds or milliseconds - you need to divide by Sys.getTimerResolution() to get a proper time delta.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline darcone

Junior Devvie




Size matters


« Reply #2 - Posted 2003-04-01 04:43:19 »

Yes, I do not have that code here (sitting in school with laptop) but what I do to get the time delta is something like...

(Sys.getTime() - temp)/Sys.getTimerResolution();

Any other ideas?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #3 - Posted 2003-04-01 04:56:20 »

Don't know exactly what you are doing there with your speed, but this look wrong, bc. it does not depend on time:

1  
2  
3  
   
posX += currentSpeed * java.lang.Math.cos(java.lang.Math.toRadians(angle));
posY += currentSpeed * java.lang.Math.sin(java.lang.Math.toRadians(angle));


Most simple motion is described by:

1  
x' = x + v*dt


accForward, accBackwards, slowdownForward and
slowdownBackwards seem to take the role of accelerations correctly. For acceleration is constant during the time of one frame, you you even enhance your intergration of position to

1  
x' = x + v*dt + 0.5*a*dt²


HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline darcone

Junior Devvie




Size matters


« Reply #4 - Posted 2003-04-01 05:28:36 »

First I thaught you had solved my problem, but when I looked again I see that there is no reason why my method should not work? Ok here it is..

For accelerating, the method accelerate(boolean forward) is called. It is called every frame (actually every time the program detects that keyboard press) so it would be called 400 times a second if the fps is 400.

Therefore, the currentSpeed variable would be incremented every second * FPS. And each frame it would be currentSpeed += accForward * td.
Consequently, it would inrement the currentSpeed with the same amount /second in all FPS:es. The posX += cos/sin etc code is the code for moving the ship depending on the currentSpeed variable, and then currentSpeed is decremented with the slowdown* variables depending on the td also.

I see nothing wrong with this, or is there another way that I should handle this?
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #5 - Posted 2003-04-01 06:01:48 »

1  
2  
x' = x + v*dt
v'
= v + a*dt

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
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.

Gibbo3771 (10 views)
2014-11-24 19:59:16

trollwarrior1 (35 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!