|
World of Rigid Bodies (WoRB)
|
Encapsulates a sphere. More...
#include <Geometry.h>


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. | |
Encapsulates a sphere.
Definition at line 153 of file Geometry.h.
| WoRB::Sphere::Sphere | ( | ) | [inline] |
Definition at line 157 of file Geometry.h.
: Geometry( Geometry::_Sphere ) , Radius( 0 ) { }
| 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.
| 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.
| 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.
| double WoRB::Sphere::Radius |
Holds the radius of the sphere.
Definition at line 165 of file Geometry.h.
Referenced by WoRB::Ball::Ball(), Check(), WoRB::Cuboid::Check(), WoRB_TestBed::Dump(), Intersects(), WoRB::Ball::Render(), WoRB::Ball::RenderWireframe(), SetMass(), and Volume().