Tuesday, April 24, 2012

Got physics and gravity working in ball bounce demo.

I got the physics in the game working, thanks to this site: http://freespace.virgin.net/hugo.elias/models/m_snokr.htm There is a lot of theory on the web about elastic ball collisions that is just useless to use practically, thanks to that site for breaking it down to something that can be used by a programmer to actually implement a simulation.

Ball Bounce Demo with Gravity and Physics

The code is here: BallBouncePhysics4.c

I compile it with the command:
 
gcc `sdl-config --cflags --libs` c4.c -o c4

Everything was slowing down and stopping so I had to put in an extra boost to the impulse to offset that. I think it was caused by the rounding down of the float to an int when I stored the impulse. I may upgrade the delta_x and delta_y of each object to a float to see if this eliminates the need for the added value. When two balls strike each other the two points where they hit are tangent to their surface and a line is perpendicular to this tangent and goes through both their centers. In order to calculate the result you need to rotate this perpendicular axis line so that it becomes the x axis. Each ball imparts an impulse that is transfered into the other ball. Their relative masses are part of this calculation. Here is a graphical example of how to calculate the value:Elastic Collision Calculationhttp://en.wikipedia.org/wiki/Elastic_collision



No comments:

Post a Comment