# Collision Detection and Response

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

## Recommended Posts

Can anyone tell me how to detect collision using a sphere-plane routine and respond to it? Take it easy on me because this is my first thread. Hehehe.

##### Share on other sites
This is might be helpful, lots of useful articles here at gamedev.net take a browse round the articles section.

##### Share on other sites
basically if you have your point A and you want to test whether it intersects B with radius r.
Well if (Ax-Bx)^2 + (Ay-By)^2 <= r^2
A is inside the circle with center B and radius r because of pythagoras theoreum.

##### Share on other sites
Here's the sphere/plane code from my intersection library:

// --------------------------------------------------------------------------------------bool Intersection::SpherePlane(const Sphere& sphere, const Plane& plane){	return fabsf(Distance::PointPlane(sphere.GetCenter(), plane) <= sphere.GetRadius());}// --------------------------------------------------------------------------------------bool Intersection::SpherePlane(const Sphere& sphere, const Vector3& spherevel,							   const Plane& plane, float& time, Vector3& intersection,							   bool statictest){	// Static test if requested	if (statictest && SpherePlane(sphere, plane))	{		time = 0.0f;		return true;	}		// No collision if no velocity	if (spherevel == Vector3::ZERO)		return false;		// Local variables for convenience	Vector3 normal = plane.GetNormal();	Vector3 center = sphere.GetCenter();	float radius = sphere.GetRadius();		// Distance from sphere center to plane	float distcenterplane = Distance::PointPlane(center, plane);		// Only collide with plane if approaching from front and not already intersecting	if (distcenterplane <= radius)		return false;		// Velocity vector projected onto plane normal	float projectedvel = spherevel.Dot(normal);		// Distance from sphere to plane	float distsphereplane = distcenterplane - radius;		// If the velocity vector does not reach the plane, no collision	if (-projectedvel < distsphereplane)		return false;			// The first time of intersection with the plane	time = distsphereplane / -projectedvel;		// The point on the sphere that will intersect the plane	Vector3 spherepoint = center - radius * normal;		// The intersection point	intersection = spherepoint + time * spherevel;		// Collision found	return true;}// --------------------------------------------------------------------------------------

There's some support stuff there, but it should be fairly self-explanatory. Sphere is just a class with a center and radius, and plane is a class with a normal and distance.

This function takes a sphere, a plane, and a sphere velocity and returns the first time of intersection and the intersection point.

Response depends on what you want to do - bounce, slide, etc. Sliding is more complicated, but bouncing can be done by reflecting the velocity vector. To reflect the velocity around the plane normal:

Vector3 newvel = vel - 2.0f * vel.Dot(normal) * normal;

Hope that helps.

##### Share on other sites
Oops, that was me.

• 21
• 16
• 9
• 17
• 13