Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Physics::Component::SoftBody Struct Reference

Soft body physics component. More...

#include <SoftBody.hpp>

Public Member Functions

size_t GetVertexCount () const
 Get number of vertices (from invMasses size).
size_t GetEdgeCount () const
 Get number of edge constraints.
void PinVertex (uint32_t vertexIndex)
 Pin a vertex (fix it in space).
void UnpinVertex (uint32_t vertexIndex, float mass=1.0f)
 Unpin a vertex.
bool IsVertexPinned (uint32_t vertexIndex) const
 Check if a vertex is pinned.
bool IsValid () const
 Check if the soft body configuration is valid.
 SoftBody ()=default
 Default constructor - creates an empty SoftBody.
 SoftBody (const SoftBodySettings &settings)
 Construct with specific settings.
 SoftBody (SoftBodyType bodyType, const SoftBodySettings &settings)
 Construct with type and settings.

Public Attributes

SoftBodyType type = SoftBodyType::Custom
 Type of soft body (affects default settings).
SoftBodySettings settings
 Simulation settings.
std::vector< float > invMasses
 Vertex inverse masses (0 = pinned/fixed). Size must match Mesh.vertices.size().
std::vector< uint32_t > pinnedVertices
 Indices of pinned (fixed) vertices.
std::vector< std::pair< uint32_t, uint32_t > > edges

Detailed Description

Soft body physics component.

Represents the physics simulation data for a deformable object using Jolt's SoftBody system. This component works in conjunction with Object::Component::Mesh which holds the geometry.

Architecture

  • Mesh (Object plugin): Contains vertices[], normals[], indices[] for rendering
  • SoftBody (Physics plugin): Contains physics settings and per-vertex physics data
  • Jolt: Maintains internal vertex copy for simulation (unavoidable)

The SoftBodySystem:

  1. On construct: Auto-detects Mesh component and initializes physics data
  2. On update: Writes Jolt simulation results back to Mesh.vertices

Usage

From existing Mesh (e.g., imported .obj):

auto mesh = OBJLoader("model.obj").GetMesh();
auto teapot = core.CreateEntity();
teapot.AddComponent<Transform>(core, position);
teapot.AddComponent<Mesh>(core, mesh);
teapot.AddComponent<SoftBody>(core, SoftBodySettings::Balloon(5000.0f));
static SoftBodySettings Balloon(float pressure=1000.0f)
Settings for pressure-based soft bodies (balloons).
Definition SoftBody.hpp:164
SoftBody()=default
Default constructor - creates an empty SoftBody.

Procedural cloth (using Object::Helper):

auto cloth = Object::Helper::CreateCloth(core, 10, 10, 0.1f, position);
auto& soft = cloth.AddComponent<SoftBody>(core, SoftBodySettings::Cloth(0.5f));
soft.PinVertex(0); // Pin top-left corner
soft.PinVertex(9); // Pin top-right corner
Engine::Entity CreateCloth(Engine::Core &core, CreateClothInfo info)
Definition CreateShape.cpp:75
static SoftBodySettings Cloth(float stiffness=0.5f)
Settings optimized for cloth simulation.
Definition SoftBody.hpp:121

Note: Collider components (BoxCollider, SphereCollider, etc.) are ignored for SoftBody. Use SoftBodySettings::vertexRadius for collision detection.

See also
Object::Component::Mesh
Object::Helper::CreateCloth
Object::Helper::CreateRope
Examples
Creating a 2-unit jelly cube with 5x5x5 grid:, Creating a cloth for soft body simulation:, and Creating a rope for soft body simulation:.

Constructor & Destructor Documentation

◆ SoftBody() [1/3]

Physics::Component::SoftBody::SoftBody ( )
default

Default constructor - creates an empty SoftBody.

The SoftBody will be configured automatically when added to an entity that already has a Mesh component. The InitSoftBodySystem hook will:

  • Auto-detect the Mesh and initialize invMasses
  • Generate edge constraints from mesh faces

◆ SoftBody() [2/3]

Physics::Component::SoftBody::SoftBody ( const SoftBodySettings & settings)
inlineexplicit

Construct with specific settings.

Parameters
settingsSimulation settings (use factory methods like SoftBodySettings::Cloth())

◆ SoftBody() [3/3]

Physics::Component::SoftBody::SoftBody ( SoftBodyType bodyType,
const SoftBodySettings & settings )
inline

Construct with type and settings.

Parameters
bodyTypeType of soft body (affects internal processing)
settingsSimulation settings

Member Function Documentation

◆ GetEdgeCount()

size_t Physics::Component::SoftBody::GetEdgeCount ( ) const
inlinenodiscard

Get number of edge constraints.

◆ GetVertexCount()

size_t Physics::Component::SoftBody::GetVertexCount ( ) const
inlinenodiscard

Get number of vertices (from invMasses size).

◆ IsValid()

bool Physics::Component::SoftBody::IsValid ( ) const
inlinenodiscard

Check if the soft body configuration is valid.

Note
Only checks if vertex data has been initialized

◆ IsVertexPinned()

bool Physics::Component::SoftBody::IsVertexPinned ( uint32_t vertexIndex) const
inlinenodiscard

Check if a vertex is pinned.

◆ PinVertex()

void Physics::Component::SoftBody::PinVertex ( uint32_t vertexIndex)
inline

Pin a vertex (fix it in space).

Parameters
vertexIndexIndex of vertex to pin
Examples
Creating a cloth for soft body simulation:, and Creating a rope for soft body simulation:.

◆ UnpinVertex()

void Physics::Component::SoftBody::UnpinVertex ( uint32_t vertexIndex,
float mass = 1.0f )
inline

Unpin a vertex.

kMinMass is used to avoid instability from huge inverse masses.

Parameters
vertexIndexIndex of vertex to unpin
massMass to assign (default 1.0)

Member Data Documentation

◆ edges

std::vector<std::pair<uint32_t, uint32_t> > Physics::Component::SoftBody::edges

Edge constraints (pairs of vertex indices) for rope/chain without faces Auto-generated from Mesh.indices if empty

◆ invMasses

std::vector<float> Physics::Component::SoftBody::invMasses

Vertex inverse masses (0 = pinned/fixed). Size must match Mesh.vertices.size().

◆ pinnedVertices

std::vector<uint32_t> Physics::Component::SoftBody::pinnedVertices

Indices of pinned (fixed) vertices.

◆ settings

SoftBodySettings Physics::Component::SoftBody::settings

Simulation settings.

◆ type

SoftBodyType Physics::Component::SoftBody::type = SoftBodyType::Custom

Type of soft body (affects default settings).


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