VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
vex::PhysicsSystem Class Reference

Class representing a physics system. More...

#include <PhysicsSystem.hpp>

Classes

struct  InterpCache

Public Member Functions

 PhysicsSystem (vex::Registry &registry)
 Constructor, initializes physics system.
 ~PhysicsSystem ()
 Destructor, simply calls shutdown().
void setDebugRenderer (JPH::DebugRenderer *renderer)
 Sets the debug renderer for the physics system.
void drawDebug (bool drawConstraints=true, bool drawWireframe=true)
 Draws debug information for the physics system.
bool init (size_t maxBodies=1024)
 Initializes jolts physics system.
void shutdown ()
 clears all physics objects
void update (float deltaTime)
 Updates physics, it is technically fixed time but needs delta to track if required time already passed.
void SyncBodies ()
 Scans registry for PhysicsComponents without bodies and creates them.
void SetGravityVector (const glm::vec3 &gravity)
 Allows for updating gravity.
void SetGravityFactor (JPH::BodyID bodyId, float gravityFactor)
 Allows for updating gravity factor per object.
void SetFriction (JPH::BodyID bodyId, float friction)
 Allows for updating friction.
float GetFriction (JPH::BodyID bodyId)
 Allows for getting friction.
void SetBounciness (JPH::BodyID bodyId, float bounciness)
 Allows for updating bounciness.
float GetBounciness (JPH::BodyID bodyId)
 Allows for getting bounciness.
void SetLinearVelocity (JPH::BodyID bodyId, const glm::vec3 &velocity)
 Allows for updating linear velocity.
glm::vec3 GetLinearVelocity (JPH::BodyID bodyId)
 Allows for getting linear velocity.
glm::vec3 GetVelocityAtPosition (JPH::BodyID bodyId, const glm::vec3 &point)
 Gets the velocity of a specific point on the body (in World Space).
void AddLinearVelocity (JPH::BodyID bodyId, const glm::vec3 &velocity)
 Allows for adding to linear velocity.
void SetAngularVelocity (JPH::BodyID bodyId, const glm::vec3 &velocity)
 Allows for setting angular velocity.
glm::vec3 GetAngularVelocity (JPH::BodyID bodyId)
 Allows for getting angular velocity.
void AddAngularVelocity (JPH::BodyID bodyId, const glm::vec3 &velocity)
 Allows for adding to angular velocity.
void AddTorque (JPH::BodyID bodyId, const glm::vec3 &torque)
 Applies torque to the body, it is reset next physics tick.
void AddForce (JPH::BodyID bodyId, const glm::vec3 &force)
 Applies force at the mass center of the body, it is reset next physics tick.
void AddForceAtPosition (JPH::BodyID bodyId, const glm::vec3 &force, const glm::vec3 &position)
 Applies force at the given position of the body, it is reset next physics tick.
void AddImpulse (JPH::BodyID bodyId, const glm::vec3 &impulse)
 Applies impulse at the mass center of the body, it is reset next physics tick.
void AddImpulseAtPosition (JPH::BodyID bodyId, const glm::vec3 &impulse, const glm::vec3 &position)
 Applies impulse at the given position of the body, it is reset next physics tick.
void AddAngularImpulse (JPH::BodyID bodyId, const glm::vec3 &impulse)
 Applies angular impulse at the mass center of the body, it is reset next physics tick.
void SetBodyActive (JPH::BodyID bodyId, bool active)
 Allows for setting body active state.
bool GetBodyActive (JPH::BodyID bodyId)
 Allows for getting body active state.
std::optional< JPH::BodyID > CreateBodyForEntity (vex::Entity e, vex::Registry &r, PhysicsComponent &pc)
 Allows to recreate physics body at runtime.
std::optional< JPH::BodyID > RecreateBodyForEntity (vex::Entity e, PhysicsComponent &pc)
 Allows to recreate physics body at runtime.
void DestroyBodyForEntity (PhysicsComponent &pc)
 Destroys a physics body.
