Wraith  0.1.5
Basic 3D game engine in C++
Collisions

For collision of GameObject entity tested every frame. NOTE: GameObject Entity must be registered in order to process collision callback. More...

Collaboration diagram for Collisions:

Enumerations

enum class  Collidable::VolumeType { BSPHERE , AABB , OBB }
 Values that represent volume types. More...
 
enum class  Collidable::VolumeHierarchyType { OCTREE }
 Values that represent volume hierarchy htypes. More...
 

Functions

virtual void Collidable::terrainCollision ()
 Terrain collision callback for this object. More...
 
void Collidable::setColliderModel (Model *pColliderModel, VolumeType volumeType)
 Sets collider model and Collision Volume type. More...
 
void Collidable::setColliderModel (Model *pColliderModel, VolumeHierarchyType volumeHierarchyType, int maxDepth)
 Sets collider model and Collision Volume Hierarchy type. More...
 
void Collidable::updateCollisionData (const Matrix &world)
 Updates the collision data described by world matrix. More...
 
template<class UserClass >
void Collidable::setCollidableGroup ()
 Sets collidable group. More...
 
void Collidable::submitCollisionRegistration ()
 Submit collision registration to current scene. More...
 
void Collidable::submitCollisionDeregistration ()
 Submit collision deregistration. More...
 
bool Collidable::isRegisteredForCollisions () const
 Query if this object is registered for collisions. More...
 

Detailed Description

For collision of GameObject entity tested every frame. NOTE: GameObject Entity must be registered in order to process collision callback.

Collisions are a way for game objects to interact with each other on a virtual physical level.

You set up the colliders by passing in the collider model (which is just an ordinary model) and calling

- GameObject::setColliderModel(pColliderModel, VolumeType::BSphere) for BSphere volume
- GameObject::setColliderModel(pColliderModel, VolumeType::OBB) for OBB volume
- GameObject::setColliderModel(pColliderModel, VolumeType::AABB) for AABB volume
- GameObject::setColliderModel(pColliderModel, VolumeHierarchyType::OCTREE, maxDept) for Octree (must pass in maximum depth that is greater than or equal to 1)
void setColliderModel(Model *pColliderModel, VolumeType volumeType)
Sets collider model and Collision Volume type.
Definition: Collidable.cpp:37

Here is an example of setting up collision callbacks of tank, bullet, warehouse, wind mill, and tree game object. As well as how an enitity would interact with Terrain collisions.

NOTE: This is simply to set the callbacks for collisions and not to set up the collision tests. NOTE: The use of an octree uses the VolumeHiearchyType::OCTREE instead of VolumeType (only for BSphsere, AABB, and OBB)

That must be done within a user derived Scene entity in the Scene::initialize().

// -----------------------------------------------------------------------------------
// LevelOne Scene Set Up
// -----------------------------------------------------------------------------------
LevelOneScene::initialize() // Example of a user derived Scene Entity
{
setCollisionPair<Tank, Bullet>();
setCollisionSelf<Bullet>();
setCollisionPair<Bullet, WindMill>();
Tank* pTank = new Tank();
Bullet* pBullet = new Bullet();
WindMill* pWindMill = new WindMill();
}
// -----------------------------------------------------------------------------------
// Tank Game Object Set Up
// -----------------------------------------------------------------------------------
// IMPORTANT: Used for snapping tank to terrain
#include "SceneManager.h"
#include "Scene.h"
#include "Terrain.h"
// USES OBB for collision detection
Tank::Tank()
{
// IMPORTANT: Set collidable group here using the class type of the object.
// As well as setting the collider model
// and Volume Type (refer to Collidable::VolumeType)
GameObject::setCollidableGroup<Tank>();
GameObject::setColliderModel(_pTankModel, VolumeType::OBB);
// Submit Registration
}
void Tank::update()
{
// IMPORTANT: For objects with changing world matrix (translating, rotating, scaling)
// A call to update collider data must be made
this->move(); // call to move changes position thus changes world matrix
}
void Tank::collision(Bullet* pBullet) // Bullet collision callback
{
this->applyDamage(pBullet->getDamageValue());
}
void Tank::terrainCollision() // Terrain collision
{
// NOTE: returns a const Terrain* not just a Terrain*.
const Terrain* pTerrain = SceneManager::GetCurrentScene()->getTerrain();
// Setting position close the terrain
Vect terrainPosition = pTerrain->computePointOnTerrainObject();
this->setPosition(terrainPosition);
// Setting orientation based on terrain normal
// (basically using the normal to determine how to rotate tank
// to make it appear tilt as it moves on the terrain)
Vect terrainNormal = pTerrain->computePointOnTerrainObject();
this->setOrientation(terrainPosition);
}
// -----------------------------------------------------------------------------------
// Bullet Game Object Set Up
// -----------------------------------------------------------------------------------
// USES BSphere for collision detection
Bullet::Bullet()
{
// IMPORTANT: Set collidable group here using the class type of the object.
// As well as setting the collider model
// and Volume Type (refer to Collidable::VolumeType)
GameObject::setCollidableGroup<Bullet>();
GameObject::setColliderModel(_pBulletModel, VolumeType::BSPHERE);
// Submit Registration
}
void Bullet::update()
{
// IMPORTANT: For objects with changing world matrix (translating, rotating, scaling)
// A call to update collider data must be made
this->move(); // call to move changes position thus changes world matrix
}
void Bullet::collision(Tank* pTank) // Tank collision callback
{
this->destroy();
}
void Bullet::collision(Bullet* pBullet) // Bullet collision callback
{
this->destroy();
}
void Bullet::collision(WindMill* pWindMill) // WindMill collision callback
{
this->destroy();
}
void Bullet::terrainCollision() // Terrain collision
{
this->destroy();
}
// -----------------------------------------------------------------------------------
// Warehouse Game Object Set Up
// -----------------------------------------------------------------------------------
// USES AABB for collision detection
Warehouse::Warehouse()
{
// IMPORTANT: Set collidable group here using the class type of the object.
// As well as setting the collider model
// and Volume Type (refer to Collidable::VolumeType)
GameObject::setCollidableGroup<Warehouse>();
GameObject::setColliderModel(_pWarehouseModel, VolumeType::AABB);
// IMPORTANT: Since this game object is stationary (meaning world matrix will not change)
// You may update collision data once here
// Submit Registration
}
void Warehouse::collision(Bullet*) // Bullet collision callback
{
// Does nothing
}
void submitCollisionRegistration()
Submit collision registration to current scene.
Definition: Collidable.cpp:80
void updateCollisionData(const Matrix &world)
Updates the collision data described by world matrix.
Definition: Collidable.cpp:71
static Scene * GetCurrentScene()
Gets the current scene.
Definition: SceneManager.h:130
const Terrain * getTerrain() const
Gets the current terrain from the scene.
Definition: Scene.cpp:83
void submitUpdateRegistration()
Submit update registration to current scene.
Definition: Updatable.cpp:24

