Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I originally started a thread over on Stack Overflow about this but it's diverged into mathematics which is way beyond my understanding.

Basically I have the following formula (it's JavaScript but I don't think it should really matter) that represents a bouncing object, with gravity:

Rubber: 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 who understands this kind of thing 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.

Many thanks folks - I really appreciate it!

share|improve this question
    
That while-loop makes me want to cry :P put the variable mutations in the body of the loop, for goodness' sake! –  Ben Millwood Feb 5 '13 at 15:03
    
Sorry Ben, you've lost me. I'm all up for advice on making this more elegant but my primary objective is to make it work; then it can be tidied/optimised so as not to upset folk. –  Dan Feb 5 '13 at 15:21
    
Yeah, but I mean, if I want to answer your question, I need to understand your code, and if I wanted to do that, the first thing I'd do would be to change the while loop so that there was no assignment or decrement in the while condition. Maybe that's just me. –  Ben Millwood Feb 5 '13 at 15:48
    
My instinct is that the problem is way simpler than you're making it, but I'm not sure in what context your function is executed, and what its input and output are meant to be. In general, when you bounce off a thing, you just multiply your speed-in-that-direction by a constant (say, a half, or two thirds, or some number depending on the precise properties of the collision). –  Ben Millwood Feb 5 '13 at 15:51
    
Thanks Ben. I see your reasoning completely but I'd make a real mess of this if I tried to refactor it when I don't really understand it to begin with. The wider context is that this code belongs to the standard jQuery UI library: code.jquery.com/ui/1.10.0/jquery-ui.js (search for 'Bounce: function'). A fiddle is here, showing it in action: jsfiddle.net/NtkNB. So that is the context. I hoped that there'd be a constant for restitution but there isn't - it's just this function that determines the 'bounce' properties, but having spent a long time tweaking I've drawn a blank :(. –  Dan Feb 5 '13 at 16:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.