Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not 100% sure, but I think that by resetting the ball to the bottom line when it actually arrives a few pixels <em>below</em> the bottom line you exactly compensate the elasticity factor, so that the ball bounces on indefinitely.</p> <p>Try removing the line <code>yPosition=(displayHeight-20);</code>, then it should work better.</p> <p>This way, the ball will eventually come to a rest, but this is still not correct, as the time the ball falls through the ground is not counted at all.</p> <p><strong>Update</strong> Okay, I think now I've got it. Here are the relevant bits:</p> <pre><code>// first update position, then update velocity yPosition=yVelocity+yPosition; if (yPosition &gt;= floor) { // invert velocity and position w/ elasticity applied yVelocity = -yVelocity * elasticity; yPosition = floor - (yPosition - floor) * elasticity; } if(Math.abs(yVelocity) &lt;= 2 &amp;&amp; Math.abs(yPosition - floor) &lt;= 2){ // stop everything when close to rest yPosition=floor; yVelocity=0; } else { // otherwise accelerate, even after bounce yVelocity=yVelocity+yAccelleration; } </code></pre> <p>So the main changes w.r.t. your code are:</p> <ol> <li>first to update the position, and then the velocity</li> <li>instead of resetting the ball to the floor, reverting the amount it fell through the floor, and also applying the elasticity factor to that</li> <li>always to accelerate the ball, even when it bounces off the floor</li> </ol> <p>And here how it looks like (floor being at 500). Not perfect, but close.</p> <p><img src="https://i.stack.imgur.com/mjfBO.png" alt="enter image description here"></p> <p><strong>Update 2</strong>: Just found <a href="http://processing.org/learning/topics/bouncybubbles.html" rel="nofollow noreferrer">this Processing example</a>, which is actually very close to your original code, with the difference that they <em>always</em> apply acceleration, and that they do so <em>before</em> the collision-checks. Seems like this alone does the trick.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload