Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (800)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (866)
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 Sensitive Jumping  (Read 2351 times)
0 Members and 1 Guest are viewing this topic.
Offline QuicK2800

Senior Newbie


Medals: 1
Exp: 4 years



« Posted 2016-12-22 21:59:55 »

I am working a 3D project.

My movement is scaled by a delta factor so that it is not affected by fps drops, however...
When my fps drops:
1) My jump height is correct
2) My movement speed is correct
3) My gravity's speed is not

Any suggestions as to why this is occurring? (Code below..)
Sorry, I can't figure out how to put the code in a spoiler tag.

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  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
public class Player extends MovableGameObject {
   
   private boolean inAir = false;
   private final float GRAVITY = -0.015f;
   private final float JUMP_POWER = 0.22f;
   private final float WALK_SPEED = 3.5f;
   private final float RUN_MODIFIER = 3f;
   private final float CROUCH_MODIFIER = 2.5f;
   
   private final Vector3f[] collisionPositions;
   private Vector3f hitBox = new Vector3f(0.4f, 1.9f, 0.4f);
   private final int collisionPointCount = 8;
   private int direction;
   
   public Player(TexturedModel model, Vector3f position, float rotX,
         float rotY, float rotZ, float scale) {
      super(model, position, rotX, rotY, rotZ, scale);
      calculateDirection();
   }
   
   public void move(ChunkEngine chunkEngine) {

      Vector2f direction = new Vector2f((float)Math.cos(Math.toRadians(rotY)), (float)Math.sin(Math.toRadians(rotY)));
     
      boolean W = Keyboard.isKeyDown(Keyboard.KEY_W);
      boolean A = Keyboard.isKeyDown(Keyboard.KEY_A);
      boolean S = Keyboard.isKeyDown(Keyboard.KEY_S);
      boolean D = Keyboard.isKeyDown(Keyboard.KEY_D);

     
      int runFlag = isRunning ? 1 : 0;
      int crouchFlag = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) ? 1 : 0;
      if (isRunning)
      System.out.println("Running!");
     
      velocity.x = 0;
      velocity.z = 0;
     
      float speedModifier = (WALK_SPEED + RUN_MODIFIER * runFlag - CROUCH_MODIFIER * crouchFlag);
     
      if (W) {
         velocity.z += -direction.x * speedModifier * DisplayManager.getDelta();
         velocity.x += direction.y * speedModifier * DisplayManager.getDelta();
      }
      if (S) {
         velocity.z += direction.x * speedModifier * DisplayManager.getDelta();
         velocity.x += -direction.y * speedModifier * DisplayManager.getDelta();
      }
     
      if (D) {
         velocity.x += direction.x * speedModifier * DisplayManager.getDelta();
         velocity.z += direction.y * speedModifier * DisplayManager.getDelta();
      }
      if (A) {
         velocity.x += -direction.x * speedModifier * DisplayManager.getDelta();
         velocity.z += -direction.y * speedModifier * DisplayManager.getDelta();
      }
     
     
      if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && !inAir) {
         inAir = true;
         velocity.y = JUMP_POWER;
      }
     
      if (!W && !S) {
         velocity.z *= 0.9f;
      }
     
      if (!D && !A) {
         velocity.x *= 0.9f;
      }
     
      if (velocity.x > 0) {
         if (chunkEngine.getBlockAt(position.x + velocity.x + hitBox.x, position.y, position.z + hitBox.z).getBlockID() != 0
          || chunkEngine.getBlockAt(position.x + velocity.x + hitBox.x, position.y, position.z - hitBox.z).getBlockID() != 0)
            position.x -= velocity.x;
      }
      if (velocity.x < 0) {
         if (chunkEngine.getBlockAt(position.x + velocity.x - hitBox.x, position.y, position.z + hitBox.z).getBlockID() != 0
          || chunkEngine.getBlockAt(position.x + velocity.x - hitBox.x, position.y, position.z - hitBox.z).getBlockID() != 0)
            position.x -= velocity.x;
      }
     
      if (velocity.z > 0) {
         if (chunkEngine.getBlockAt(position.x + hitBox.x, position.y, position.z + velocity.z + hitBox.z).getBlockID() != 0
          || chunkEngine.getBlockAt(position.x - hitBox.x, position.y, position.z + velocity.z + hitBox.z).getBlockID() != 0)
            position.z -= velocity.z;
      }
      if (velocity.z < 0) {
         if (chunkEngine.getBlockAt(position.x + hitBox.x, position.y, position.z + velocity.z - hitBox.z).getBlockID() != 0
          || chunkEngine.getBlockAt(position.x - hitBox.x, position.y, position.z + velocity.z - hitBox.z).getBlockID() != 0)
            position.z -= velocity.z;
      }
     
      if (inAir) velocity.y += GRAVITY;
      else {
         velocity.y = 0;
      }
     
      if (chunkEngine.getBlockAt(position.x, position.y-0.5f, position.z).getBlockID() == 0) {
         inAir = true;
      }
     
      if (velocity.y <= 0) {
         if (chunkEngine.getBlockAt(position.x + hitBox.x, position.y + velocity.y, position.z + hitBox.z).getBlockID() != 0
           || chunkEngine.getBlockAt(position.x - hitBox.x, position.y + velocity.y, position.z + hitBox.z).getBlockID() != 0
          || chunkEngine.getBlockAt(position.x - hitBox.x, position.y + velocity.y, position.z - hitBox.z).getBlockID() != 0
          || chunkEngine.getBlockAt(position.x + hitBox.x, position.y + velocity.y, position.z - hitBox.z).getBlockID() != 0) {
            position.y -= velocity.y;
            inAir = false;
         }
      }

      // Apply velocities to position
      super.increasePosition();
     
      rotX -= Mouse.getDY() * 0.1f;
      rotY += Mouse.getDX() * 0.1f;
     
      // Post-Movement corrections
     
      y[0] = (int)position.getY();
     
      if (rotX > 90) rotX = 90f;
     
      calculateDirection();
   }

   public int getX() {
      return x[0];
   }
   
   public int getY() {
      return y[0];
   }

   public int getZ() {
      return z[0];
   }
}
Offline Catharsis

JGO Ninja


Medals: 76
Projects: 1
Exp: 21 years


TyphonRT rocks!


« Reply #1 - Posted 2016-12-22 23:17:06 »

So yeah... A few things which will lead in a more solid direction. Things get tricky with dynamics / gravity and other related calculations as you have seen.

The quick hack is this.

The next thing to do is this.

Also there are performance problems with your move method. Never create new objects if at all possible in any game loop. A further optimization is to not poll the input device in the move method of your game / entity class! I'll see if I can get a mostly correct example with the quick hack and these optimizations above mostly worked out in some pseudo-code modifications to your posted class in a follow up post.

Check out the TyphonRT Video Suite:
http://www.typhonvideo.com/

Founder & Principal Architect; TyphonRT, Inc.
http://www.typhonrt.org/
http://www.egrsoftware.com/
https://plus.google.com/u/0/+MichaelLeahy/
Offline SteveSmith
« Reply #2 - Posted 2016-12-23 08:48:26 »

Is the gravity speed to high or low?  And I assume you're talking about the value of velocity.y?

Java Multiplayer FPS Engine - https://github.com/SteveSmith16384/SteveTech1
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline QuicK2800

Senior Newbie


Medals: 1
Exp: 4 years



« Reply #3 - Posted 2016-12-26 01:54:16 »

Thank you for the replies guys.

I am currently on vacation and will look back into this when I get back!

However to answer your question:
The gravity appears to be lower when my fps drops however the height of my jump is correct. And it is only the velocity.y that feels this effect.
When my fps is at 60 the gravity speed is fine
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (339 views)
2019-09-04 15:33:17

hadezbladez (5121 views)
2018-11-16 13:46:03

hadezbladez (2003 views)
2018-11-16 13:41:33

hadezbladez (5390 views)
2018-11-16 13:35:35

hadezbladez (1114 views)
2018-11-16 13:32:03

EgonOlsen (4529 views)
2018-06-10 19:43:48

EgonOlsen (5385 views)
2018-06-10 19:43:44

EgonOlsen (3076 views)
2018-06-10 19:43:20

DesertCoockie (3970 views)
2018-05-13 18:23:11

nelsongames (4520 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!