# How to create simple physics for a group of balloons colliding in the screen (2D)

The game is 2D, how can I make simple physics for a group of balloons colliding in the screen.

What I need is the balloons not to overlap and to bounce when they reach the limits of the screen or other balloons.

Should I use something like box2d or is there a simple algorithm to do something like this?

There are simple algorithms to do this, but they are probably better implemented in Box2D than you could yourself. In particular:

• Box2D will let you have differently-sized and differently-dense (and so differently-massed) balloons, as well as different surface friction, and simulate them correctly.
• Box2D will give you realistic behavior if you want to simulate e.g. an object heavier than air falling onto the balloons.
• Box2D uses spatial subdivision, reducing the number of checks you need to do.
• Box2D will correctly simulate a balloon "pushing up" between two others, which is going to be tricky to get good and stable if you hack up your own penetration-and-correction test.

It sounds like this might be best approximated by circle-circle collision.

Store the balloons in terms of a center point and a radius (of a circle which closely matches the balloon art). Given this, collision checking is simple! Remember your pythagorean theorem. If one balloon is at (x1,y1) with radius r1 and the other is at (x2, y2) with radius r2, then:

if((x2-x1)^2 + (y2-y1)^2 < (r1+r2)^2) {
// collision
}


So basically, the distance between them is sqrt((x2-x1)^2 + (y2-y1)^2) and the collision distance is r1+r2. Since sqrt is an expensive operation, square both of those to get rid of the square root, and you're left with the code I wrote above.

• This is at best 1/3 of an answer. It does not cover how to handle large numbers of balloons via a quadtree or BSP, nor does it cover how to handle the collision stably and realistically. – user744 Sep 23 '10 at 13:51
• Wow, that's a bit harsh. A 2D radius check is a very good starting point, and the need to handle large numbers of balloons was not specified. Whilst a perfectly stable/correct implementation is non-trivial, just pushing colliding circles away from each other may give satisfactory results. – bluescrn Sep 23 '10 at 16:30
• Ricket's answer does not even explain how to push circles away from each other. It's just an overlap check. Saying 1/3 of an answer is 1/3 of an answer is harsh? I guess next time I'll just downvote bad answers silently. – user744 Sep 23 '10 at 17:14
• Yes, this solves at best 1/3 of the problem. Collision detection is the easy part, but what to do when the balloons collide? If I just move the 2 balloons away I could be just causing more collissions. It's not that easy. – Damian Sep 23 '10 at 21:13