CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
PhysicsComponent Class Reference

The Component that manages the physics for every Entity that registers with it. More...

#include <physics.h>

Inherits corgi::Component< PhysicsData >.

Detailed Description

The Component that manages the physics for every Entity that registers with it.

Public Member Functions

 PhysicsComponent ()
 Constructor for the PhysicsComponent.
 
virtual ~PhysicsComponent ()
 Destructor for the PhysicsComponent.
 
virtual void AddFromRawData (corgi::EntityRef &entity, const void *raw_data)
 Deserialize a flat binary buffer to create and populate an Entity from raw data. More...
 
virtual RawDataUniquePtr ExportRawData (const corgi::EntityRef &entity) const
 Serializes a PhysicsComponent's data for a given Entity. More...
 
virtual void Init ()
 Initializes the internal state of the variables for the PhysicsComponent.
 
virtual void InitEntity (corgi::EntityRef &)
 Adds an Entity to the TransformComponent. More...
 
virtual void CleanupEntity (corgi::EntityRef &entity)
 Disables physics for the given Entity. More...
 
virtual void UpdateAllEntities (corgi::WorldTime delta_time)
 Update the physics data for all Entities registered with this Component. More...
 
void ProcessBulletTickCallback ()
 Called after every Bullet frame update to handle all the collisions.
 
void UpdatePhysicsFromTransform (const corgi::EntityRef &entity)
 Update the physics world to match the transform for a given Entity. More...
 
void UpdatePhysicsScale (const corgi::EntityRef &entity)
 Update the physics scale to match the transform for a given Entity. More...
 
void EnablePhysics (const corgi::EntityRef &entity)
 Enables physics for a given Entity. More...
 
void DisablePhysics (const corgi::EntityRef &entity)
 Disables physics for a given Entity. More...
 
void AwakenEntity (const corgi::EntityRef &entity)
 Activate the rigid bodies for a given Entity to force reevaluation within the scene. More...
 
void AwakenAllEntities ()
 Activates the rigid bodies in every Entity to force reevaluation within the scene.
 
void InitStaticMesh (corgi::EntityRef &entity)
 Initialize the data needed to generate a static mesh. More...
 
void AddStaticMeshTriangle (const corgi::EntityRef &entity, const mathfu::vec3 &pt0, const mathfu::vec3 &pt1, const mathfu::vec3 &pt2)
 Add a triangle to the static mesh for the given entity. More...
 
void FinalizeStaticMesh (const corgi::EntityRef &entity, short collision_type, short collides_with, float mass, float restitution, const std::string &user_tag)
 Generates a static mesh shape and adds it to the world, based on the previously added static mesh triangles. More...
 
void GenerateRaycastShape (corgi::EntityRef &entity, bool result_exportable)
 Generate an AABB based on the rendermesh that collides with the raycast layer. More...
 
corgi::EntityRef RaycastSingle (mathfu::vec3 &start, mathfu::vec3 &end)
 Performs a raycast into the world, returning the first Entity hit. More...
 
corgi::EntityRef RaycastSingle (mathfu::vec3 &start, mathfu::vec3 &end, short layer_mask)
 Performs a raycast into the world, returning the first Entity hit. More...
 
corgi::EntityRef RaycastSingle (mathfu::vec3 &start, mathfu::vec3 &end, mathfu::vec3 *hit_point)
 Performs a raycast into the world, returning the first Entity hit. More...
 
corgi::EntityRef RaycastSingle (mathfu::vec3 &start, mathfu::vec3 &end, short layer_mask, mathfu::vec3 *hit_point)
 Performs a raycast into the world, returning the first Entity hit. More...
 
float GravityForEntity (const corgi::EntityRef &entity) const
 Get the gravity value of the given Entity. More...
 
void DebugDrawWorld (fplbase::Renderer *renderer, const mathfu::mat4 &camera_transform)
 Render the entire physics worlds using Bullet's default debugging tools. More...
 
void DebugDrawObject (fplbase::Renderer *renderer, const mathfu::mat4 &camera_transform, const corgi::EntityRef &entity, const mathfu::vec3 &color)
 Render a given Entity's physics using Bullet's default debugging tools. More...
 
btDiscreteDynamicsWorld * bullet_world ()
 Get the bullet world. More...
 
void set_gravity (float gravity)
 Set the gravity value. More...
 
float gravity () const
 Get the gravity value. More...
 
void set_max_steps (int max_steps)
 Set the max steps. More...
 
int max_steps () const
 Get the max steps. More...
 
