MB-86

i have been making a pool game and now i need to add more balls, i need to put more balls onto the table first then what would be the collision code for all the balls if they were in an array

Re: XNA Framework XNA multiple bounding spheres

Arek Bal

Since, (I guess) it's normal pool game: "Yes, you should check all the balls for intersecting with each other, and table "walls"...".




Re: XNA Framework XNA multiple bounding spheres

MB-86

how would you do this in a array



Re: XNA Framework XNA multiple bounding spheres

psychogeek

Whether the balls are in an array or not does not matter. The concept here is that you check each ball against the other balls using a simple bounding-sphere collision formula. There are many articles on the web that explain bounding-sphere collisions.

Since it's a pool game, that's the least of your worries. You also have to factor in drag, spin, speed/direction vectors etc....but I guess this is a start. You may find this book useful:

http://www.amazon.com/Mathematics-Physics-Programmers-Game-Development/dp/1584503300/ref=pd_ys_qtk_saved-cart_img/105-3955511-2442045

It has a section dedicated to pool mathematics along with detailed collision-detection chapters. The code is not in C# but that should not matter...it's the explanations and formulas that count.

Hope this helps




Re: XNA Framework XNA multiple bounding spheres

Aranda

This is can be handled in a simple manner, in fact I have recently done something very similar. I assume you have a Ball class that has a BoundingSphere.

int iMaxBalls = 10;

Ball[] balls = new Ball[iMaxBalls];

for (int i = 0; i < iMaxBalls; i++)

{

for (int j = i + 1; j < iMaxBalls; j++)

{

if (ballsIdea.BoundingSphere.Intersects(balls[j].BoundingSphere)

{

// Enter your collision code here.

}

}

}

Notice that the j loop starts at i + 1. This ensures that each ball is tested against all other balls (not itself) and only once. Also bear in mind that this does not take into account the case where a ball could collide with more than one other ball in a single frame. To do that, you would need to compute all ball collisions and afterwards handle the effect of those collisions. Also, when handling the collisions, remember that Newton is your friend - Momentum is conserved. That is, Sum(Mass * Velocity) before collisions = Sum(Mass * Velocity) after collisions. Good luck.