|
Basic Physics |
edfredcoder
Member #7,985
November 2006
|
Hello. I am trying to get a basic physics system working, but something doesn't work. The first thing I tried was to get a falling object, of a small mass, to fall on top of a heavy object and bounce up. The heavy object has its velocity set to 0 each frame to keep it from falling downward too. What should happen is the falling block should bounce up and down on the stationary block, eventually coming to rest. What actually happens is the falling block bounces up and down, then rests on the stationary block for a moment, then slowly sinks through it. My code is as follows:
This is not literally what my code is; for example, the entities also have information about what bitmap represents them, but this is the general gist of it. Could someone help me? I think the order of physics events might be the problem, but I'm very new to physics programming. |
gillius
Member #119
April 2000
|
I didn't examine the code, but I have a general advice that might apply here. The most common problems in physics simulations is that the math (particularly the integration) is not precise because of breaking the simulation into frames. This problem typically destroys all "stable-states" and therefore you need to code specifically for rest states. For example if you write a routine for sliding friction and objects, after getting to where they should finally stop, just studder instead, usually is an example of this case. So you need to put in checks to make sure that friction doesn't reverse the direction, and/or add things to clamp to certain values, such as 0, if the value is close enough (or you can get values like 1e-20 just from floating point error). Probably if you have an object that's supposed to rest but starts to sink, you are checking for collision but then not reversing the "bad" calculation you just made, so the gravity pulls it down for 1 frame, then you set velo to 0 because it collided, but in the next frame it starts "falling" again. Changing your order of operations and/or checking explicitly for rest states can help. An example: if bounce speed is less than 1 frame's worth of acceleration, set "resting" flag to true then stop calculating gravity until something changes. Gillius |
edfredcoder
Member #7,985
November 2006
|
I tried what you suggested, adding a lower threshold to the velocity. I also changed the order of events; velocities are modified, then collision detection occurs. This has solved my problem, mostly, except that, for the frame when the block is bouncing up, the two blocks overlap. Does anyone know of how to fix this, or maybe another (open source) physics engine I could look at to see how everything works? |
Paul Pridham
Member #250
April 2000
|
Johan Peitz adapted the "Box2D" physics example for Allegro. Perhaps it could be some help: http://www.allegro.cc/forums/thread/590241 ---- |
edfredcoder
Member #7,985
November 2006
|
Thank you! |
Johan Halmén
Member #1,550
September 2001
|
For the frame where the objects overlap, recalculate the position. Either zero the overlap by adjusting the y position, like if the falling object slows down a bit when collision, then continue. Or mirror the overlap: if in frame n the y position is 0.7 and in frame n+1 the y position is -0.4 and collision is when y position is 0.0, set the y position in frame n+1 to +0.4. This would be a perfect simulation, if we had no friction, no energy loss at collision and no gravity. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
|