CollisionDatacollision_data ()
 Get the collision data. More...
 
void set_collision_callback (CollisionCallback callback, void *user_data)
 Sets the callback function for collisions. More...
 
- Public Member Functions inherited from corgi::Component< PhysicsData >
 Component ()
 Construct a Component without an EntityManager.
 
virtual ~Component ()
 Destructor for a Component.
 
virtual void AddEntityGenerically (EntityRef &entity)
 Provides an alternate way to add Entities if you do not care about the returned data structure, and if you do not feel like casting the BaseComponent into something more specific. More...
 
PhysicsDataAddEntity (EntityRef &entity, AllocationLocation alloc_location)
 Adds an Entity to the list that this Component is tracking. More...
 
PhysicsDataAddEntity (EntityRef &entity)
 Adds an Entity to the list that this Component is tracking. More...
 
virtual void RemoveEntity (EntityRef &entity)
 Removes an Entity from the list of Entities. More...
 
virtual EntityIterator RemoveEntity (EntityIterator iter)
 Removes an Entity from the list of Entities. More...
 
virtual EntityIterator begin ()
 Gets an iterator that will iterate over every Entity associated with the Component, starting from the beginning. More...
 
virtual EntityIterator end ()
 Gets an iterator that points to the end of the list of all entites in the Component. More...
 
virtual bool HasDataForEntity (const EntityRef &entity)
 Checks if this component contains any data associated with the supplied entity.
 
virtual void * GetComponentDataAsVoid (const EntityRef &entity)
 Gets the data for a given Entity as a void pointer. More...
 
virtual const void * GetComponentDataAsVoid (const EntityRef &entity) const
 Gets the data for a given Entity as a const void pointer. More...
 
PhysicsDataGetComponentData (size_t data_index)
 Gets the Component data stored at a given index. More...
 
PhysicsDataGetComponentData (const EntityRef &entity)
 Gets the data for a given Entity. More...
 
const PhysicsDataGetComponentData (size_t data_index) const
 Gets the Component data stored at a given index. More...
 
const PhysicsDataGetComponentData (const EntityRef &entity) const
 Gets the data for a given Entity. More...
 
virtual void ClearComponentData ()
 Clears all tracked Component data.
 
ComponentDataType * Data (const EntityRef &entity)
 A utility function for retrieving the Component data for an Entity from a specific Component. More...
 
ComponentDataType * Data (const EntityRef &entity) const
 A utility function for retrieving the Component data for an Entity from a specific Component. More...
 
bool IsRegisteredWithComponent (const EntityRef &entity)
 A utility function for checking if an entity is registered with a particular component. More...
 
ComponentDataType * GetComponent ()
 A utility function for retrieving a reference to a specific Component object, by type. More...
 
virtual void Cleanup ()
 Override this function with any code that executes when this Component is removed from the EntityManager. (i.e. Usually when the game/state is over and everything is shutting down.)
 
virtual void SetEntityManager (EntityManager *entity_manager)
 Set the EntityManager for this Component. More...
 
virtual void SetComponentIdOnDataType (ComponentId id)
 Sets the Component ID on the data type. More...
 
- Public Member Functions inherited from corgi::ComponentInterface
virtual ~ComponentInterface ()
 A destructor for the Component interface.
 

Additional Inherited Members

- Public Types inherited from corgi::Component< PhysicsData >
typedef VectorPool
< ComponentData >::Iterator 
EntityIterator
 An iterator to iterate through all of the Entities in the Component.
 
typedef PhysicsData value_type
 The templated data type stored by this Component.
 
- Public Types inherited from corgi::ComponentInterface
typedef std::unique_ptr
< uint8_t, std::function< void(uint8_t *)> > 
RawDataUniquePtr
 A pointer type for exported raw data.
 
- Static Public Member Functions inherited from corgi::Component< PhysicsData >
static ComponentId GetComponentId ()
 Get the ID for this Component. More...
 
- Protected Member Functions inherited from corgi::Component< PhysicsData >
size_t GetComponentDataIndex (const EntityRef &entity) const
 Get the index of the Component data for a given Entity. More...
 
- Protected Attributes inherited from corgi::Component< PhysicsData >
VectorPool< ComponentData > component_data_
 Storage for all of the data for the Component.
 
EntityManagerentity_manager_
 A pointer to the EntityManager for this Component. This is the main point of contact for Components that need to talk to other things.
 
std::unordered_map
< EntityIdType, ComponentIndex
component_index_lookup_
 A map, for translating unique entity IDs into vectorpool indexes.
 

