Wall bug on physics simulation
Im creating a simulation whit gravity and a ball in a confined box. You can also control the ball. Sometimes the ball gets stuck in the wall and slowly goes through it. I will post the code. is there any way to make so that there is no way for the ball to pass the wall.
Code:
[COLOR="Red"]This is the main class[/COLOR]
import processing.core.PApplet;
public class gravity extends PApplet
{
Ball b1;
public void setup()
{
size(500,600);
background(1);
b1 = new Ball((float)0.2,0,50,50);
}
public void draw()
{
b1.speed(width, height);
fill(34,240,30);
ellipse(b1.Xget(),b1.Yget(),30,30 );
rect(5,580,500,2);
System.out.println( b1.Yget() );
}
public void keyPressed()
{
if(key == CODED)
{
if(keyCode == LEFT)
{
b1.speedX -= 0.5;
}
if(keyCode == RIGHT)
{
b1.speedX += 0.5;
}
if(keyCode == UP)
{
b1.speedY -= 0.5;
}
if(keyCode == DOWN)
{
b1.speedY += 0.5;
}
}
}
}
Code:
[COLOR="Red"]This is the Ball class[/COLOR]
import processing.core.PApplet;
public class Ball extends PApplet
{
float x;
float y;
float speedX;
float speedY;
float gravity = (float)0.03;
float friction = (float)-0.9;
public Ball(float spdX,float spdY, float xIn, float yIn)
{
speedX = spdX;
speedY = spdY;
x = xIn;
y = yIn;
}
public Ball()
{
speedX = 0;
speedY = 0;
x = 0;
y = 0;
}
public void speed(int width,int height)
{
y += speedY;
x += speedX;
if(speedY>= 6)
{
speedY = 6;
}
if(y < 590)
{
speedY += gravity;
}
else if(y>= 590)
{
speedY = speedY * friction;
}
else
speedY += gravity;
if(x >= 490 || x <= 0)
{
speedX = speedX * friction;
}
}
public float Xget()
{
return x;
}
public float Yget()
{
return y;
}
}