# Bouncing Objects

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

For a 2D game of mine, I wish to have a function capable of making sprites 'bounce' when collision is detected, such as the ground. This object will *only* have a downward force only, STRAIGHT down. No other forces will be used, so this should simplify the algorithm a bit. Basically my title bitmap is going to come down from the top of the screen at hit a specific stopping point on the screen. At this point the title should 'bounce' slowly, each time it bounces, the height decreases a little bit. The title should bounce exactly as it would if you took a golf ball and dropped it down onto the ground; it will continue to bounce until it stops eventually. If anyone could show me the math into making this work, that would be great.

##### Share on other sites
You shouldn't need more than some very simple physics for this. Can't guarantee this will be correct, but here's a sketch:

bool Object::Update(float dt){    vel += gravityAccel * dt;    pos += vel * dt;    if (pos < ground + halfHeight)    {        pos = ground + halfHeight;        vel *= -coefficientOfRestitution;        if (fabs(vel) < threshold)            return false;    }    return true;}

dt is the timestep. Since we're only concerned with one dimension, vel and pos are scalars. gravityAccel is whatever works given the units and scale. coefficientOfRestitution is in the range [0, 1], where 0 doesn't bounce at all and 1 never stops bouncing. The function returns false once the object has come to rest, based on the value of threshold. If you're working in screenspace and y increases toward the bottom of the screen, you may have to switch some things around.

##### Share on other sites
or use sin() function that is activated when it has hit the ground... with decreasing multiplier each bounce...

y = sin(time)*multiplier;

##### Share on other sites
Quote:
 Original post by Rasmadrakor use sin() function that is activated when it has hit the ground... with decreasing multiplier each bounce...y = sin(time)*multiplier;

I don't quite understand how the above formula works.

An object has a starting point. This is the x,y coordinate to which it begins to drop. It also has an ending point, which is where the object collides. The distance between these two points must be scaled at every complete bounce in order to provide smaller bounces. The object will eventually stop bouncing once the distance has been scaled enough. The thing is, I need a good formula for this.

I have created a structure called 'Bounce' which controls the dynamics of a bouncing object. It is pretty empty for now, but I have placed it below:

    struct Bounce    {        int startx, starty;        double fallx, fally;        int ground;        DWORD ElapsedTime;        void UpdateBounce(int DeltaTime)        {            ElapsedTime += DeltaTime;            // Perform math here        }    };

--------------------
Above:
startx, starty: The starting location of the object (where it begins it's drop)
fallx, fally: This is the actual position of the object as it is being moved. When reading the new location after the move every frame, these variables are accessed.
ground: This is a value on the Y axis that represents an area for collision. The starting location is *always* above this ground value. When the object is dropped, the ground determines when the fall stops, and the object is accelerated back up.
ElapsedTime: Total time of the bouncing.
--------------------

Somehwere I need to be able to specify the speed of the animation of the bounce, not necessarily the force at which the object is dropped.

The 'UpdateBounce' function is where I perform the necessary calculations to get the object bouncing correctly. Can someone help me figure this out? I am really clueless!

Thanks!

##### Share on other sites
Not trying to push my own proposed solution here, but did you try the 'simple physics' approach? Although I may or may not have gotten the example code right, in principle this approach should give you exactly the behavior you're looking for.
Quote:
 Somehwere I need to be able to specify the speed of the animation of the bounce, not necessarily the force at which the object is dropped.
This does add some complication, but I'm guessing that with a little math you could figure out a height, gravity, and coefficient of restitution that would give you approximately the total time you're looking for. However, I'm not sure what you mean by 'speed of the animation of the bounce'. Do you mean the total time from when you drop the object to when it comes to rest?

Anyway, the physics approach is my best suggestion for getting semi-realistic behavior. But maybe some other method will prove to be better for your particular needs.

##### Share on other sites
Yes, the time from when the object begins to move to when it stops is the "total animation time". I need to be able to scale this time to speed up the bounce, or slow it down. None of the physics of the object will change, just the speed at which the physics are applied.

I still need a good formula. Can anyone help?

##### Share on other sites
Quote:
 Original post by MrDoomMasterYes, the time from when the object begins to move to when it stops is the "total animation time". I need to be able to scale this time to speed up the bounce, or slow it down. None of the physics of the object will change, just the speed at which the physics are applied.I still need a good formula. Can anyone help?

Increase/decrease the delta time for that object.

##### Share on other sites
I guess your game is not based upon time based movement. First of all, I really recomend making your game time based. Time based movement means that you calculate the deltaTime between each frame and then you simply apply vel/acc/movment to your objets according to some simple laws of physics.

This is an old discussion that should get you into it: http://www.gamedev.net/community/forums/topic.asp?topic_id=138001

Once you have time based movement, this kind of problems gets so much easier. So I recomend you to implement time based movement rather than fixing the bouce effect without timebased movement. You will gain so much from it.

Quote:
 Original post by Rasmadrakor use sin() function that is activated when it has hit the ground... with decreasing multiplier each bounce...y = sin(time)*multiplier;

:/ That would make a very unnatural bounce effect.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633672
• Total Posts
3013272
×