Member Function Documentation

virtual void PhysicsComponent::AddFromRawData ( corgi::EntityRef entity,
const void *  raw_data 
)
virtual

Deserialize a flat binary buffer to create and populate an Entity from raw data.

Parameters
[in,out]entityAn EntityRef reference that points to an Entity that is being added from the raw data.
[in]raw_dataA void pointer to the raw FlatBuffer data.

Implements corgi::ComponentInterface.

void PhysicsComponent::AddStaticMeshTriangle ( const corgi::EntityRef entity,
const mathfu::vec3 &  pt0,
const mathfu::vec3 &  pt1,
const mathfu::vec3 &  pt2 
)

Add a triangle to the static mesh for the given entity.

Note
Note that InitStaticMesh() needs to be called beforehand.
Parameters
[in]entityAn EntityRef to the Entity whose PhysicsData should add the new static mesh.
[in]pt0The first vertex for the triangle.
[in]pt1The second vertex for the triangle.
[in]pt2The third vertex for the triangle.
void PhysicsComponent::AwakenEntity ( const corgi::EntityRef entity)

Activate the rigid bodies for a given Entity to force reevaluation within the scene.

Parameters
[in]entityAn EntityRef reference to the Entity whose rigid bodies should be activated.
btDiscreteDynamicsWorld* PhysicsComponent::bullet_world ( )
inline

Get the bullet world.

Returns
Returns the bullet world as a pointer to btDiscreteDynamicsWorld.
virtual void PhysicsComponent::CleanupEntity ( corgi::EntityRef entity)
virtual

Disables physics for the given Entity.

Parameters
[in]entityAn EntityRef reference to the Entity that should be cleaned up by having its physics disabled.

Reimplemented from corgi::Component< PhysicsData >.

CollisionData& PhysicsComponent::collision_data ( )
inline

Get the collision data.

Returns
Returns a reference to the CollisionData.
void PhysicsComponent::DebugDrawObject ( fplbase::Renderer *  renderer,
const mathfu::mat4 &  camera_transform,
const corgi::EntityRef entity,
const mathfu::vec3 &  color 
)

Render a given Entity's physics using Bullet's default debugging tools.

Parameters
[in]rendererA pointer to the fplbase::Renderer to handle the rendering.
[in]camera_transformThe camera transform for the view projection.
[in]entityThe Entity whose physics should be rendered.
[in]colorThe color to draw the object in RGB.
void PhysicsComponent::DebugDrawWorld ( fplbase::Renderer *  renderer,
const mathfu::mat4 &  camera_transform 
)

Render the entire physics worlds using Bullet's default debugging tools.

Parameters
[in]rendererA pointer to the fplbase::Renderer to handle the rendering.
[in]camera_transformThe camera transform for the view projection.
void PhysicsComponent::DisablePhysics ( const corgi::EntityRef entity)

Disables physics for a given Entity.

Parameters
[in]entityAn EntityRef reference to the Entity whose physics should be disabled.
void PhysicsComponent::EnablePhysics ( const corgi::EntityRef entity)

Enables physics for a given Entity.

Parameters
[in]entityAn EntityRef reference to the Entity whose physics should be enabled.
virtual RawDataUniquePtr PhysicsComponent::ExportRawData ( const corgi::EntityRef entity) const
virtual

Serializes a PhysicsComponent's data for a given Entity.

Parameters
[in]entityAn EntityRef reference to an Entity whose corresponding PhysicsData will be serialized.
Returns
Returns a RawDataUniquePtr to the start of the raw data in a flat binary buffer.

Reimplemented from corgi::Component< PhysicsData >.

void PhysicsComponent::FinalizeStaticMesh ( const corgi::EntityRef entity,
short  collision_type,
short  collides_with,
float  mass,
float  restitution,
const std::string &  user_tag 
)

Generates a static mesh shape and adds it to the world, based on the previously added static mesh triangles.

Parameters
[in]entityAn EntityRef to the Entity whose PhysicsData contains the static mesh.
[in]collision_typeA bit field determining the type of collision for the rigid body.
[in]collides_withA bit field determining what type of objects the rigid body can collide into.
[in]massThe mass of the rigid body.
[in]restitutionThe restitution for the rigid body.
[in]user_tagA C-string provided by the user to identify the rigid body.
void PhysicsComponent::GenerateRaycastShape ( corgi::EntityRef entity,
bool  result_exportable 
)

Generate an AABB based on the rendermesh that collides with the raycast layer.

