We''ve all seen the silly demo which has a bunch of balls bouncing around in 3D space...I hope so anyway. It''s a fairly simple/common demo.
What I want is a good algorithm to do collision detection between the balls that are bouncing. Since all the balls are well...balls, I''m saying each one is a particle defined by a sphere of radius R.
When the number of balls is small, it''s fairly simple to check if any of the spheres overlap;
For each x, y, z, get a minimum (coordinate - radius) and a maximum (coordinate + radius). For every other ball, check if the spheres overlap. The order of this is shocking however. N*N*(Number of operations to see if a ball is overlapping another ball).
So I want a more efficient algorithm to achieve this. I''ve had quite a look around and the only real collision detection I can find is based on trying to do collision detection on a polymesh, using cubes or sphere (elipsoids) to approximate the polygons.
Can anyone suggest some reading on this matter?
So far the very best I''ve come up with sorting the balls into a series of trees by x, y, z and then using the binary tree to throw away any balls which are outside the target area. This can reduce the calculations in the best case situation, but on the worst case it''s more calculations and the average case is only something like 2% more efficient. When I actually implement it and use random starting positions and velocities on the balls, I get a comparable time taken for collision detection (1000 balls).
Any thoughts on the matter most welcome. I''m getting quite frustrated by things which say: "Particle Collision Detection Algorithm!" and then spent 30 pages on generating BSPTrees from polymeshs and collision caching and about 1/2 a page on actually doing collision detection.
Thanks!

# Bouncing Balls

Started by Shadow Mint, Jun 29 2001 06:13 PM

4 replies to this topic

###
#2
Members - Reputation: **2076**

Posted 29 June 2001 - 10:06 PM

An idea that comes to mind is to discretize the containing volume and test for collisions within the elements. What I mean is that you divide your volume - the cube within which the balls are bouncing - into a 3D matrix of smaller volumes. You then index through them searching for which balls have their centers encompassed by a cube element, and test those against each other. This has bizarre results for balls that should collide but are in adjacent cubes, so it''s far from perfect but should yield fair results.

If your application is object-oriented, you could even have the individual balls query the cube elements - the one they are in as well as the adjacent cubes - for the coordinates of any occupying balls, and then do collision detection against them.

You call also simplify your detection by checking the distance between the centers of your spheres rather than for overlap of their volumes. If the vector distance between two balls (centers) is less than twice the radius, there''s been a collision.

I hope this helps somewhat.

If your application is object-oriented, you could even have the individual balls query the cube elements - the one they are in as well as the adjacent cubes - for the coordinates of any occupying balls, and then do collision detection against them.

You call also simplify your detection by checking the distance between the centers of your spheres rather than for overlap of their volumes. If the vector distance between two balls (centers) is less than twice the radius, there''s been a collision.

I hope this helps somewhat.

###
#3
Members - Reputation: **1098**

Posted 30 June 2001 - 06:57 AM

Initialize the system by calculating, for each ball, the first time it will hit any other ball. Keep these times in sorted order.

Then, advance the simulation to the time of the next collision (i.e. the first thing on your list). After the collision, recalculate the new collision time for every other ball, update your list accordingly.

With this method you only have to do n comparisons every collision instead of n^2 comparisons every frame. You also get around the problem of fast balls missing collisions because your simulation time was to high and the ball "warped through" the one it was supposed to collide with.

-Mike

Then, advance the simulation to the time of the next collision (i.e. the first thing on your list). After the collision, recalculate the new collision time for every other ball, update your list accordingly.

With this method you only have to do n comparisons every collision instead of n^2 comparisons every frame. You also get around the problem of fast balls missing collisions because your simulation time was to high and the ball "warped through" the one it was supposed to collide with.

-Mike

###
#4
Members - Reputation: **351**

Posted 01 July 2001 - 10:04 PM

Another method is ''sweep and prune'':

To do this project all the balls onto each axis in turn. E.g. on the x axis this means taking the x coordinate of each ball, adding and subtracting it''s radius and noting the values obtained. Do this for every ball and sort the results. Repeat on the y ans z axes.

As the balls move keep track of these values and keep them sorted. Assuming they are not moving far each frame the sorting may not often change or at most it will require a few exchanges, done by a couple of passes of a bubble sort.

A collision can only occur between two balls when they overlap on all three axes. This greatly reduces the number of pairs that need to be considered, and it''s easy to detect new candidate pairs during sorting, e.g. when the upper bound of one object is swapped with the lower bound of another during sorting.

In general the calculations are quick and scale with the number of particles, e.g. O(n). You do not need to know anything about how the objects move (needed for the time prediction technique) or how they are distributed (needed to fix a grid size). And this works well for other shapes: just use bounding spheres or axiz-aligned bounding boxes for more general objects.

To do this project all the balls onto each axis in turn. E.g. on the x axis this means taking the x coordinate of each ball, adding and subtracting it''s radius and noting the values obtained. Do this for every ball and sort the results. Repeat on the y ans z axes.

As the balls move keep track of these values and keep them sorted. Assuming they are not moving far each frame the sorting may not often change or at most it will require a few exchanges, done by a couple of passes of a bubble sort.

A collision can only occur between two balls when they overlap on all three axes. This greatly reduces the number of pairs that need to be considered, and it''s easy to detect new candidate pairs during sorting, e.g. when the upper bound of one object is swapped with the lower bound of another during sorting.

In general the calculations are quick and scale with the number of particles, e.g. O(n). You do not need to know anything about how the objects move (needed for the time prediction technique) or how they are distributed (needed to fix a grid size). And this works well for other shapes: just use bounding spheres or axiz-aligned bounding boxes for more general objects.

###
#5
Members - Reputation: **132**

Posted 05 July 2001 - 08:53 PM

A thing i thought of whilst reading the above "sweet and prune" reply:

Firstly, check only the x-axis for all the balls, and save all overlapping balls found (radius distance checking). Next, move on to the y-axis, and check only the collisions for those balls having collided in the x-axis. Save only the ones that have 'collided' in both x-axis _and_ y-axis. Finally, check the z-axis collision for those balls that still are left. :D

Is this efficiant - or what?.

//parklife

Edited by - parklife on July 6, 2001 4:01:45 AM

Firstly, check only the x-axis for all the balls, and save all overlapping balls found (radius distance checking). Next, move on to the y-axis, and check only the collisions for those balls having collided in the x-axis. Save only the ones that have 'collided' in both x-axis _and_ y-axis. Finally, check the z-axis collision for those balls that still are left. :D

Is this efficiant - or what?.

//parklife

Edited by - parklife on July 6, 2001 4:01:45 AM