Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (724)
Games in Android Showcase (216)
games submitted by our members
Games in WIP (791)
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 845 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: 68
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?

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  
 
 

 
Archive (35 views)
2017-04-27 17:45:51

buddyBro (244 views)
2017-04-05 03:38:00

CopyableCougar4 (668 views)
2017-03-24 15:39:42

theagentd (666 views)
2017-03-24 15:32:08

Rule (715 views)
2017-03-19 12:43:22

Rule (689 views)
2017-03-19 12:42:17

Rule (691 views)
2017-03-19 12:36:21

theagentd (705 views)
2017-03-16 05:07:07

theagentd (637 views)
2017-03-15 22:37:06

theagentd (478 views)
2017-03-15 22:32:18
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!