Advanced Sphere-Sphere Continuous Collision Detection (CCD)

Vector between sphere centers and relative speed, and sum of radiuses
vec{s} = s1.pos - s2.pos
vec{v} = s1.vel - s2.vel
r = s1.radius + s2.radius

If c is negative, they already overlap.
c = vec{s}.dot(vec{s})

a = vec{v}.dot(vec{v})

If b is 0.0 or positive, they are not moving towards eachother.
b = vec{v}.dot(vec{s})

If d is negative, no real roots, and therefore no collisions
d = b*b - a*c

If we’ve come so far, we can calculate when we collide
t = ( -b - sqrt{d}) / a

Code

	bool testMovingSphereSphere(Scenenode *A, Scenenode *B, double &t)
	{
		Planet *pa = (Planet *) A;
		Planet *pb = (Planet *) B;

		Vector3D<double> s = pa-&gt;pos - pb-&gt;pos; // vector between the centers of each sphere
		Vector3D<double> v = pa-&gt;vel - pb-&gt;vel; // relative velocity between spheres
		double r = pa-&gt;radius + pb-&gt;radius;

		double c = s.dot(s) - r*r; // if negative, they overlap
		if (c &lt; 0.0) // if true, they already overlap
		{
			t = .0;
			return true;
		}

		float a = v.dot(v);
		//if (a &lt; EPSILON)
		//	return false; // does not move towards each other

		float b = v.dot(s);
		if (b &gt;= 0.0)
			return false; // does not move towards each other

		float d = b*b - a*c;
		if (d &lt; 0.0)
			return false; // no real roots ... no collision

		t = (-b - sqrt(d)) / a;

		return true;

	}

Popularity: 5% [?]

2 Comments

  1. Tatyana says

    vector v = s1.vel-s2-vel?

  2. o says

    Ja, jeg “bommet” litt :)

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)