views:

826

answers:

3

Ok so I have bunch of balls:

Ignight Balls


What I'm trying to figure out is how to make these circles:

  • Rotate based on the surfaces they are touching

  • Fix collision penetration when dealing with multiple touching objects.


EDIT: This is what I mean by rotation

alt text

  • Ball 0 will rotate anti-clockwise as it's leaning on Ball 3

  • Ball 5 will rotate clockwise as it's leaning on Ball 0


Even though solutions to this are universal, just for the record I'm using Javascript and SVG, and would prefer implementing this myself rather than using a library.

Help would be very much appreciated. Thanks! :)

+2  A: 

From a physics standpoint it sounds like you want to conserve both linear and angular momentum.

As a starting point, you'll want establish ODE matrices that model both and then perform some linear algebra to solve them. I personally would use Numpy/Scipy (probably using a sparse array) for that solution. But there are many approaches (sympy comes to mind). What modules do you want to use?

You'll want to familiarize yourself with coefficient of restitution and coefficient of friction and decide if you want to conserve kinetic energy too. (do you want/care if they keep bouncing and rolling around forever?) (you'll probably need energy matrices as well)

You'll be solving these matrices every timestep all the while checking the condition that no two ball centers are closer than the sum of the two radii. (..and if they do, you adjust the momentum and energy terms for a post-collision condition)

This is just the barest of beginnings to a big project. Can I ask why you want to do this from scratch?

bpowah
Hi bpowah. Thanks. I have already implemented part of what you mentioned. But I'm stuck with fixing obj coordinates during penetration when multiple objs are touching each other. And don't have a clue how to make objs rotate "conceptually".
RadiantHex
I'm not sure I know exactly what you mean by "fixing obj coords during penetration". Am I safe to assume that you can resolve the location of every ball at the end of a timestep while maintaining conservation of all momentum terms? If so, how is what you get different than what you want?
bpowah
He propably means that after given timestep a number of balls can interpenetrate and he has hard times resolving it. I'm not in the physics/games field for some two years now, but I remember that it's relatively easy solvable using, iirc the term, relaxation. Basically at the end of each step you push every pair apart from each other and iterate for a fixed amount of steps or until interpenertrations are smaller that some treshold. But I could have forgotten some "quirk" here.
Tomasz Zielinski
+3  A: 

I would recommend checking out game physics simulation books and articles. See O'Reilly's Physics for Game Developers and the Gamasutra website, for example.

Matt Anderson
+3  A: 

Here are a few links I think would help you out on your quest:

Box2D

Advanced Character Physics

Javascript Ball Simulation

Box2D has what your looking for, and its open source I believe. You can download the files and see how they do what they do in order to achieve your effect.

Let me know if this helps, trying to get better at answering questions on here. :)

EDIT:

So I went ahead and thought this out just a bit more to give some insight as far as how I would approach it. Take a look at the image below:

Basically, compare the angles on a grid, if the ball is falling +30 degrees compared to the ball it falls on then rotate the ball positively. If its falling -30 degrees compared to the ball it fall on then rotate the ball negatively. Im not saying this is the correct solution, but just thinking about it, this is the way I would approach the problem off the bat.

alt text

alvincrespo