Note that there are some explanatory texts on larger screens.

plurals
  1. POThe Math of a Jump in a 2D game
    text
    copied!<p>I'm working in J2ME, I have my gameloop doing the following:</p> <pre><code>public void run() { Graphics g = this.getGraphics(); while (running) { long diff = System.currentTimeMillis() - lastLoop; lastLoop = System.currentTimeMillis(); input(); this.level.doLogic(); render(g, diff); try { Thread.sleep(10); } catch (InterruptedException e) { stop(e); } } } </code></pre> <p>So it's just a basic gameloop, the <code>doLogic()</code> function calls for all the logic functions of the characters in the scene and <code>render(g, diff)</code> calls the <code>animateChar</code> function of every character on scene, following this, the <code>animChar</code> function in the Character class sets up everything in the screen as this:</p> <pre><code>protected void animChar(long diff) { this.checkGravity(); this.move((int) ((diff * this.dx) / 1000), (int) ((diff * this.dy) / 1000)); if (this.acumFrame &gt; this.framerate) { this.nextFrame(); this.acumFrame = 0; } else { this.acumFrame += diff; } } </code></pre> <p>This ensures me that everything must to move according to the time that the machine takes to go from cycle to cycle (remember it's a phone, not a gaming rig). I'm sure it's not the most efficient way to achieve this behavior so I'm totally open for criticism of my programming skills in the comments, but here my problem: When I make I character jump, what I do is that I put his <em>dy</em> to a negative value, say -200 and I set the boolean <em>jumping</em> to true, that makes the character go up, and then I have this function called <code>checkGravity()</code> that ensure that everything that goes up has to go down, <code>checkGravity</code> also checks for the character being over platforms so I will strip it down a little for the sake of your time:</p> <pre><code>public void checkGravity() { if (this.jumping) { this.jumpSpeed += 10; if (this.jumpSpeed &gt; 0) { this.jumping = false; this.falling = true; } this.dy = this.jumpSpeed; } if (this.falling) { this.jumpSpeed += 10; if (this.jumpSpeed &gt; 200) this.jumpSpeed = 200; this.dy = this.jumpSpeed; if (this.collidesWithPlatform()) { this.falling = false; this.standing = true; this.jumping = false; this.jumpSpeed = 0; this.dy = this.jumpSpeed; } } } </code></pre> <p>So, the problem is, that this function updates the <em>dy</em> regardless of the <em>diff</em>, making the characters fly like Superman in slow machines, and I have no idea how to implement the <em>diff</em> factor so that when a character is jumping, his speed decrement in a proportional way to the game speed. Can anyone help me fix this issue? Or give me pointers on how to <strong>make a 2D Jump in J2ME the right way</strong>. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload