Example #1
0
IntersectData AABB::IntersectAABB(const AABB& other) const
{
	//The distance between the AABB's on the X, Y, and Z axis.
	//Computed twice because there are two possible valid distances, depending
	//on the location of the AABB's.
	Vector3f distances1 = other.GetMinExtents() - m_maxExtents;
	Vector3f distances2 = m_minExtents - other.GetMaxExtents();

	//The correct distances will be whichever distance is larger for that
	//particular axis.
	Vector3f distances = Vector3f(distances1.Max(distances2));

	float maxDistance = distances.Max();
	
	//If there is any distance between the two AABB's, then max distance will
	//be greather than or equal to 0. If there is distance between the two
	//AABBs, then they aren't intersecting.
	//
	//Therefore, if the AABBs are intersecting, then the distance between them
	//must be less than zero.

	//TODO: This might actually need to return the minDistance if they are 
	//intersecting.
	return IntersectData(maxDistance < 0, maxDistance);
}
Example #2
0
IntersectData Collider::Intersect(const Collider& other) const
{
    if(m_type == TYPE_SPHERE && other.GetType() == TYPE_SPHERE)
    {
        BoundingSphere* self = (BoundingSphere*)this;
        return self->IntersectBoundingSphere((BoundingSphere&)other);
    }

    std::cerr << "Error: Collisions not implemented between specified "
              << "colliders." << std::endl;
    exit(1);

    //Control should never reach this point
    return IntersectData(false, 0);
}
IntersectData BoundingSphere::IntersectBoundingSphere(const BoundingSphere& other) const
{
	//The radius is the distance from any point on the sphere to the center.
	//
	//Therefore, by adding the radius of two spheres together, the result is
	//the distance between the centers of the spheres when they are touching.
	float radiusDistance = m_radius + other.GetRadius();
	float centerDistance = (other.GetCenter() - m_center).Length();

	//Since the radiusDistance is the distance bwteen the centers of the 
	//spheres are when they're touching, you can subtract that from the
	//distance between the centers of the spheres to get the actual distance
	//between the two spheres.
	float distance = centerDistance - radiusDistance;

	//Spheres can only be intersecting if the distance between them is less
	//than 0.
	return IntersectData(distance < 0, distance);
}
Example #4
0
IntersectData Plane::IntersectSphere(const BoundingSphere& other) const
{
	//Calculating the dot product between the Plane's normal and the Sphere's 
	//center gets how far the sphere's center is along the Plane's normal.
	//
	//Adding the distance adjusts this value based on how far the Plane itself
	//is along the normal.
	//
	//The end result of this is how far the Sphere's center is from the Plane.
	//The absolute value is taken so that this result is always positive.
	float distanceFromSphereCenter = 
		(float)fabs(m_normal.Dot(other.GetCenter()) + m_distance);

	//As long as the distanceFromSphereCenter is valid and positive, then
	//the distance from the sphere can be calculated simply by subtracting
	//it's radius.
	float distanceFromSphere = distanceFromSphereCenter - other.GetRadius();

	//The only time the plane can be intersecting the sphere is if the sphere
	//has less than 0 distance from the plane. Otherwise, if there is distance
	//between the plane and sphere, then there must be a gap between the
	//plane and sphere, and they cannot be intersecting.
	return IntersectData(distanceFromSphere < 0, m_normal * distanceFromSphere);
}