Note
If the entity already collides with the raycast layer, no change occurs. If there is no rendermesh, a unit cube is used instead.
Parameters
[in]entityAn EntityRef to the Entity whose AABB should be generated for raycasting.
[in]result_exportableA bool flag determining if the resulting shape should be exported.
float PhysicsComponent::gravity ( ) const
inline

Get the gravity value.

Returns
Returns the gravity as a float.
float PhysicsComponent::GravityForEntity ( const corgi::EntityRef entity) const

Get the gravity value of the given Entity.

Parameters
[in]entityThe Entity to get the custom gravity for.
Returns
The gravity value of the Entity.
virtual void PhysicsComponent::InitEntity ( corgi::EntityRef )
virtual

Adds an Entity to the TransformComponent.

Note
PhysicsComponents require that you have a TransformComponent.

Reimplemented from corgi::Component< PhysicsData >.

void PhysicsComponent::InitStaticMesh ( corgi::EntityRef entity)

Initialize the data needed to generate a static mesh.

Adds the entity to the PhysicsComponent, if necessary.

Parameters
[in]entityAn EntityRef to the entity that should be added to the PhysicsComponent, if necessary.
int PhysicsComponent::max_steps ( ) const
inline

Get the max steps.

Returns
Returns the max steps as an int.
corgi::EntityRef PhysicsComponent::RaycastSingle ( mathfu::vec3 &  start,
mathfu::vec3 &  end 
)

Performs a raycast into the world, returning the first Entity hit.

Parameters
[in]startThe start point (origin) of the ray.
[in]endThe end point of the ray.
Returns
Returns an EntityRef to the first Entity that was hit.
corgi::EntityRef PhysicsComponent::RaycastSingle ( mathfu::vec3 &  start,
mathfu::vec3 &  end,
short  layer_mask 
)

Performs a raycast into the world, returning the first Entity hit.

Parameters
[in]startThe start point (origin) of the ray.
[in]endThe end point of the ray.
[in]layer_maskA bit field used to specify which layers the raycast could hit.
Returns
Returns an EntityRef to the first Entity that was hit.
corgi::EntityRef PhysicsComponent::RaycastSingle ( mathfu::vec3 &  start,
mathfu::vec3 &  end,
mathfu::vec3 *  hit_point 
)

Performs a raycast into the world, returning the first Entity hit.

Parameters
[in]startThe start point (origin) of the ray.
[in]endThe end point of the ray.
[out]hit_pointCaptures the output of the value where the ray hit the Entity in world position, as a vec3.
Returns
Returns an EntityRef to the first Entity that was hit.
corgi::EntityRef PhysicsComponent::RaycastSingle ( mathfu::vec3 &  start,
mathfu::vec3 &  end,
short  layer_mask,
mathfu::vec3 *  hit_point 
)

Performs a raycast into the world, returning the first Entity hit.

Parameters
[in]startThe start point (origin) of the ray.
[in]endThe end point of the ray.
[in]layer_maskA bit field used to specify which layers the raycast could hit.
[out]hit_pointCaptures the output of the value where the ray hit the Entity in world position, as a vec3.
Returns
Returns an EntityRef to the first Entity that was hit.
void PhysicsComponent::set_collision_callback ( CollisionCallback  callback,
void *  user_data 
)
inline

Sets the callback function for collisions.

Parameters
[in]callbackThe event callback to call when a collision occurs. If a callback is registered, then it is called in addition to evaluating the graph.
[in]user_dataA void pointer to any data provided to for the callback.
void PhysicsComponent::set_gravity ( float  gravity)
inline

Set the gravity value.

Parameters
[in]gravityA float to set as the new gravity value.
void PhysicsComponent::set_max_steps ( int  max_steps)
inline

Set the max steps.

Parameters
[in]max_stepsAn int to set as the new max steps value.
virtual void PhysicsComponent::UpdateAllEntities ( corgi::WorldTime  delta_time)
virtual

Update the physics data for all Entities registered with this Component.

Parameters
[in]delta_timeThe time delta since the last call.

Reimplemented from corgi::Component< PhysicsData >.

void PhysicsComponent::UpdatePhysicsFromTransform ( const corgi::EntityRef entity)

Update the physics world to match the transform for a given Entity.

Parameters
[in]entityThe Entity whose physics data should be updated to match the transform data.
void PhysicsComponent::UpdatePhysicsScale ( const corgi::EntityRef entity)

Update the physics scale to match the transform for a given Entity.

Parameters
[in]entityThe Entity whose physics scale should be updated to match the transform data.

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