bool raycast (const glm::vec3 &origin, const glm::vec3 &direction, float maxDistance, RaycastHit &hit)
 Performs a raycast in the physics world.
int GetCollisionSteps ()
 Allows for getting collision steps (collision accuracy).
void setCollisionSteps (int steps)
 Allows for setting collision steps (collision accuracy).
void setEnableSmoothing (bool enable)
 Allows for enabling/disabling smoothing.
glm::vec3 GetPhysicsPosition (JPH::BodyID bodyId)
 Retrieves the physics position of a body.
glm::quat GetPhysicsRotation (JPH::BodyID bodyId)
 Retrieves the physics rotation of a body.
PhysicsComponentgetPhysicsComponentByBodyId (JPH::BodyID id)
vex::Entity getEntityByBodyId (JPH::BodyID id)

Private Member Functions

void SyncBodyToTransform (vex::Entity e, vex::Registry &r, const JPH::BodyID &id, float alpha=1.0f)
void onPhysicsComponentDestroy (vex::Registry &reg, vex::Entity e)
void InitializeCharacter (vex::Entity e, CharacterComponent &cc)
void WeldVertices (const std::vector< glm::vec3 > &inVerts, const std::vector< uint32_t > &inIndices, JPH::VertexList &outVerts, JPH::IndexedTriangleList &outTris)
 Helper to weld vertices based on position ONLY (ignoring UVs/Normals which split render meshes).

Static Private Member Functions

static JPH::Quat EulerToQuat (const glm::vec3 &eulerDeg)
static glm::vec3 QuatToEuler (const JPH::Quat &q)

Private Attributes

int collisionSteps = 3
bool enableSmoothing = true
JPH::TempAllocatorImpl * m_tempAllocator = nullptr
JPH::JobSystem * m_jobSystem = nullptr
JPH::PhysicsSystem * m_physicsSystem = nullptr
JPH::DebugRenderer * m_debugRenderer = nullptr
vex::Registrym_registry
BPLayerInterfaceImpl m_bpInterface
ObjectVsBroadPhaseLayerFilterImpl m_objVsBpFilter
ObjectLayerPairFilterImpl m_objLayerPairFilter
std::unique_ptr< MyActivationListenerm_activationListener
std::unique_ptr< JPH::ContactListener > m_contactListener
std::unordered_map< JPH::BodyID, vex::Entity, BodyIDHasherm_bodyToEntity
std::unordered_map< JPH::BodyID, InterpCache, BodyIDHasherm_interpCache
float m_fixedDt = 1.0f / 60.0f
float m_accumulator = 0.0f

Friends

class MyContactListener

Detailed Description

Class representing a physics system.

Definition at line 299 of file PhysicsSystem.hpp.

Constructor & Destructor Documentation

◆ PhysicsSystem()

vex::PhysicsSystem::PhysicsSystem ( vex::Registry & registry)
inline

Constructor, initializes physics system.

Parameters
vex::Registry&registry The registry to use for entity-component system.

Definition at line 303 of file PhysicsSystem.hpp.

◆ ~PhysicsSystem()

vex::PhysicsSystem::~PhysicsSystem ( )

Destructor, simply calls shutdown().

Definition at line 78 of file PhysicsSystem.cpp.

Member Function Documentation

◆ AddAngularImpulse()

void vex::PhysicsSystem::AddAngularImpulse ( JPH::BodyID bodyId,
const glm::vec3 & impulse )

Applies angular impulse at the mass center of the body, it is reset next physics tick.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& impulse The impulse to apply.

Definition at line 694 of file PhysicsSystem.cpp.

◆ AddAngularVelocity()

void vex::PhysicsSystem::AddAngularVelocity ( JPH::BodyID bodyId,
const glm::vec3 & velocity )

Allows for adding to angular velocity.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& velocity The velocity to add.

Definition at line 668 of file PhysicsSystem.cpp.

◆ AddForce()

void vex::PhysicsSystem::AddForce ( JPH::BodyID bodyId,
const glm::vec3 & force )

