Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Physics::System Namespace Reference

Classes

struct  ConstraintContext
struct  Vec3Hash
 Hash function for glm::vec3 to use in unordered_map. More...
struct  Vec3Equal
 Equality function for glm::vec3 (exact comparison). More...
struct  DeduplicatedMesh
 Structure holding deduplicated mesh data for Jolt soft body. More...
struct  CreateSettingsResult
 Result of creating Jolt shared settings, includes vertex mapping. More...

Functions

static void OnCharacterControllerConstruct (Engine::Core::Registry &registry, Engine::EntityId entityId)
static void OnCharacterControllerDestroy (Engine::Core::Registry &registry, Engine::EntityId entityId)
void CharacterControllerUpdate (Engine::Core &core)
 Update the character controller system. This will process all entities with character controllers and update their state based on input and physics interactions.
void InitCharacterControllerSystem (Engine::Core &core)
 Initialize the character controller system. This will set up necessary resources and state for character controllers to function.
void ShutdownCharacterControllerSystem (Engine::Core &core)
 Shutdown the character controller system. This will clean up any resources used by the system.
Component::RigidBodyInternalGetBodyInternal (Engine::Entity entity, const char *constraintName, const char *bodyName)
void FinalizeConstraint (ConstraintContext &ctx, Engine::Entity entity, JPH::Constraint *joltConstraint, Component::ConstraintType type, const Component::ConstraintSettings &settings, const char *constraintName)
void DestroyConstraint (Engine::Core::Registry &registry, Engine::EntityId entity, const char *constraintName)
template<typename ConstraintT>
static bool ValidateConstraint (const ConstraintT &constraint, const char *constraintName)
template<typename SettingsT, typename ConstraintT>
static JPH::Constraint * CreateJoltConstraint (ConstraintContext &ctx, SettingsT &joltSettings, const ConstraintT &constraint, Component::RigidBodyInternal *internalA, const char *constraintName)
template<Component::ConstraintType TYPE, typename CompT, typename SettingsT, typename Configurator, typename ExtraValidate>
static void CreateConstraintGeneric (Engine::Core::Registry &registry, Engine::EntityId entity, const char *constraintName, Configurator &&configurator, ExtraValidate &&extraValidate)
static void OnFixedConstraintConstruct (Engine::Core::Registry &registry, Engine::EntityId entity)
static void OnFixedConstraintDestroy (Engine::Core::Registry &registry, Engine::EntityId entity)
static void OnDistanceConstraintConstruct (Engine::Core::Registry &registry, Engine::EntityId entity)
static void OnDistanceConstraintDestroy (Engine::Core::Registry &registry, Engine::EntityId entity)
static void OnPointConstraintConstruct (Engine::Core::Registry &registry, Engine::EntityId entity)
static void OnPointConstraintDestroy (Engine::Core::Registry &registry, Engine::EntityId entity)
void InitConstraintSystem (Engine::Core &core)
void InitJoltPhysics (Engine::Core &core)
 Init internal elements needed by JoltPhysics.
void InitPhysicsManager (Engine::Core &core)
 Init the PhysicsManager.
void PhysicsUpdate (Engine::Core &core)
 Applies every update of the physics system.
static JPH::RefConst< JPH::Shape > CreateConvexHullFromMesh (const Object::Component::Mesh &mesh, const Component::ConvexHullMeshCollider *meshCollider, const glm::vec3 &scale)
 Create a ConvexHullShape from mesh vertices.
static JPH::RefConst< JPH::Shape > CreateMeshShapeFromMesh (const Object::Component::Mesh &mesh, const Component::MeshCollider *meshCollider, const glm::vec3 &scale)
 Create a MeshShape from mesh vertices and indices.
static JPH::RefConst< JPH::Shape > CreateShapeFromColliders (Engine::Core::Registry &registry, Engine::EntityId entity)
 Create a Jolt shape from collider components.
static void OnRigidBodyConstruct (Engine::Core::Registry &registry, Engine::EntityId entityId)
 Called when RigidBody component is added to an entity.
static void OnRigidBodyInternalConstruct (Engine::Core::Registry &registry, Engine::EntityId entityId)
static void OnRigidBodyDestroy (Engine::Core::Registry &registry, Engine::EntityId entityId)
 Called when RigidBody component is removed from an entity.
