We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello world!
I'm currently going through the 9 chapters of Nature of Code and I have an issue with one exercise. In the "2.4 Intro to friction forces" video, I did everything properly, understood the script and changed it to a single dropping ball with constant gravity being the only factor changing the ball PVector acceleration. Strangely, in both my and Daniel Shiffman sketch, the bouncing ball lose max height at each 2 rebound. He does not adress this issue.
This is very weird and should not happen?!? since there is nothing that should make the ball lose velocity on each 2 rebounds. My guess is that there is something I don't know about the edgeBounce() I made and how it interact with the window borders, maybe chipping away a pixel of height in the process of reverting the ball velocity when it touches the ground.
The code below work properly, but I think the fault reside somewhere in it or in how it interact with the ball.
void edgeBounce() {
if (location.x > width) {
location.x = width;
velocity.x *= -1;
}
if (location.x < 0) {
location.x = 0;
velocity.x *= -1;
}
if (location.y > height) {
location.y = height;
velocity.y *= -1;
}
if (location.y < 0) {
location.y = 0;
velocity.y *= -1;
}
Can anyone help me on this? Why is the ball losing velocity at each secound bounce? Google drive link to my sketch, you can visualise the problem yourself!
https://drive.google.com/open?id=1yrXe4HzpJoNk_WAZoUy24jcRY_TjHli4
Any help very appreciated!
Answers
there is a lot going on:
updateBall() is changing velocity: velocity.add(acceleration);
acceleration is changed in applyForce
and applyForce is called with gravity in draw()
so gravity still has big influence in your code
full sketch:
Comming back on my old post and still haven't found an answer to it. I forgot to mention that friction is only applied when mouse is pressed, so the loss heppening each 2 bouces is unrelated to friction since I'm not activating it.
I think (this is a quick guess) your problem might be a combination of a distance round-off during bounds checking and a magic number combination:
In order to prevent the ball from becoming trapped and re-bouncingoutside the boundary, you move the ball back inside a barrier before it bounces, potentially changing the distance traveled:
Thise means that, for certain initial starting points and math/acceleration combinations that tend to overshoot the boundary, the ball gets a little acceleration "boost" by being relocated up at the bottom of bounce. For a particular magic combination of gravity and mass with your particular starting point -- e.g. gravity 0.30 -- this creates a ball location outside the frame and a corresponding boost every second bounce.
If you change gravity to 0.35, then you will eventually get a boost every frame -- the ball will find a stable height and bounce there forever. Same code, slightly different values, different result, all due to how far offsets happen to make the ball travel outside the frame when it bounces.
if you disable the line
location.y = height;
then your ball will get no boost -- the offsets outside the frame are lost, and it will gradually bounce lower and lower.Eventually it will be moving slowly enough that its bounce distance won't bring it back into the frame, and it will become trapped outside the wall (which is what the location reassignment is supposed to prevent).
I can't see what you mean.
Does it occur in this code too?