Recently I did some experiments with Ruby and Qt. I thought, it would be a nice adventure to write something like a little “classical-mechanics physic engine”. Of course, the focus lies on gaining experience with Ruby and Qt, and not to get exact physical simulations of a specific body.
Yet I managed to implement following stuff:
- Objects (circles, balls) can be added, which own attributes:
- Mass
- Radius of the ball/circle
- A force, that acts on the object
- Initial position
- Initial velocity
- Elastic collision
- just the implementation of the centric elastic collision (a little minimalist, I know
; it’ll be changed soon)
- just the implementation of the centric elastic collision (a little minimalist, I know
This all should work within 3 dimensions (actually more). However, the display is 2-dimensional. So you’ll only see 2D.
Source: physics_05042010.tar.gz (published under GPL)
Requirements:
- >= Ruby 1.8.7
- >= Qt 4.6 (not sure about the exact version, may function with lower versions too)
- version matching Ruby bindings for Qt.
The code is uncommented yet, but I think not unreadable. Nomenclature should do it for now.
What bothers me is the collision detection. Now it’s a simple cross-check (checks the distance two objects) on all existing objects, which is done every move. I can imagine, that this is not the cheapest way, if it’s about computationally intensity. Additionally I cannot avoid a “leapover” of two objects (at small volumes and/or high velocities).
How “leapover” effects on bouncing balls: video (look at the smallest ball)
I also considered a more mathematical approach: If you imagine two objects as two curves through time (R -> R³), you will be able to determine the crossing points in a more analytical (in my oppionion better) way. But this case is only applicable for objects without volume. So what about the extension of such a body? The curve would now look like a tube through time. This looks again like a lot needed computing time. And the “leapover”-problem is again unsolved, isn’t it?