# sphere on sphere intersection algorithm problems

This topic is 5088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is Dimitri's ( hope i spelled your name ok) sphere Ray intersection test. i have played around with it, and tried to do a sphere sphere collision test with it. it works to an extent,but the problem is that the collission isnt detected when the bounding spheres touch but when their centers touch, which gives the impression that one charachter is overlapping the other, and no matter what i do, i cant seem to get that to change. anyone got any ideas ? also could you explain out this algorithm to me, as maby picked it up wrong and this is why i cant fix it.
public bool SphereSphereIntersection(Vector3 point, Vector3 vel, double rad, double intersectTime1, double intersectTime2)
{
bool ret = false;
double det , b, k;

k = Vector3.Dot(vel,vel);

b = -Vector3.Dot(point,vel)/k;

det = ( b*b ) -( Vector3.Dot(point,point) - (rad))/k ;

if(det <0)
{
ret = false;
}

det = Math.Sqrt((double)det);

intersectTime1 = b-det;
intersectTime2 = b+det;

if(intersectTime2 < 0)
{
ret = false;
}

if(intersectTime1 < 0)
{
intersectTime1 = 0;
ret = true;
}

return ret;
}


##### Share on other sites
You need to use r1+r2 as radius, as i have said. Otherwise it is like radius of one sphere is 0. If i interpret "the problem is that the collission isnt detected when the bounding spheres touch but when their centers touch" right, it might mean something like that....

Another things to do:
First, you can ignore intersecttime2 or rename it, i think. It's time when overlapping ends, don't sure you need it..

Second, possible coding mistake with "double intersectTime1, double intersectTime2" - i don't sure it will return any value. Don't know how it should be done in C#, but probably not so. (maybe just "double &intersectTime1", as in C++ ?).

Another thing, you might need to remove
intersectTime1 = 0;
- it is executed only if spheres overlap at initial point.
If you use backstepping hack, you need to remove that, and use negative first intersection time for backstepping. If you don't, it should never be executed if everything works right....

And finally, problem might reside elsewhere.

As about naming of parameters - "Vector3 Point" explains no more than "Vector3 P" . (and it is not a point, but vector that should be set to difference of positions). It probably will be better if you'll write some "wrapper" code that takes 2 "sphere" classes and uses this code. (that's how i did it myself for similar things)

##### Share on other sites
Quote:
 Original post by fguihenk = Vector3.Dot(vel,vel);b = -Vector3.Dot(point,vel)/k;

Would dmytry write this code? It seems fallible.

And, I stumbled across this incredible conversation about 0/0 on, of course, Usenet. :)

##### Share on other sites
Quote:
Original post by ajas95
Quote:
 Original post by fguihenk = Vector3.Dot(vel,vel);b = -Vector3.Dot(point,vel)/k;

Would dmytry write this code? It seems fallible.

And, I stumbled across this incredible conversation about 0/0 on, of course, Usenet. :)

Oh, jeez, need to test if velocity is zero [grin]. Actually i wrote this code without assuming velocity could be zero, i just modified my ray-sphere intersection....if i understand right, my code must return "undefined" in this case.

So, probably need to do something like
if(k<=verysmall)return false;
but it will return that there's no collision if spheres overlap and velocity is very small, you might need to specially test for that case.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013440
×