Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

sphere on sphere intersection algorithm problems

Started by fguihen Jan 7, 2005 at 12:10 PM 2 replies 550+ views
Original Post
fguihen
fguihen
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;
		}
I currently only use c# and directX9, in case its relivant and i didnt mention it in the post
Dmytry
Dmytry
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 algorithm, i have explained derivations in your other thread...

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)
ajas95
ajas95
Quote:
Original post by fguihen
k = 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. :)
Dmytry
Dmytry
Quote:
Original post by ajas95
Quote:
Original post by fguihen
k = 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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.