Applies force at the mass center of the body, it is reset next physics tick.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& force The force to apply.

Definition at line 678 of file PhysicsSystem.cpp.

◆ AddForceAtPosition()

void vex::PhysicsSystem::AddForceAtPosition ( JPH::BodyID bodyId,
const glm::vec3 & force,
const glm::vec3 & position )

Applies force at the given position of the body, it is reset next physics tick.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& force The force to apply.
constglm::vec3& position The position to apply the force at.

Definition at line 682 of file PhysicsSystem.cpp.

◆ AddImpulse()

void vex::PhysicsSystem::AddImpulse ( JPH::BodyID bodyId,
const glm::vec3 & impulse )

Applies impulse at the mass center of the body, it is reset next physics tick.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& impulse The impulse to apply.

Definition at line 686 of file PhysicsSystem.cpp.

◆ AddImpulseAtPosition()

void vex::PhysicsSystem::AddImpulseAtPosition ( JPH::BodyID bodyId,
const glm::vec3 & impulse,
const glm::vec3 & position )

Applies impulse at the given position of the body, it is reset next physics tick.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& impulse The impulse to apply.
constglm::vec3& position The position to apply the impulse at.

Definition at line 690 of file PhysicsSystem.cpp.

◆ AddLinearVelocity()

void vex::PhysicsSystem::AddLinearVelocity ( JPH::BodyID bodyId,
const glm::vec3 & velocity )

Allows for adding to linear velocity.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& velocity The velocity to add.

Definition at line 653 of file PhysicsSystem.cpp.

◆ AddTorque()

void vex::PhysicsSystem::AddTorque ( JPH::BodyID bodyId,
const glm::vec3 & torque )

Applies torque to the body, it is reset next physics tick.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& torque The torque to apply.

Definition at line 674 of file PhysicsSystem.cpp.

◆ CreateBodyForEntity()

std::optional< JPH::BodyID > vex::PhysicsSystem::CreateBodyForEntity ( vex::Entity e,
vex::Registry & r,
PhysicsComponent & pc )

Allows to recreate physics body at runtime.

Parameters
vex::Entitye The entity to create the body for.
vex::Registry&r The registry to get the entity data from.
PhysicsComponent&pc The physics component to update.
Returns
std::optional<JPH::BodyID> The ID of the created body.

Definition at line 330 of file PhysicsSystem.cpp.

◆ DestroyBodyForEntity()

void vex::PhysicsSystem::DestroyBodyForEntity ( PhysicsComponent & pc)

Destroys a physics body.

Parameters
PhysicsComponent&pc The physics component to update.

Definition at line 495 of file PhysicsSystem.cpp.

◆ drawDebug()

void vex::PhysicsSystem::drawDebug ( bool drawConstraints = true,
bool drawWireframe = true )

Draws debug information for the physics system.

Parameters
booldrawConstraints Whether to draw constraints.
booldrawWireframe Whether to draw wireframe.

Definition at line 118 of file PhysicsSystem.cpp.

◆ EulerToQuat()

JPH::Quat vex::PhysicsSystem::EulerToQuat ( const glm::vec3 & eulerDeg)
staticprivate

Definition at line 512 of file PhysicsSystem.cpp.

◆ GetAngularVelocity()

glm::vec3 vex::PhysicsSystem::GetAngularVelocity ( JPH::BodyID bodyId)

Allows for getting angular velocity.

Parameters
JPH::BodyIDbodyId The ID of the body to get.

Definition at line 663 of file PhysicsSystem.cpp.

◆ GetBodyActive()

bool vex::PhysicsSystem::GetBodyActive ( JPH::BodyID bodyId)

Allows for getting body active state.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
Returns
bool The active state of the body.

Definition at line 703 of file PhysicsSystem.cpp.

◆ GetBounciness()

float vex::PhysicsSystem::GetBounciness ( JPH::BodyID bodyId)

Allows for getting bounciness.

Parameters
JPH::BodyIDbodyId The ID of the body to get.

