Bullet Physics 3D Block Toy
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
CObject Class Reference

The game object. More...

#include <Object.h>

Inheritance diagram for CObject:
CCommon

Public Member Functions

 CObject (const eObject, const CInstanceDesc &)
 Constructor. More...
 
 ~CObject ()
 Destructor. More...
 
void ApplyImpulse (const Vector3 &, const Vector3 &)
 Apply impulse. More...
 
void AddContact (CObject *, const CContactDesc &)
 Add a contact. More...
 
void PlayCollisionSounds ()
 Play collision sounds. More...
 
void kill ()
 Kill me. More...
 
const bool IsDead () const
 Query whether dead. More...
 
const eObject GetObjectType () const
 Reader function for the object type.
 
const Vector3 GetPosition () const
 Get object type. More...
 
const Quaternion GetOrientation () const
 Get orientation. More...
 

Private Member Functions

btConvexHullShape * ComputeConvexHull ()
 Compute convex hull. More...
 
const bool ContactType (CObject *, eObject, eObject) const
 Contact type test. More...
 
const bool ContactType (CObject *, eObject) const
 Contact type test. More...
 

Private Attributes

eObject m_eObjectType = eObject::Unknown
 Object type.
 
eMesh m_eMeshType = eMesh::Unknown
 Mesh type.
 
eModel m_eModelType = eModel::Unknown
 Model type.
 
float m_fScale = 1.0f
 Scale (used for models).
 
UINT m_nShapeIndex = 0
 Shape index.
 
UINT m_nEffectIndex = 0
 Effect index.
 
Quaternion m_qOrientation
 Initial orientation.
 
bool m_bDead = false
 Is dead or not.
 
btRigidBody * m_pBody = nullptr
 Pointer to physics body.
 
std::map< CObject *, CContactDescm_mapContact [2]
 Contact lists.
 
UINT m_nContactIndex = 0
 Contact index.
 

Friends

class CRenderer
 

Additional Inherited Members

- Protected Attributes inherited from CCommon
const float m_fFloorHt = 0.5f
 Floor height.
 
const Vector3 m_vPlanePos = Vector3(0.0f, 0.0f, 100.0f)
 Plane position.
 
const Vector3 m_vPlaneSize = Vector3(600.0f, 2.0f*m_fFloorHt, 600.0f)
 Plane size.
 
const float m_fMoonRadius = 25.0f
 Moon radius.
 
const float m_fBallRadius = 2.0f
 Ball radius.
 
const Vector3 m_vBoxSize = Vector3(9.0f, 6.0f, 6.0f)
 Box size.

 
const float m_fTetrahedronSize = 16.0f
 Tetrahedron size.
 
const float m_fIcosahedronSize = 16.0f
 Icosahedron size.
 
const float m_fDodecahedronSize = 16.0f
 Dodecahedron size.
 
const float m_fTeapotSize = 8.0f
 Teapot size.
 
- Static Protected Attributes inherited from CCommon
static CRendererm_pRenderer = nullptr
 Pointer to the renderer.
 
static LBaseCamera * m_pCamera = nullptr
 Pointer to the camera.
 
static CObjectManagerm_pObjectManager = nullptr
 Pointer to object manager.
 
static btDiscreteDynamicsWorld * m_pPhysicsWorld = nullptr
 Pointer to physics engine.
 
static btDefaultCollisionConfiguration * m_pConfig = nullptr
 Bullet physics configuration.
 
static btCollisionDispatcher * m_pDispatcher = nullptr
 Bullet physics dispatcher.
 
static btBroadphaseInterface * m_pBroadphase = nullptr
 Bullet physics broadphase.
 
static btSequentialImpulseConstraintSolver * m_pSolver = nullptr
 Bullet physics constraint solver.
 
static btAlignedObjectArray< btCollisionShape * > m_btCollisionShapes
 Collision shapes.
 
static bool m_bCollisionSoundsMuted = false
 Mute collision sounds.
 

Detailed Description

CObject is the abstract representation of an object.

Constructor & Destructor Documentation

◆ CObject()

CObject::CObject ( const eObject  t,
const CInstanceDesc d 
)

Constructor. Make an instance of a geometric primitive object with a shape for collision detection and response. Add a rigid body to the physics world with user pointer pointing to this so that we can later map collisions between physics bodies to the corresponding objects.

Parameters
tObject type.
dInstance descriptor.

◆ ~CObject()

CObject::~CObject ( )

Destructor. This is mainly just cleaning up after Bullet Physics by deleting the physics body, motion state, and collision object.

Member Function Documentation

◆ AddContact()

void CObject::AddContact ( CObject pObj,
const CContactDesc d 
)

Add a contact to the current contact map. This function is called by a callback function myTickCallback(), which is called from Bullet Physics whenever a contact between two bodies is detected.

Parameters
pObjPointer to object in contact.
dContact descriptor for the contact.

◆ ApplyImpulse()

void CObject::ApplyImpulse ( const Vector3 &  force,
const Vector3 &  pos 
)

Apply an impulse through a point.

Parameters
forceDirection and magnitude of impulse.
posPoint through which the impulse is applied.

◆ ComputeConvexHull()

btConvexHullShape * CObject::ComputeConvexHull ( )
private

Compute the convex hull for a primitive of type m_eMeshType. A convex hull is a triangle mesh that fits closely to the original mesh but is convex. Bullet Physics has the ability to compute a convex hull from the vertices of a mesh and use it as a collision shape. Append a pointer to it to m_btCollisionShapes.

Returns
Pointer to the convex hull created.

◆ ContactType() [1/2]

const bool CObject::ContactType ( CObject p,
eObject  t 
) const
private

Determine whether this object and another object have the right types, ignoring the order of the type parameters.

Parameters
pPointer to an object.
tObject type.
Returns
true if this object or *p has types t.

◆ ContactType() [2/2]

const bool CObject::ContactType ( CObject p,
eObject  t0,
eObject  t1 
) const
private

Determine whether this object and another object have the right types, ignoring the order of the type parameters.

Parameters
pPointer to an object.
t0First object type.
t1Second object type.
Returns
true if this object and *p have types t0 and t1 in any order.

◆ GetOrientation()

const Quaternion CObject::GetOrientation ( ) const

Get the object orientation. If it has a physics body, then get it from the physics engine, otherwise from m_qOrientation.

Returns
The object orientation quaternion.

◆ GetPosition()

const Vector3 CObject::GetPosition ( ) const

Get position.

Get the position of the object center. If it has a physics body, then get it from the physics engine, otherwise return the zero vector.

Returns
The position of the object center.

◆ IsDead()

const bool CObject::IsDead ( ) const

Reader function for the "is dead" flag.

Returns
true if marked as being dead, that is, ready for disposal.

◆ kill()

void CObject::kill ( )

Kill an object by marking its "is dead" flag. The object will get deleted later at the appropriate time.

◆ PlayCollisionSounds()

void CObject::PlayCollisionSounds ( )

Play collision sounds, which are managed as follows. There are two contact maps, m_mapContact[0] and m_mapContact[1], which map a pointer to the object that this object is in contact with to a contact descriptor for the contact. The current contact map is m_mapContact[m_nContactIndex], where m_nContactIndex flips from zero to one each time this function is called. The other contact map m_mapContact[m_nContactIndex^1] is the contact map from the previous frame. Entries are place in the current contact map by a callback function myTickCallback() calling function AddContact() above. This function is then called at the end of the frame to play the appropriate sound when either (1) there is an entry in the current contact map for an object that does not have an entry in the previous contact map, or, (2) the entry for an object in the current contact map has more points of contact than the one in the previous contact map.