Jump in Processing?

Hello, I know that processing is not very suitable for this purpose but I want to make a small pixel game with it.

I need the character to jump a certain height in a certain time.

I’ve already written some code.

main:

Player p=new Player();


void setup(){
  frameRate(60);
  //fullScreen(P2D);

  p.transform(4,9,1.5,1);
  p.groundY=height;
  p.jumpSettings(2f,1f);
}

void draw(){
  background(60);
  p.display();
}

void touchStarted(){
  p.jump();
}

Player class:

class Player{/*
  private ArrayList<Animation> anim=
  new ArrayList<Animation>();
  private int currentAnim=0;*/
  private boolean onGround=false;
  
  
  public float x,y,w,h;
  public float tileSize=60;
  public float groundY=720,skyY=0;
  public float maxSpeed,speed=0,a=0.1,
  jumpTime=0,jumpHeight=0;
  public float jumpDuration=0.5;
  float ly; //just for testing
  
  
  public void transform(float xpos,float ypos,
  float wsize,float hsize){
    x=xpos*tileSize; y=ypos*tileSize;
    w=wsize*tileSize; h=hsize*tileSize;
  }
  
  public void jumpSettings(float jh,float jd){
    jumpDuration=jd;
    jumpHeight=jh*tileSize;
    println("    Huh? "+jumpHeight);
    maxSpeed=jumpHeight*(4f/jd);
    a=pow(maxSpeed,2f)/2f/jumpHeight;
  }
  
  
  
  
  
  
  public void display(){
    stroke(150,60,50);
    strokeWeight(5);
    fill(255,110,100);
    rect(x,y,w,h);
    physics();
  }
  
  private void physics(){
    ly=speed;
    speed+=(float)a*(1f/frameRate);
    if(ly<0 & speed>0)
    println("jump height: "+(height-(y+h)));
    y+=(float)speed*(1f/frameRate);
    jumpTime+=1f/frameRate;
    
    if(y+h>groundY){
      y=groundY-h;
      if(!onGround)
      println("jump time: "+
      int(jumpTime*100f)/100f);
      onGround=true;
      jumpTime=0;
    }
    else{
      onGround=false;
    }
  }
  
  public void jump(){
    if(onGround){
      speed=-maxSpeed;
      onGround=false;
      jumpTime=0;
    }
  }
  
}

But this works weird for some reason. The jump time is about the same as I specified but the jump height is always lower.

Sorry I tried to find the right solution for a long time but I can’t.
I will be glad if you can help!
Thanks in advance.

1 Like

Maybe everything is correct and he is just immediately after the jump falling the first time.

Hence the number is smaller?

You can comment out the falling part in the code and check

“Jump time” is actually the time that the character was in the air (did not touch the ground) as the character appears above the ground the first time when loading the sketch “jump time” shows a smaller number.
But the problem is not in this problem in the height of the jump. For some reason it is always less than I specified. Although the calculations seem to be correct.

h - jump height
V - maximum speed (initial speed)
a - acceleration

h = V² ÷ 2 ÷ a(i found this formula on a website)
h × a = V² ÷ 2
a = V² ÷ 2 ÷ h

I also noticed that the longer the duration of the jump, the closer the jump height value is to the one I specified.

My argument is still valid

I am on a journey and can’t help you…

1 Like

I was able to find a solution, but it took a long time.

Player class:

class Player{
  private ArrayList<Animation> anim=
  new ArrayList<Animation>();
  private int currentAnim=0;
  private boolean onGround=false;
  private float last_millis=0;
  
  
  public float x,y0,y1,w,h;
  public float tileSize=60;
  public float groundY=720,skyY=0;
  public float jumpSpeed,v0,v1=0,a=0.1,
  jumpTime=0,jumpHeight=0;
  public float jumpDuration=0.5;
  float ls;
  
  
  public void transform(float xpos,float ypos,
  float wsize,float hsize){
    x=xpos*tileSize; y0=ypos*tileSize;
    w=wsize*tileSize; h=hsize*tileSize;
  }
  
  public void jumpSettings(float jh,float jd){
    jumpDuration=jd;
    jumpHeight=jh*tileSize;
    println("    Huh? "+jumpHeight);
    jumpSpeed=jumpHeight*4f*(1f/jd);
    
    //a = V0² ÷ 2 ÷ S
    a=pow(jumpSpeed,2f)/2f/jumpHeight;
  }
  
  
  
  public void display(){
    stroke(150,60,50);
    strokeWeight(5);
    fill(255,110,100);
    rect(x,y1,w,h);
    physics();
  }
  
  private void physics(){
    ls=v1;
    
    /*
    calculate the speed using the formula:
    V1 = V0 + a × t
    */
    v1=(float)v0+a*jumpTime;
    if(ls<0 & v1>=0)
    println("jump height: "+(height-(y1+h)));
    
    /*
    calculate the traveled distance by the formula:
    S = (V1² - V0²) ÷ 2a
    y1 = y0 + S
    */
    y1=y0+(float)(pow(v1,2)-pow(v0,2))/(a*2f);
    if(y1+h>groundY){
      y1=groundY-h;
      v1=0;
      v0=0;
      y0=y1;
      if(!onGround)
      println("jump time: "+jumpTime);
      jumpTime=0;
      onGround=true;
    }
    
    jumpTime+=(float)(millis()-last_millis)/1000f;
    last_millis=millis();
  }
  
  
  public void jump(){
    if(onGround){
      v1=0;
      v0=-jumpSpeed;
      onGround=false;
      jumpTime=0;
    }
  }
  
}
1 Like