Search code examples
processingphysics

Making an object jump once with realistic gravity


I want my object (a rectangle at this point) to jump once, to lose speed on the way up. When it's almost stopped, I want it to turn back and gain speed on the way down. When it hits the ground again, it should stop. When I press the Key again it should again do the same.


The code I tried to write implement a variable float gravity = 0.75; and a variable that keeps track of the speed float speed = 10;


while the speed is greater than 1, the speed should be subtracted by the Y-coordinate of the rectangle and then the speed should get lower since I multiply it with gravity which is less than 1

else if (keyCode == UP|| key == 'w') {
    while (speed >1 ) { 
      playerYPosition = playerYPosition-speed;
      speed = speed * gravity;
    }

Gravity gets higher than 1 but negative, so that the number I subtract adds up in final and the rectangle gets lower. To gain speed, the speed gets multiplied with gravity.

    gravity = -1.25;
    speed = speed * gravity;
    playerYPosition = playerYPosition-speed;

The speed should now be around -1.2..., so it's smaller than -1 and this while(...)should work. Again it should gain speed over time till it reaches the starting speed, just negative and with that the starting point.

while (speed < -1 && speed > -10) {
      playerYPosition = playerYPosition-speed;
      speed = speed * gravity;

Then the gravity should again go to 0.75 and the speed to 10.

gravity = 0.75;
speed = 10;

So instead of doing that, the rectangle just constantly jumps up (I think) 10 Pixels, nothing more.

Here's the whole codeblock, to reread:

float gravity = 0.75;
float speed = 10;

else if (keyCode == UP|| key == 'w') {
    while (speed >1 ) { 
      playerYPosition = playerYPosition-speed;
      speed = speed * gravity;
    }
    gravity = -1.25;
    speed = speed * gravity;
    playerYPosition = playerYPosition-speed;
    
    while (speed < -1 && speed > -10) {
      playerYPosition = playerYPosition-speed;
      speed = speed * gravity;
    }
    gravity = 0.75;
    speed = 10;

Solution

  • If you use a while loop to calculate your Y position you won't be able to visualize your jump simulation since all the maths will be computed in a single frame. To do that physic-based jump simulation you will need to have a variable representing the ground ( which is your initial Y variable ), also, once your player click, you will need to give the speed variable your full speed then each frame decrease it by the amount of your gravity while checking every frame if you haven't reached the ground yet. Here is an example I wrote to demonstrate that, I tried keeping your variable names:

    final float gravity = 0.5; 
    final float jmp_speed = 10; //the speed which the square is going to jump
    float speed = 0; //the actual speed every frame
    final float ystart = 280; //the initial Y position ( which also represent the Y of the ground )
    float playerYPosition = ystart; //give the player the Y of the ground on start
    
    void setup(){
      size(300,300);
    }
    
    void keyPressed() {
        //also check that the player is on ground before jumping
       if( (keyCode == UP || key == 'w') && (playerYPosition == ystart) ) 
        {
          speed=-jmp_speed; //when the player press jump and he is on ground we give speed the max value
        }
    }
    
    void draw(){
    
        background(150);
        rect(150,playerYPosition,10,10);
    
        //this code will be executed every frame
        //check if the player Y position won't hit the ground if we increment it by speed
          if(playerYPosition+speed < ystart)
          {
            playerYPosition+=speed;
            speed+=gravity; //increment speed by gravity ( will make speed value go from negative to positive slowly)
          }
          else
          {
            //if the player is gonna hit the ground the next frame
    
            playerYPosition = ystart; // put back the player on ground
            speed=0; // make the speed 0
          }  
    }