static void OnRigidBodyInternalDestroy (Engine::Core::Registry &registry, Engine::EntityId entityId)
void InitRigidBodySystem (Engine::Core &core)
 Initialize RigidBody system and register entt hooks.
void ShutdownRigidBodySystem (Engine::Core &core)
 Shutdown RigidBody system and unregister entt hooks.
static std::vector< std::pair< uint32_t, uint32_t > > GenerateEdgesFromFaces (const std::vector< uint32_t > &faceIndices)
 Generate edge constraints from triangle face indices.
static DeduplicatedMesh DeduplicateMesh (const Object::Component::Mesh &mesh)
 Convert a potentially "flat" mesh (with duplicated vertices) to an indexed mesh with unique vertices.
static CreateSettingsResult CreateJoltSharedSettings (const Component::SoftBody &softBody, const Object::Component::Mesh &mesh, const glm::vec3 &scale=glm::vec3(1.0f))
 Convert SoftBody component to Jolt SoftBodySharedSettings.
static JPH::SoftBodyCreationSettings CreateJoltCreationSettings (const Component::SoftBody &softBody, const JPH::Ref< JPH::SoftBodySharedSettings > &sharedSettings, const JPH::RVec3 &position, const JPH::Quat &rotation)
 Create Jolt SoftBodyCreationSettings from component.
static void OnSoftBodyConstruct (Engine::Core::Registry &registry, Engine::EntityId entityId)
static void OnSoftBodyInternalConstruct (Engine::Core::Registry &registry, Engine::EntityId entityId)
static void OnSoftBodyDestroy (Engine::Core::Registry &registry, Engine::EntityId entityId)
static void OnSoftBodyInternalDestroy (Engine::Core::Registry &registry, Engine::EntityId entityId)
void InitSoftBodySystem (Engine::Core &core)
 Initialize the soft body system.
void ShutdownSoftBodySystem (Engine::Core &core)
 Shutdown the soft body system.
void SyncSoftBodyVertices (Engine::Core &core)
 Synchronize soft body vertex positions to mesh data.
void SyncTransformWithPhysics (Engine::Core &core)
 Synchronize Transform components with Jolt Physics body positions.
void VehicleControlSystem (Engine::Core &core)
 Apply vehicle controller inputs to Jolt vehicle constraints.
void VehicleRPMUpdate (Engine::Core &core)
 Update per-vehicle RPM telemetry from Jolt controller. Runs in FixedTimeUpdate.
static void CreateJoltWheelSettings (JPH::WheelSettingsWV &joltWheel, const Component::WheelSettings &wheelSettings, const glm::vec3 &position, bool isRear)
 Create Jolt wheel settings from Physics wheel configuration.
static void OnVehicleConstruct (Engine::Core::Registry &registry, Engine::EntityId entityId)
 Called when Vehicle component is added to an entity.
static void OnVehicleDestroy (Engine::Core::Registry &registry, Engine::EntityId entityId)
 Called when Vehicle component is removed from an entity.
void InitVehicleSystem (Engine::Core &core)
 Initialize the vehicle system with entt lifecycle hooks.
void WheelTransformSyncSystem (Engine::Core &core)
 Sync wheel entity transforms with Jolt vehicle wheel positions.

Function Documentation

◆ CharacterControllerUpdate()

void Physics::System::CharacterControllerUpdate ( Engine::Core & core)

Update the character controller system. This will process all entities with character controllers and update their state based on input and physics interactions.

Parameters
coreThe engine core to update the system with.

◆ CreateConstraintGeneric()

template<Component::ConstraintType TYPE, typename CompT, typename SettingsT, typename Configurator, typename ExtraValidate>
void Physics::System::CreateConstraintGeneric ( Engine::Core::Registry & registry,
Engine::EntityId entity,
const char * constraintName,
Configurator && configurator,
ExtraValidate && extraValidate )
static

◆ CreateConvexHullFromMesh()

JPH::RefConst< JPH::Shape > Physics::System::CreateConvexHullFromMesh ( const Object::Component::Mesh & mesh,
const Component::ConvexHullMeshCollider * meshCollider,
const glm::vec3 & scale )
static

Create a ConvexHullShape from mesh vertices.

Parameters
meshThe mesh component containing vertices
meshColliderPointer to ConvexHullMeshCollider settings (nullable). If null, default settings are used.
scaleScale to apply to the mesh vertices (from Transform component)
Returns
RefConst to the created shape, or nullptr on failure

◆ CreateJoltConstraint()

template<typename SettingsT, typename ConstraintT>
JPH::Constraint * Physics::System::CreateJoltConstraint ( ConstraintContext & ctx,
SettingsT & joltSettings,
const ConstraintT & constraint,
Component::RigidBodyInternal * internalA,
const char * constraintName )
static

◆ CreateJoltCreationSettings()

JPH::SoftBodyCreationSettings Physics::System::CreateJoltCreationSettings ( const Component::SoftBody & softBody,
const JPH::Ref< JPH::SoftBodySharedSettings > & sharedSettings,
const JPH::RVec3 & position,
const JPH::Quat & rotation )
static

Create Jolt SoftBodyCreationSettings from component.

◆ CreateJoltSharedSettings()

CreateSettingsResult Physics::System::CreateJoltSharedSettings ( const Component::SoftBody & softBody,
const Object::Component::Mesh & mesh,
const glm::vec3 & scale = glm::vec3(1.0f) )
static

Convert SoftBody component to Jolt SoftBodySharedSettings.

Parameters
softBodyThe soft body component with settings and physics data
meshThe mesh containing vertices and face indices
scaleThe scale factor from Transform to apply to vertices (default: 1.0)
Returns
CreateSettingsResult with settings and vertex mapping
Note
The scale is applied to mesh vertices before creating the Jolt soft body. This allows users to use Transform.scale to resize soft bodies without manually scaling the mesh vertices.

◆ CreateJoltWheelSettings()

void Physics::System::CreateJoltWheelSettings ( JPH::WheelSettingsWV & joltWheel,
const Component::WheelSettings & wheelSettings,
const glm::vec3 & position,
bool isRear )
static

Create Jolt wheel settings from Physics wheel configuration.

◆ CreateMeshShapeFromMesh()

JPH::RefConst< JPH::Shape > Physics::System::CreateMeshShapeFromMesh ( const Object::Component::Mesh & mesh,
const Component::MeshCollider * meshCollider,
const glm::vec3 & scale )
static

Create a MeshShape from mesh vertices and indices.

Parameters
meshThe mesh component containing vertices and indices
meshColliderPointer to MeshCollider settings (nullable)
scaleScale to apply to the mesh vertices (from Transform component)
Returns
RefConst to the created shape, or nullptr on failure

◆ CreateShapeFromColliders()

JPH::RefConst< JPH::Shape > Physics::System::CreateShapeFromColliders ( Engine::Core::Registry & registry,
Engine::EntityId entity )
static

Create a Jolt shape from collider components.

Returns
Shared pointer to shape, or nullptr if no collider found
Note
Priority order when multiple colliders exist:
  1. SphereCollider
  2. CapsuleCollider
  3. BoxCollider
  4. MeshCollider (requires Object::Mesh component)
If no collider is found, it will default to the MeshCollider with default settings, which can be pretty heavy. Make sure to always use the most appropriate colliders for RigidBodies.

◆ DeduplicateMesh()

DeduplicatedMesh Physics::System::DeduplicateMesh ( const Object::Component::Mesh & mesh)
static

Convert a potentially "flat" mesh (with duplicated vertices) to an indexed mesh with unique vertices.

OBJLoader creates meshes where each face has its own copy of vertices, resulting in indices like [0, 1, 2, 3, 4, 5, ...] where vertices are duplicated. Jolt SoftBody needs a proper indexed mesh with shared vertices for constraint creation.

Parameters
meshThe input mesh (potentially with duplicated vertices)
Returns
DeduplicatedMesh with unique vertices and proper indices

◆ DestroyConstraint()

void Physics::System::DestroyConstraint ( Engine::Core::Registry & registry,
Engine::EntityId entity,
const char * constraintName )

◆ FinalizeConstraint()

void Physics::System::FinalizeConstraint ( ConstraintContext & ctx,
Engine::Entity entity,
JPH::Constraint * joltConstraint,
Component::ConstraintType type,
const Component::ConstraintSettings & settings,
const char * constraintName )

◆ GenerateEdgesFromFaces()

std::vector< std::pair< uint32_t, uint32_t > > Physics::System::GenerateEdgesFromFaces ( const std::vector< uint32_t > & faceIndices)
static

Generate edge constraints from triangle face indices.

This function takes triangle indices and creates unique edge pairs for soft body constraint creation.

Parameters
faceIndicesTriangle indices (size must be multiple of 3)
Returns
Vector of unique edge pairs (vertex index A, vertex index B)

◆ GetBodyInternal()

Component::RigidBodyInternal * Physics::System::GetBodyInternal ( Engine::Entity entity,
const char * constraintName,
const char * bodyName )

◆ InitCharacterControllerSystem()

void Physics::System::InitCharacterControllerSystem ( Engine::Core & core)

Initialize the character controller system. This will set up necessary resources and state for character controllers to function.

Parameters
coreThe engine core to initialize the system with.

◆ InitConstraintSystem()

◆ InitJoltPhysics()

void Physics::System::InitJoltPhysics ( Engine::Core & core)

Init internal elements needed by JoltPhysics.

Parameters
corecore
Note
To be used with the "Startup" scheduler.

◆ InitPhysicsManager()

void Physics::System::InitPhysicsManager ( Engine::Core & core)

Init the PhysicsManager.

Parameters
corecore
Note
To be used with the "Startup" scheduler.

◆ InitRigidBodySystem()

void Physics::System::InitRigidBodySystem ( Engine::Core & core)

Initialize RigidBody system and register entt hooks.

This system sets up the entt hooks for RigidBody component:

  • on_construct: Creates Jolt body, adds RigidBodyInternal component
  • on_destroy: Removes Jolt body, cleans up RigidBodyInternal
Parameters
coreThe engine core
Note
To be used with the "Startup" scheduler
Examples
/home/runner/work/EngineSquared/EngineSquared/src/plugin/physics/src/system/ConstraintSystem.hpp.

◆ InitSoftBodySystem()

void Physics::System::InitSoftBodySystem ( Engine::Core & core)

Initialize the soft body system.

Registers entt hooks for SoftBody component creation and destruction. Should be called during physics plugin initialization.

Parameters
coreReference to the engine core

◆ InitVehicleSystem()

void Physics::System::InitVehicleSystem ( Engine::Core & core)

Initialize the vehicle system with entt lifecycle hooks.

Registers callbacks for Vehicle component construction and destruction. Must be called during Engine startup (Scheduler::Startup).

◆ OnCharacterControllerConstruct()

void Physics::System::OnCharacterControllerConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

◆ OnCharacterControllerDestroy()

void Physics::System::OnCharacterControllerDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

◆ OnDistanceConstraintConstruct()

void Physics::System::OnDistanceConstraintConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entity )
static

◆ OnDistanceConstraintDestroy()

void Physics::System::OnDistanceConstraintDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entity )
static

◆ OnFixedConstraintConstruct()

void Physics::System::OnFixedConstraintConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entity )
static

◆ OnFixedConstraintDestroy()

void Physics::System::OnFixedConstraintDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entity )
static

◆ OnPointConstraintConstruct()

void Physics::System::OnPointConstraintConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entity )
static

◆ OnPointConstraintDestroy()

void Physics::System::OnPointConstraintDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entity )
static

◆ OnRigidBodyConstruct()

void Physics::System::OnRigidBodyConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

Called when RigidBody component is added to an entity.

Note
This creates:
  1. Default collider if no collider exists
  2. Jolt physics body with all properties
  3. RigidBodyInternal component with BodyID

◆ OnRigidBodyDestroy()

void Physics::System::OnRigidBodyDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

Called when RigidBody component is removed from an entity.

Note
This destroys:
  1. Jolt physics body
  2. RigidBodyInternal component
Colliders are NOT automatically removed (user may want to keep them)

◆ OnRigidBodyInternalConstruct()

void Physics::System::OnRigidBodyInternalConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

◆ OnRigidBodyInternalDestroy()

void Physics::System::OnRigidBodyInternalDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

◆ OnSoftBodyConstruct()

void Physics::System::OnSoftBodyConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

◆ OnSoftBodyDestroy()

void Physics::System::OnSoftBodyDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

◆ OnSoftBodyInternalConstruct()

void Physics::System::OnSoftBodyInternalConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

◆ OnSoftBodyInternalDestroy()

void Physics::System::OnSoftBodyInternalDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

◆ OnVehicleConstruct()

void Physics::System::OnVehicleConstruct ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

Called when Vehicle component is added to an entity.

Creates:

  1. Jolt WheeledVehicleController with all settings
  2. VehicleConstraint attached to chassis
  3. VehicleInternal component with constraint data

Note: Jolt's WheeledVehicle does NOT use separate physics bodies for wheels. Wheels are simulated via raycasts/shape casts from the chassis.

◆ OnVehicleDestroy()

void Physics::System::OnVehicleDestroy ( Engine::Core::Registry & registry,
Engine::EntityId entityId )
static

Called when Vehicle component is removed from an entity.

Destroys:

  1. Jolt VehicleConstraint
  2. VehicleInternal component

◆ PhysicsUpdate()

void Physics::System::PhysicsUpdate ( Engine::Core & core)

Applies every update of the physics system.

After physics update, flush any buffered contact events from worker threads to the EventManager in the core.

Parameters
corecore
Note
To be used with the "RelativeTimeUpdate" scheduler.

◆ ShutdownCharacterControllerSystem()

void Physics::System::ShutdownCharacterControllerSystem ( Engine::Core & core)

Shutdown the character controller system. This will clean up any resources used by the system.

Parameters
coreThe engine core to shut down the system from.

◆ ShutdownRigidBodySystem()

void Physics::System::ShutdownRigidBodySystem ( Engine::Core & core)

Shutdown RigidBody system and unregister entt hooks.

This system disconnects the entt hooks for RigidBody component.

Parameters
coreThe engine core
Note
To be used with the "Shutdown" scheduler

◆ ShutdownSoftBodySystem()

void Physics::System::ShutdownSoftBodySystem ( Engine::Core & core)

Shutdown the soft body system.

Cleans up any remaining soft bodies.

Parameters
coreReference to the engine core

◆ SyncSoftBodyVertices()

void Physics::System::SyncSoftBodyVertices ( Engine::Core & core)

Synchronize soft body vertex positions to mesh data.

Updates vertex positions from Jolt soft body simulation. Should be called after physics update.

Parameters
coreReference to the engine core

◆ SyncTransformWithPhysics()

void Physics::System::SyncTransformWithPhysics ( Engine::Core & core)

Synchronize Transform components with Jolt Physics body positions.

This system should run in the FixedTimeUpdate scheduler AFTER PhysicsUpdate. It reads the position and rotation from Jolt bodies and updates the corresponding Transform components.

Only dynamic and kinematic bodies are synchronized (static bodies don't move).

Parameters
coreThe engine core
Note
To be used with the "FixedTimeUpdate" scheduler

◆ ValidateConstraint()

template<typename ConstraintT>
bool Physics::System::ValidateConstraint ( const ConstraintT & constraint,
const char * constraintName )
static

◆ VehicleControlSystem()

void Physics::System::VehicleControlSystem ( Engine::Core & core)

Apply vehicle controller inputs to Jolt vehicle constraints.

Should run every physics tick (FixedTimeUpdate scheduler). Reads VehicleController component values and applies them to the WheeledVehicleController.

◆ VehicleRPMUpdate()

void Physics::System::VehicleRPMUpdate ( Engine::Core & core)

Update per-vehicle RPM telemetry from Jolt controller. Runs in FixedTimeUpdate.

◆ WheelTransformSyncSystem()

void Physics::System::WheelTransformSyncSystem ( Engine::Core & core)

Sync wheel entity transforms with Jolt vehicle wheel positions.

Should run every physics tick (FixedTimeUpdate scheduler) AFTER PhysicsUpdate. Updates the Transform component of each wheel entity to match the computed wheel position and rotation from the vehicle constraint.