Using Collision Volume Octree has some slight difference such as using a difference enum type VolumeHierarchyType::OCTREE and GameObject::setColliderModel() taking an extra paremeter max depth. IMPORTANT: The depth value must be greater than or equal to 1. Anything less will result in a error.

// -----------------------------------------------------------------------------------
// WindMill Game Object Set Up
// -----------------------------------------------------------------------------------
// USES OCTREE for collision detection
WindMill::WindMill()
{
// IMPORTANT: Set collidable group here using the class type of the object.
// As well as setting the collider model
// and Volume HiearchyType (refer to Collidable::VolumeHierarchyType)
GameObject::setCollidableGroup<WindMill>();
// IMPORTANT: Octree also takes in maximum depth (depth value must be equal to or greater than 1)
GameObject::setColliderModel(_pWindMillModel, VolumeHierarchyType::OCTREE, 4);
// IMPORTANT: Since this game object is stationary (meaning world matrix will not change)
// You may update collision data once here
// Submit Registration
}
void WindMill::collision(Bullet*) // Bullet collision callback
{
// Does nothing
}
// -----------------------------------------------------------------------------------
// Tree Game Object Set Up
// -----------------------------------------------------------------------------------
// USES OCTREE for collision detection
Tree::Tree()
{
// IMPORTANT: Set collidable group here using the class type of the object.
// As well as setting the collider model
// and Volume HiearchyType (refer to Collidable::VolumeHierarchyType)
GameObject::setCollidableGroup<Tree>();
// IMPORTANT: Octree also takes in maximum depth (depth value must be equal to or greater than 1)
GameObject::setColliderModel(_pTreeModel, VolumeHierarchyType::OCTREE, 5);
// IMPORTANT: Since this game object is stationary (meaning world matrix will not change)
// You may update collision data once here
// Submit Registration
}
void Tree::collision(Bullet*) // Bullet collision callback
{
// Does nothing
}

Enumeration Type Documentation

◆ VolumeHierarchyType

Values that represent volume hierarchy htypes.

To be used in GameObject::setColliderModel(). Currently there is Collidable::VolumeHierarchyType::OCTREE

◆ VolumeType

Values that represent volume types.

To be used in GameObject::setColliderModel(). Currently there is Collidable::VolumeType::BSPHERE, Collidable::VolumeType::AABB, and Collidable::VolumeType::OBB

Function Documentation

◆ isRegisteredForCollisions()

bool Collidable::isRegisteredForCollisions ( ) const
protected

Query if this object is registered for collisions.

Returns
True if registered for collisions, false if not.

◆ setCollidableGroup()

template<class UserClass >
void Collidable::setCollidableGroup ( )
inlineprotected

Sets collidable group.

Must be called when the GameObject enters the scene for the first time and will use collisions.

Template Parameters
UserClassType of the user class.

◆ setColliderModel() [1/2]

void Collidable::setColliderModel ( Model *  pColliderModel,
VolumeHierarchyType  volumeHierarchyType,
int  maxDepth 
)
protected

Sets collider model and Collision Volume Hierarchy type.

MUST be set if collisions are to be used.

Parameters
pColliderModelpointer to a collider model.
volumeTypecollision volume type to be used.
maxDepthThe maximum depth of volume hierarchy.

◆ setColliderModel() [2/2]

void Collidable::setColliderModel ( Model *  pColliderModel,
VolumeType  volumeType 
)
protected

Sets collider model and Collision Volume type.

MUST be set if collisions are to be used.

Parameters
pColliderModelpointer to a collider model.
volumeTypecollision volume type to be used.

◆ submitCollisionDeregistration()

void Collidable::submitCollisionDeregistration ( )
protected

Submit collision deregistration.

◆ submitCollisionRegistration()

void Collidable::submitCollisionRegistration ( )
protected

Submit collision registration to current scene.

◆ terrainCollision()

void Collidable::terrainCollision ( )
virtual

Terrain collision callback for this object.

To be implemented by user in object derived from GameObject (NOT directly derived from Collidable). Called ONLY by current active scene.

◆ updateCollisionData()

void Collidable::updateCollisionData ( const Matrix &  world)
protected

Updates the collision data described by world matrix.

Must be called whenever the world matrix has changed.

Parameters
worldThe world.