Definition at line 634 of file PhysicsSystem.cpp.

◆ GetCollisionSteps()

int vex::PhysicsSystem::GetCollisionSteps ( )
inline

Allows for getting collision steps (collision accuracy).

Returns
int - number of collision steps

Definition at line 464 of file PhysicsSystem.hpp.

◆ getEntityByBodyId()

vex::Entity vex::PhysicsSystem::getEntityByBodyId ( JPH::BodyID id)

Definition at line 573 of file PhysicsSystem.cpp.

◆ GetFriction()

float vex::PhysicsSystem::GetFriction ( JPH::BodyID bodyId)

Allows for getting friction.

Parameters
JPH::BodyIDbodyId The ID of the body to get.

Definition at line 624 of file PhysicsSystem.cpp.

◆ GetLinearVelocity()

glm::vec3 vex::PhysicsSystem::GetLinearVelocity ( JPH::BodyID bodyId)

Allows for getting linear velocity.

Parameters
JPH::BodyIDbodyId The ID of the body to get.

Definition at line 642 of file PhysicsSystem.cpp.

◆ getPhysicsComponentByBodyId()

PhysicsComponent & vex::PhysicsSystem::getPhysicsComponentByBodyId ( JPH::BodyID id)

Definition at line 564 of file PhysicsSystem.cpp.

◆ GetPhysicsPosition()

glm::vec3 vex::PhysicsSystem::GetPhysicsPosition ( JPH::BodyID bodyId)

Retrieves the physics position of a body.

Parameters
JPH::BodyIDbodyId - the body ID to retrieve the position for
Returns
glm::vec3 - the physics position of the body

Definition at line 707 of file PhysicsSystem.cpp.

◆ GetPhysicsRotation()

glm::quat vex::PhysicsSystem::GetPhysicsRotation ( JPH::BodyID bodyId)

Retrieves the physics rotation of a body.

Parameters
JPH::BodyIDbodyId - the body ID to retrieve the rotation for
Returns
glm::quat - the physics rotation of the body

Definition at line 713 of file PhysicsSystem.cpp.

◆ GetVelocityAtPosition()

glm::vec3 vex::PhysicsSystem::GetVelocityAtPosition ( JPH::BodyID bodyId,
const glm::vec3 & point )

Gets the velocity of a specific point on the body (in World Space).

Parameters
JPH::BodyIDbodyId The ID of the body to get.
constglm::vec3& point The point on the body to get the velocity at.

Definition at line 647 of file PhysicsSystem.cpp.

◆ init()

bool vex::PhysicsSystem::init ( size_t maxBodies = 1024)

Initializes jolts physics system.

Parameters
size_tmaxBodies The maximum number of bodies to support.
Todo
make it use max models from context

Definition at line 34 of file PhysicsSystem.cpp.

◆ InitializeCharacter()

void vex::PhysicsSystem::InitializeCharacter ( vex::Entity e,
CharacterComponent & cc )
private

Definition at line 163 of file PhysicsSystem.cpp.

◆ onPhysicsComponentDestroy()

void vex::PhysicsSystem::onPhysicsComponentDestroy ( vex::Registry & reg,
vex::Entity e )
private

Definition at line 505 of file PhysicsSystem.cpp.

◆ QuatToEuler()

glm::vec3 vex::PhysicsSystem::QuatToEuler ( const JPH::Quat & q)
staticprivate

Definition at line 518 of file PhysicsSystem.cpp.

◆ raycast()

bool vex::PhysicsSystem::raycast ( const glm::vec3 & origin,
const glm::vec3 & direction,
float maxDistance,
RaycastHit & hit )

Performs a raycast in the physics world.

Parameters
constglm::vec3& origin The origin of the ray.
constglm::vec3& direction The direction of the ray.
floatmaxDistance The maximum distance to check.
RaycastHit&hit The hit information.
Returns
bool - true if a hit occurred, false otherwise.

Definition at line 598 of file PhysicsSystem.cpp.

◆ RecreateBodyForEntity()

