Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help with circle collision and rotation? - Game Physics

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! :)

like image 554
RadiantHex Avatar asked Jan 24 '10 16:01

RadiantHex


People also ask

How collision works in games?

Collision Physics in Video Games In the context of rigid body simulations, a collision happens when the shapes of two rigid bodies are intersecting, or when the distance between these shapes falls below a small tolerance.

What method do we usually use for collision detection in games?

A hitbox is an invisible shape commonly used in video games for real-time collision detection; it is a type of bounding box.

How do you do collision detection?

If both the horizontal and vertical edges overlap we have a collision. We check if the right side of the first object is greater than the left side of the second object and if the second object's right side is greater than the first object's left side; similarly for the vertical axis.


2 Answers

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

like image 197
alvincrespo Avatar answered Sep 21 '22 19:09

alvincrespo


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?

like image 45
Paul Avatar answered Sep 21 '22 19:09

Paul