Box-Sphere Collision

Jan 19, 2009 at 3:18 PM

I realise this has been mentioned before but the fix that was posted seem to work for only Box-Mesh collisions.

Basically, if you have a rolling object (a sphere) on a set of boxes it will bounce up at the edge of each box even though there is no gap. This is prooving to be a bit of a problem as I am working on a marble rolling game and it makes the game quite hard when it bounces around seemingly at random on the edges.

Is there any way to fix this please? I've looked through the source myself and I think I can see where it is going wrong but don't know how to fix it. I got as far as working out that the normal worked out when you roll over an edge is pointing in an odd direction (it should really be perpendicular to the surface I think but it isn't always).

It'd would be great if somoene could help me with this.

Jan 19, 2009 at 6:00 PM
Just as a follow-up....

I have tried using a TriangleMesh instead of a Box. With the previously posted fix the problem seems to be pretty none-existant but my framerate drops from around 15fps to less than 1fps. I am using around 100 platforms, so with 100 Box's the performance is OK but is incredibly poor with 100 triangleMesh's.

Jan 20, 2009 at 6:23 PM
Hmm - I'm no expert at all, but here is my suggestions:

For each one of those TriMesh objects, make sure they aren't colliding with each other -- for each platform collision skin, add all the other platform-skins to it's "nonCollidables" list.

I think you can also add a callback function to the platform's collision skin.  Make it check any object trying to collide with the platform, and if it is also a platform (or...just not the ball?) then return false - I think that will stop the collision detection and contact points bring being generated.

I'd also check your collision system settings - if you're using a grid, check with different values to see if it speeds things up.

On my little game project I had a Box that I had placed slightly penetrating a tri-mesh.  I set it to immovable - it was colliding every frame, but couldn't fix itself at all.  My framerate went Waaaay down until I found it out. 

As an aside, I found out by using the Ants profiler trial - it's pretty awesome - a lot of the other profilers out there don't seem to want to work on XNA games.  Give it a try if none of the above works!

Jan 20, 2009 at 11:19 PM
Edited Jan 20, 2009 at 11:35 PM
Firstly, thanks for getting back to me :).

I tried what you suggested with regards to adding each mesh to the other's none-collidables and it had no effect.

I've tried all different CollisionSystems, SAP is by far the best. I experimented with grid but the performance was terrible. After a little tweaking it got better but SAP seems to work best overall.

I downloaded Ants aswell, what a neat little tool! Looking through the results it seems that the call to TestSkin in CollisionSystemSAP is the culprit of the bad performance. Drilling down further it seems that the majority of that time is spent checking against boxes (which should have been disabled because they have all been added to the none-collidables - something must be wrong).

I'll keep on looking into it, thanks

EDIT: I worked on the non-collidables and realised I had a bug. So, I have all my objects set up with non-collidables and it makes no difference with SAP but I read in another thread that there might be a bug and to try Brute so I did and it's definately improved but it's still a pretty bad framerate.

I just really wish somone could answer my prayers and sort out the SphereBox collision :)
Jan 25, 2009 at 1:15 PM
Hi Tw1ster!

Box-Sphere jumping
I will have a look at it.

mhhh... i fixed a bug in the collisionsystem SAP. I didn't considered the NonCollidable-list... maybe it works now. try to download the latest source .

btw: nice video @ youtube
Jan 25, 2009 at 5:34 PM
Thanks for that, CollisionSystemSAP is indeed fixed now, thanks. It's a lot quicker than the other ones but I still can't quite get the performance I want.

You've obviously seen my issue with box-sphere collisions on youtube? As you can see, at the seams the ball seems to bounce randomly. If that was fixed it would be an amazing help. I'm not good at physics myself so couldn't work out the problem but if you managed to fix it i'd be incredibly grateful!
Jan 26, 2009 at 9:13 AM
Just so you can see exactly what the problem is I did another video.

Thanks for JigLibX by the way, it's really been an amazing help to me :)

Jan 29, 2009 at 3:33 PM
I've been looking a little more at SphereBox as I am desperate to solve this - it's doing my head in!

The only thing I can find is that it seems to happen when this statement...

if (System.Math.Max(oldDepth, newDepth) > -collTolerance)

There is no else clause but if you put one in and whack on a breakpoint whenever the ball goes over an edge of a box the program will hit the breakpoint, so nothing is done about the collision (atleast to my understanding). I was trying to make the ball just stay at the same Y position in that case (a bit of a hack I know) but I couldn't get it working.

Lets hope i've inspired someone to have a closer look at it like me :).

Oct 29, 2009 at 6:01 PM


I'm doing a ball-platformer and this is a really big issue. Has anyone ever found a way to solve this?



Mar 7, 2010 at 8:01 PM

i found that is connected with colision direction detection:

i replaced(file:SphereBox.cs ln: ~73)

                if (oldDist < -JiggleMath.Epsilon)
                    dir = oldBoxPoint - oldSphere.Position - oldBoxPoint;
                    JiggleMath.NormalizeSafe(ref dir);
                else if (oldDist > JiggleMath.Epsilon)
                    dir = oldSphere.Position - oldBoxPoint;
                    JiggleMath.NormalizeSafe(ref dir);
                    dir = oldSphere.Position - oldBox.GetCentre();
                    JiggleMath.NormalizeSafe(ref dir);


                dir = Vector3.Up;


and now when ball is on top, everything works fine (no jumping) , this dont solve problem because collison with side of bo don't work since it's hardcodet to Vector.Up;