World of Rigid Bodies (WoRB)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
WoRB::Sphere Class Reference

Encapsulates a sphere. More...

#include <Geometry.h>

Inheritance diagram for WoRB::Sphere:
Collaboration diagram for WoRB::Sphere:

Public Member Functions

 Sphere ()
double Volume () const
 Gets the volume of the sphere.
void SetMass (double mass)
 Sets body mass and principal moment of inertia of the sphere.
bool Intersects (const HalfSpace &plane) const
 Tests for intersection of the sphere and a half-space.
bool Intersects (const Sphere &B) const
 Tests for intersection between two spheres.
unsigned Check (CollisionResolver &owner, const HalfSpace &plane) const
 Checks for collision between the sphere and a half-space.
unsigned Check (CollisionResolver &owner, const TruePlane &plane) const
 Checks for collision between the sphere and a true plane.
unsigned Check (CollisionResolver &owner, const Sphere &B) const
 Checks for collision between two spheres.

Data Fields

double Radius
 Holds the radius of the sphere.

Detailed Description

Encapsulates a sphere.

Definition at line 153 of file Geometry.h.


Constructor & Destructor Documentation

WoRB::Sphere::Sphere ( ) [inline]

Definition at line 157 of file Geometry.h.


Member Function Documentation

unsigned Sphere::Check ( CollisionResolver owner,
const HalfSpace plane 
) const

Checks for collision between the sphere and a half-space.

Definition at line 114 of file CollisionDetection.cpp.

References WoRB::Geometry::Body, WoRB::HalfSpace::Direction, WoRB::Quaternion::Dot(), WoRB::CollisionResolver::HasSpaceForMoreContacts(), WoRB::HalfSpace::Offset, WoRB::Geometry::Position(), Radius, and WoRB::CollisionResolver::RegisterNewContact().

{
    if ( ! owner.HasSpaceForMoreContacts () ) { 
        return 0; // We do not have space left for new contacts
    }

    // Get the position of the center of the sphere
    //
    Quaternion position = Position ();

    // Find the distance from the plane
    //
    double distance = plane.Direction.Dot( position ) - Radius - plane.Offset;

    if ( distance >= 0 ) {
        return 0;
    }

    // New contact: it has a normal in the plane direction.
    //
    return owner.RegisterNewContact(
        /* a */ Body,
        /* b */ 0, /* scenery */
        /* X */ position - plane.Direction * ( distance + Radius ),
        /* N */ plane.Direction,
        /* d */ -distance
    );
}
unsigned Sphere::Check ( CollisionResolver owner,
const TruePlane plane 
) const

Checks for collision between the sphere and a true plane.

Definition at line 71 of file CollisionDetection.cpp.

References WoRB::Geometry::Body, WoRB::TruePlane::Direction, WoRB::Quaternion::Dot(), WoRB::CollisionResolver::HasSpaceForMoreContacts(), WoRB::TruePlane::Offset, WoRB::Geometry::Position(), Radius, and WoRB::CollisionResolver::RegisterNewContact().

{
    if ( ! owner.HasSpaceForMoreContacts () ) { 
        return 0; // We do not have space left for new contacts
    }

    // Get the position of the center of the sphere
    //
    Quaternion position = Position ();

    // Find the distance the center of the sphere to the plane
    //
    double distance = plane.Direction.Dot( position ) - plane.Offset;

    // Check if there is an overlap
    //
    if ( distance * distance > Radius * Radius )
    {
        return 0;
    }

    // Check which side of the plane we're on
    //
    Quaternion normal = plane.Direction;
    double penetration = -distance;
    if ( distance < 0 )
    {
        normal = -normal;
        penetration = -penetration;
    }
    penetration += Radius;

    return owner.RegisterNewContact(
        /* a */ Body,
        /* b */ 0, /* scenery */
        /* X */ position - plane.Direction * distance,
        /* N */ normal,
        /* d */ penetration
    );
}
unsigned Sphere::Check ( CollisionResolver owner,
const Sphere B 
) const

Checks for collision between two spheres.

Definition at line 145 of file CollisionDetection.cpp.

References WoRB::Geometry::Body, WoRB::CollisionResolver::HasSpaceForMoreContacts(), WoRB::Quaternion::ImNorm(), WoRB::Geometry::Position(), Radius, and WoRB::CollisionResolver::RegisterNewContact().

{
    if ( ! owner.HasSpaceForMoreContacts () ) { 
        return 0; // We do not have space left for new contacts
    }

    // Get the positions of the centra of both spheres
    //
    Quaternion position_A = Position ();
    Quaternion position_B = B.Position ();

    // Find the displacement and the distance between the two spheres
    //
    Quaternion displacement = position_A - position_B;
    double distance = displacement.ImNorm ();

    // Check if the separation is large enough
    //
    if ( distance >= Radius + B.Radius) {
        return 0;
    }

    // The contact-normal is along the displacement with the position half-way
    //
    return owner.RegisterNewContact(
        /* a */ Body,
        /* b */ B.Body,
        /* X */ position_B + displacement * 0.5,
        /* N */ displacement * ( 1.0 / distance ),
        /* d */ Radius + B.Radius - distance
    );
}
bool WoRB::Sphere::Intersects ( const HalfSpace plane) const [inline]

Tests for intersection of the sphere and a half-space.

Definition at line 190 of file Geometry.h.

References WoRB::HalfSpace::Direction, WoRB::Quaternion::Dot(), WoRB::HalfSpace::Offset, WoRB::Geometry::Position(), and Radius.

        {
            // Find the distance from the origin
            double distance = plane.Direction.Dot( Position() ) - Radius;

            // Check for the intersection
            return distance <= plane.Offset;
        }
bool WoRB::Sphere::Intersects ( const Sphere B) const [inline]

Tests for intersection between two spheres.

Definition at line 201 of file Geometry.h.

References WoRB::Quaternion::ImSquaredNorm(), WoRB::Geometry::Position(), and Radius.

        {
            // Find the vector between the objects
            Quaternion displacement = Position() - B.Position();

            // See if it is large enough.
            double sumRadius = Radius + B.Radius;
            return displacement.ImSquaredNorm() < sumRadius * sumRadius;
        }
void WoRB::Sphere::SetMass ( double  mass) [inline]

Sets body mass and principal moment of inertia of the sphere.

Definition at line 176 of file Geometry.h.

References WoRB::Geometry::Body, WoRB::RigidBody::CalculateDerivedQuantities(), Radius, WoRB::RigidBody::SetMomentOfInertia(), and WoRB::RigidBody::SetupMass().

Referenced by WoRB::Ball::Ball().

        {
            Body->SetupMass( mass );

            double Ixx = (2.0/5.0) * mass * Radius * Radius;
            Body->SetMomentOfInertia( QTensor( Ixx, Ixx, Ixx ) );

            Body->CalculateDerivedQuantities( /*fromMomenta*/ false );
        }
double WoRB::Sphere::Volume ( ) const [inline]

Gets the volume of the sphere.

Definition at line 169 of file Geometry.h.

References WoRB::Const::Pi, and Radius.

        {
            return ( 4.0/3.0 * Const::Pi ) * Radius * Radius * Radius;
        }

Field Documentation


The documentation for this class was generated from the following files: