Hi guys,
I’m trying to make a simple platform game but I can’t solve the problem of my Ball having a different y level after a jump. When my yvelocity is high, my Ball just goes through the platform, because it’s not detected by the collision detection. I don’t know if this is related to each other, but I can’t figure it out.
I have already added an ArrayList, because in the end I want multiple platforms.
Hopefuly you can help me!
Ball b;
ArrayList <Platform> platforms;
void setup(){
size (840,840);
b = new Ball();
platforms = new ArrayList<Platform>();
platforms.add(new Platform(600, 600));
}
void draw(){
background(0);
fill(128,128,128);
rect(0,height-100,width,100);
b.display();
b.move();
for (int i=0; i<platforms.size(); i++){
Platform p = platforms.get(i);
p.display();
p.collision(b);
println(b.yvelocity);
if (p.collision(b) == true && key != 'w'){
b.gravity.y=0;
b.yvelocity.y = 0;
PVector platformspeed = b.yvelocity.copy();
platformspeed.mult(-1);
b.yvelocity.add(platformspeed);
}
if (keyPressed == true && p.collision(b) == true && key == 'w' && b.yvelocity.y>=0 && b.location.y <=p.location.y+25){
b.yvelocity.y += b.upForce.y;
b.location.add(b.yvelocity);
}
if (p.collision(b) == true && b.yvelocity.y<0){
b.yvelocity.mult(-0.3);
}
}
}
class Ball{
PVector location = new PVector (100,height-125);
PVector xvelocity = new PVector (0,0);
PVector yvelocity = new PVector(0,0);
PVector upForce = new PVector (0,-20);
PVector gravity = new PVector (0,0.7);
Ball(){
}
void display(){
fill(255);
ellipse(location.x,location.y, 25,25);
}
void move(){
location.add(yvelocity);
yvelocity.add(gravity);
if (keyPressed){
if (key == 'd'){
xvelocity.x =3;
location.add(xvelocity);
}}
if (keyPressed){
if (key == 'a'){
xvelocity.x = -3;
location.add(xvelocity);
}}
if (location.y >=height-100){
gravity.y =0;
yvelocity.y=0;
if (keyPressed == true){
if (key == 'w'){
yvelocity.y +=upForce.y;
}
}} else{
gravity.y = 0.7;
}}}
class Platform{
PVector location;
Platform (float x, float y){
location = new PVector (x,y);
}
void display(){
fill(255);
rect(location.x,location.y, 100,25);
}
boolean collision(Ball other){
if ((other.location.x+12.5 >= location.x) && (other.location.x-12.5 <= location.x +100) && (other.location.y+12.5 >= location.y) && (other.location.y <= location.y+25)){
return true;
}else {
return false;
}
}}