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.