std::optional< JPH::BodyID > vex::PhysicsSystem::RecreateBodyForEntity ( vex::Entity e,
PhysicsComponent & pc )

Allows to recreate physics body at runtime.

Parameters
vex::Entitye The entity to create the body for.
PhysicsComponent&pc The physics component to update.
Returns
std::optional<JPH::BodyID> The ID of the created body.

Definition at line 490 of file PhysicsSystem.cpp.

◆ SetAngularVelocity()

void vex::PhysicsSystem::SetAngularVelocity ( JPH::BodyID bodyId,
const glm::vec3 & velocity )

Allows for setting angular velocity.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& velocity The velocity to set.

Definition at line 659 of file PhysicsSystem.cpp.

◆ SetBodyActive()

void vex::PhysicsSystem::SetBodyActive ( JPH::BodyID bodyId,
bool active )

Allows for setting body active state.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
boolactive The active state to set.

Definition at line 698 of file PhysicsSystem.cpp.

◆ SetBounciness()

void vex::PhysicsSystem::SetBounciness ( JPH::BodyID bodyId,
float bounciness )

Allows for updating bounciness.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
floatbounciness The new bounciness value.

Definition at line 628 of file PhysicsSystem.cpp.

◆ setCollisionSteps()

void vex::PhysicsSystem::setCollisionSteps ( int steps)
inline

Allows for setting collision steps (collision accuracy).

Parameters
intsteps - number of collision steps

Definition at line 468 of file PhysicsSystem.hpp.

◆ setDebugRenderer()

void vex::PhysicsSystem::setDebugRenderer ( JPH::DebugRenderer * renderer)

Sets the debug renderer for the physics system.

Parameters
JPH::DebugRenderer*renderer The debug renderer to use.

Definition at line 114 of file PhysicsSystem.cpp.

◆ setEnableSmoothing()

void vex::PhysicsSystem::setEnableSmoothing ( bool enable)
inline

Allows for enabling/disabling smoothing.

Parameters
boolenable - true to enable smoothing, false to disable

Definition at line 472 of file PhysicsSystem.hpp.

◆ SetFriction()

void vex::PhysicsSystem::SetFriction ( JPH::BodyID bodyId,
float friction )

Allows for updating friction.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
floatfriction The new friction value.

Definition at line 618 of file PhysicsSystem.cpp.

◆ SetGravityFactor()

void vex::PhysicsSystem::SetGravityFactor ( JPH::BodyID bodyId,
float gravityFactor )
inline

Allows for updating gravity factor per object.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
floatgravityFactor The new gravity factor.

Definition at line 340 of file PhysicsSystem.hpp.

◆ SetGravityVector()

void vex::PhysicsSystem::SetGravityVector ( const glm::vec3 & gravity)
inline

Allows for updating gravity.

Parameters
constglm::vec3& gravity The new gravity vector.

Definition at line 333 of file PhysicsSystem.hpp.

◆ SetLinearVelocity()

void vex::PhysicsSystem::SetLinearVelocity ( JPH::BodyID bodyId,
const glm::vec3 & velocity )

Allows for updating linear velocity.

Parameters
JPH::BodyIDbodyId The ID of the body to update.
constglm::vec3& velocity The new linear velocity.

Definition at line 638 of file PhysicsSystem.cpp.

◆ shutdown()

void vex::PhysicsSystem::shutdown ( )

clears all physics objects

Definition at line 82 of file PhysicsSystem.cpp.

◆ SyncBodies()

void vex::PhysicsSystem::SyncBodies ( )

Scans registry for PhysicsComponents without bodies and creates them.

Definition at line 140 of file PhysicsSystem.cpp.

◆ SyncBodyToTransform()

void vex::PhysicsSystem::SyncBodyToTransform ( vex::Entity e,
vex::Registry & r,
const JPH::BodyID & id,
float alpha = 1.0f )
private

Definition at line 523 of file PhysicsSystem.cpp.

◆ update()

void vex::PhysicsSystem::update ( float deltaTime)

