shiffman / LearningProcessing

A repo for examples from the book Learning Processing
http://learningprocessing.com
606 stars 276 forks source link

the gravity ball is shaking, Is that because of the precision of float? #219

Open ghost opened 6 years ago

ghost commented 6 years ago

At some conditions, the ball won't finally 'stopped', actually it won't, I mean I can see it still shaking at the bottom of the window. Is that because of the precision of float? I have tried debugged it, only to find that it won't shaking if the speed is around +-0.0x

ghost commented 6 years ago

float x = 100; float y = 0;

float speed = 0; float gravity = 0.1;

void setup(){ size(200, 40); }

void draw(){ background(255);

fill(0); rectMode(CENTER); ellipse(x, y, 10, 20);

y = y + speed; speed = speed + gravity;

if( y > height ){ speed = speed * -0.3;//energy of motion lose. //after collision, the reverse speed will slow.

y = height;

}

}

ghost commented 6 years ago

But in https://github.com/shiffman/LearningProcessing/tree/master/chp09_arrays/example_09_11_array_append it won't, even changed the -0.95 to -0.6.