2

gravity: (x=0, y=EARTH)

I want make permanent bouncing ball. For example I set it coords (200, 200), after it falls down to ground, bounces and return to the same coords as in begin (200, 200). I tried to play with density, elasticity, friction and I got some similar behavior, but is changing. I google a lot and found that the problem is because of rounding floats. Question: which value is changing in engine and which I need to reset after that value will become more then some delta?

Second question: why if I set fixtures like this: fixtures(density=1, elasticity=1, friction=0) it not bounced to the same height, but bouncing with every bounce higher? I set elasticity something like 0,981f

Third question: how one object fixtures depends on second object fixtures? example: ball(1f, 0.6f, 1f) wall(1f, 0.6f, 0f)

and

ball(1f, 0.6f, 1f) wall(1f, 0.6f, 0.5f)

what will change?

Aleksandrs
  • 1,488
  • 1
  • 14
  • 34
  • Do you really need precisely the same coordinates every time or just roughly constant kinetic + potential energy of the ball? – JohnEye Oct 09 '12 at 12:00
  • I need that ball return to the same height (+/- 15 px can be fault) look here: [Example](http://i49.tinypic.com/j9blat.png) – Aleksandrs Oct 09 '12 at 12:21
  • 1
    Then these two questions might be useful: http://stackoverflow.com/questions/11257197/how-do-i-get-a-body-to-bounce-around-the-screen-when-there-is-gravity-box2d-and http://stackoverflow.com/questions/11257462/how-do-i-apply-a-force-to-a-body-in-the-direction-it-is-traveling-box2d – JohnEye Oct 09 '12 at 12:34
  • 1
    If I were you, I would give the body elasticity larger than 1 and monitor its velocity. If the body's velocity gets too large, the monitor would slow it down. This would keep the system in balance - the body would keep gaining a bit of energy and the excess would be trimmed off. – JohnEye Oct 09 '12 at 12:40
  • I got something similar what I needed, but now I can't calculate destroyerVelocity (this is variable from that example). But I need to calculate it depending on sprite yPos. Do you understand? I have velocity only to side (x coord) - body.applyLinearImpulse(((float)3),(float) 0, body.getWorldCenter().x, body.getWorldCenter().y); y - depends on gravity; For example: I hardcode destroyerVelocity = 9.0f after this, ball is going up or down (depends where I put sprite) goes to particular y, 180px(for example) – Aleksandrs Oct 09 '12 at 19:38
  • No, I don't really understand what you are trying to achieve. Please update the question with a more detailed description of the game mechanism that you're creating. – JohnEye Oct 10 '12 at 08:41
  • I use this post http://stackoverflow.com/questions/11257462/how-do-i-apply-a-force-to-a-body-in-the-direction-it-is-traveling-box2d . There is variable destroyerVelocity. I hardcoded it (for example =9.0f). What I [have now](http://i46.tinypic.com/11kweaw.png) but this I [wanted to get](http://i48.tinypic.com/2nvyu69.png). – Aleksandrs Oct 10 '12 at 10:21
  • 1
    I see. Perhaps you could measure the velocity just before the ball touches the ground for the first time and then set it as maximum velocity instead of the hardcoded value. Or create a table of values for different heights and figure out an appropriate function that would give you the right value for every height. Depending on the game mechanism, lateral velocity may come into play though. – JohnEye Oct 10 '12 at 10:34
  • @JohnEye "measure the velocity just before the ball touches" - it was quite easy: `if (prevVelocityY * body.getLinearVelocity().y < 0 && prevVelocityY > 0) { if (needMeasure) { defVelocity = prevVelocityY; needMeasure = false; } body.setLinearVelocity(body.getLinearVelocity().x, defVelocity * (-1.0f)); } prevVelocityY = body.getLinearVelocity().y;` thank you very much, for your help and ideas – Aleksandrs Oct 13 '12 at 05:21

1 Answers1

0

hmmm, to make a permanent bouncing ball, Make your body BodyType.DynamicBody one, then just set the gravity to 0,EARTH (9.8f) and

scene.registerUpdateHandler(new IUpdateHandler() {


    @Override
    public void onUpdate(float pSecondsElapsed) {

            if(body.getPosition().y >=CAMERA_HEIGHT)
            {
                Vector2 v=new Vector2(0, -9f); //Adjust according to the bounce required
                body.setLinearVelocity(v);
            }

    }
});

The second one :

density is the volumetric mass density (),, and gravitational acceleration is independent of mass...

elasticity is the inertia when the object is stopped at a point, so if you don't want to move the body further just set it to 0

friction is the amount of the force resisting the relative motion of the body (like medium air, water or things like that)

So, to create a bouncing ball between two fixed points try setting (density=1, elasticity=0, friction=0)

Beep.exe
  • 1,368
  • 12
  • 21