Updates physics, it is technically fixed time but needs delta to track if required time already passed.

Parameters
floatdeltaTime The time elapsed since the last update.

Definition at line 183 of file PhysicsSystem.cpp.

◆ WeldVertices()

void vex::PhysicsSystem::WeldVertices ( const std::vector< glm::vec3 > & inVerts,
const std::vector< uint32_t > & inIndices,
JPH::VertexList & outVerts,
JPH::IndexedTriangleList & outTris )
private

Helper to weld vertices based on position ONLY (ignoring UVs/Normals which split render meshes).

Parameters
conststd::vector<glm::vec3>& inVerts - the input vertices
conststd::vector<uint32_t>& inIndices - the input indices
JPH::VertexList&outVerts - the output vertices
JPH::IndexedTriangleList&outTris - the output triangles

Definition at line 287 of file PhysicsSystem.cpp.

◆ MyContactListener

friend class MyContactListener
friend

Definition at line 495 of file PhysicsSystem.hpp.

Member Data Documentation

◆ collisionSteps

int vex::PhysicsSystem::collisionSteps = 3
private

Definition at line 497 of file PhysicsSystem.hpp.

◆ enableSmoothing

bool vex::PhysicsSystem::enableSmoothing = true
private

Definition at line 498 of file PhysicsSystem.hpp.

◆ m_accumulator

float vex::PhysicsSystem::m_accumulator = 0.0f
private

Definition at line 527 of file PhysicsSystem.hpp.

◆ m_activationListener

std::unique_ptr<MyActivationListener> vex::PhysicsSystem::m_activationListener
private

Definition at line 510 of file PhysicsSystem.hpp.

◆ m_bodyToEntity

std::unordered_map<JPH::BodyID, vex::Entity, BodyIDHasher> vex::PhysicsSystem::m_bodyToEntity
private

Definition at line 513 of file PhysicsSystem.hpp.

◆ m_bpInterface

BPLayerInterfaceImpl vex::PhysicsSystem::m_bpInterface
private

Definition at line 507 of file PhysicsSystem.hpp.

◆ m_contactListener

std::unique_ptr<JPH::ContactListener> vex::PhysicsSystem::m_contactListener
private

Definition at line 511 of file PhysicsSystem.hpp.

◆ m_debugRenderer

JPH::DebugRenderer* vex::PhysicsSystem::m_debugRenderer = nullptr
private

Definition at line 503 of file PhysicsSystem.hpp.

◆ m_fixedDt

float vex::PhysicsSystem::m_fixedDt = 1.0f / 60.0f
private

Definition at line 526 of file PhysicsSystem.hpp.

◆ m_interpCache

std::unordered_map<JPH::BodyID, InterpCache, BodyIDHasher> vex::PhysicsSystem::m_interpCache
private

Definition at line 524 of file PhysicsSystem.hpp.

◆ m_jobSystem

JPH::JobSystem* vex::PhysicsSystem::m_jobSystem = nullptr
private

Definition at line 501 of file PhysicsSystem.hpp.

◆ m_objLayerPairFilter

ObjectLayerPairFilterImpl vex::PhysicsSystem::m_objLayerPairFilter
private

Definition at line 509 of file PhysicsSystem.hpp.

◆ m_objVsBpFilter

ObjectVsBroadPhaseLayerFilterImpl vex::PhysicsSystem::m_objVsBpFilter
private

Definition at line 508 of file PhysicsSystem.hpp.

◆ m_physicsSystem

JPH::PhysicsSystem* vex::PhysicsSystem::m_physicsSystem = nullptr
private

Definition at line 502 of file PhysicsSystem.hpp.

◆ m_registry

vex::Registry& vex::PhysicsSystem::m_registry
private

Definition at line 505 of file PhysicsSystem.hpp.

◆ m_tempAllocator

JPH::TempAllocatorImpl* vex::PhysicsSystem::m_tempAllocator = nullptr
private

Definition at line 500 of file PhysicsSystem.hpp.


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