Basic gravity anomaly
in
Programming Questions
•
1 year ago
I'm writing an elementary gravity simulation where a square falls to the ground and bounces with damping. However, after a few good bounces, the square seems to get 'stuck' in the floor - it does not rebound with the expected impulse. After this happens, it continues to do little mini-bounces in the floor, but without damping. Why is this happening? This code:
//Initial position
float x = 100;
float y = 0;
//Initial speed and constant acceleration
float speed = 0;
float gravity = 0.5;
void setup() {
size(200,200);
}
void draw() {
background(255);
fill(0);
noStroke();
rectMode(CENTER);
rect(x,y,10,10);
y = y + speed; //change in position
speed = speed + gravity; //acceleration
if (y > height) {
speed = speed * -0.8; //damping upon rebound
}
}
Thanks :)
//Initial position
float x = 100;
float y = 0;
//Initial speed and constant acceleration
float speed = 0;
float gravity = 0.5;
void setup() {
size(200,200);
}
void draw() {
background(255);
fill(0);
noStroke();
rectMode(CENTER);
rect(x,y,10,10);
y = y + speed; //change in position
speed = speed + gravity; //acceleration
if (y > height) {
speed = speed * -0.8; //damping upon rebound
}
}
Thanks :)
1