# Increase restitution (bounciness) of an object in a JavaScript formula

I'm going slightly around the houses on this one. I originally started a thread over on Stack Overflow about it but then it seemed to diverge into mathematics, with no joy from the SO community over there (https://math.stackexchange.com/questions/295485/modify-formula-for-bouncing-object-to-increase-restitution). So my final attempt before I admit complete failure is to ask here. I don't know if you guys do JavaScript, nor how similar this may be to any traditional gaming language, but it's worth asking.

Basically I have the following JavaScript formula representing a bouncing object with gravity, from the standard jQuery UI library: http://code.jquery.com/ui/1.10.0/jquery-ui.js (search for 'Bounce: function'). A demo here shows it in action: http://jsfiddle.net/NtkNB.

Bounce: function ( p ) {
var pow2,
bounce = 4;

while ( p < ( ( pow2 = Math.pow( 2, --bounce ) ) - 1 ) / 11 ) {}
return 1 / Math.pow( 4, 3 - bounce ) - 7.5625 * Math.pow( ( pow2 * 3 - 2 ) / 22 - p, 2 );
}


It works nicely but I need to increase the 'bounciness' of the object. I believe this is called 'restitution'.

I've played around with the numbers but it's admittedly just trial and error (mostly error!) and just can't get it to work as desired. I wondered if someone here might be able to understand this and could say whether it's actually possible to increase the restitution of the object within the bounds of this equation, or if not, how it could be modified such.

I totally appreciate any help anyone can give on this one as I've sunk so many hours into this without any joy whatsoever.

Many thanks.

• Is there any reason you're using something like this instead of Box2D if you're making a game? – Vaughan Hilts Feb 6 '13 at 19:32
• This is for an interactive banner on a website. So not really a game, but game-like motion. I basically have a few HTML div tags that need to bounce into view in a banner. That's all it needs to do, and it does it already but the client wants "more bounce"! – Briquette Feb 6 '13 at 20:00
• Gamedev is not somewhere you go to get answers when StackOverflow isn't helping. – Jimmy Feb 6 '13 at 21:00

## 1 Answer

Instead of drowning in maths, you could simulate physics.

For every object every step:

apply gravity  (add constant to velocity)
apply velocity (add velocity to position)
if position > some limit then
reverse velocity
multiply velocity by "bounciness" between 0 and 1
if velocity < 0.5 then
set velocity and gravity to 0
endif
endif


Here's a comparison of bounciness values 0.7, 0.8 and 0.9:

(Apologies for the low framerate. GIFs are complicated.)