Kemena3D
Loading...
Searching...
No Matches
kemena::kPhysicsManager Class Reference

Owns the Jolt PhysicsSystem and manages the lifecycle of all physics bodies. More...

#include <kphysicsmanager.h>

Public Member Functions

 kPhysicsManager ()
 Constructs an uninitialised manager; call init() before use.
 ~kPhysicsManager ()
 Destroys the manager, calling shutdown() to release all bodies.
bool init ()
 Initialises the Jolt physics engine and internal systems.
void shutdown ()
 Destroys all physics bodies and tears down the engine. Called automatically by the destructor.
void update (float deltaTime)
 Advances the simulation by deltaTime seconds.
void setGravity (const kVec3 &gravity)
 Sets the global gravity vector (m/s²).
kVec3 getGravity () const
 Returns the current global gravity vector (m/s²).
kPhysicsObjectcreateObject (const kPhysicsObjectDesc &desc)
 Creates a physics body and returns a new kPhysicsObject.
void destroyObject (kPhysicsObject *object)
 Removes a physics body from the simulation and destroys the object.
kCharacterControllercreateCharacter (const kCharacterControllerDesc &desc)
 Creates a character controller in this physics world.
void destroyCharacter (kCharacterController *character)
 Removes a character controller from the simulation and destroys it.
kPhysicsRaycastHit raycast (const kVec3 &origin, const kVec3 &direction, float maxDistance=1000.0f)
 Casts a ray into the physics world and returns the closest hit.

Detailed Description

Owns the Jolt PhysicsSystem and manages the lifecycle of all physics bodies.

Create one kPhysicsManager per scene. Call init() once, then step the simulation each frame with update(). After each update, sync moving objects back to their scene nodes via kObject::syncFromPhysics().

// Create a dynamic box
desc.shape.halfExtents = kVec3(1.f, 1.f, 1.f);
desc.position = kVec3(0.f, 10.f, 0.f);
kPhysicsObject* box = physics->createObject(desc);
// Attach to a scene node
myObject->attachPhysics(box);
// Game loop:
physics->update(deltaTime);
myObject->syncFromPhysics();
kPhysicsManager()
Constructs an uninitialised manager; call init() before use.
void update(float deltaTime)
Advances the simulation by deltaTime seconds.
kPhysicsObject * createObject(const kPhysicsObjectDesc &desc)
Creates a physics body and returns a new kPhysicsObject.
Represents a single rigid body or trigger volume inside the physics simulation.
Definition kphysicsobject.h:131
@ Box
Axis-aligned box defined by half-extents.
Definition kphysicsobject.h:25
KEMENA3D_API kPhysicsManager * createPhysicsManager()
Create and initialise the physics manager.
glm::vec3 kVec3
3-component float vector.
Definition kdatatype.h:33
All parameters needed to create a kPhysicsObject.
Definition kphysicsobject.h:92
kVec3 position
Definition kphysicsobject.h:95
kPhysicsShapeDesc shape
Definition kphysicsobject.h:93
kVec3 halfExtents
Box / Plane: per-axis half-extents (Plane uses x,z).
Definition kphysicsobject.h:59
kPhysicsShapeType type
Definition kphysicsobject.h:58

Constructor & Destructor Documentation

◆ kPhysicsManager()

kemena::kPhysicsManager::kPhysicsManager ( )

Constructs an uninitialised manager; call init() before use.

◆ ~kPhysicsManager()

kemena::kPhysicsManager::~kPhysicsManager ( )

Destroys the manager, calling shutdown() to release all bodies.

Member Function Documentation

◆ createCharacter()

kCharacterController * kemena::kPhysicsManager::createCharacter ( const kCharacterControllerDesc & desc)

Creates a character controller in this physics world.

The returned pointer is owned by the manager; release it with destroyCharacter(). Each character's ground state is refreshed inside update() after the world steps.

Parameters
descCapsule + motion parameters.
Returns
Pointer to the new kCharacterController, or nullptr on failure.

◆ createObject()

kPhysicsObject * kemena::kPhysicsManager::createObject ( const kPhysicsObjectDesc & desc)

Creates a physics body and returns a new kPhysicsObject.

The returned pointer is owned by this manager. Release it with destroyObject() rather than deleting it directly.

Parameters
descFull shape and motion parameters.
Returns
Pointer to the new kPhysicsObject, or nullptr on failure.

◆ destroyCharacter()

void kemena::kPhysicsManager::destroyCharacter ( kCharacterController * character)

Removes a character controller from the simulation and destroys it.

Parameters
characterPointer returned by createCharacter().

◆ destroyObject()

void kemena::kPhysicsManager::destroyObject ( kPhysicsObject * object)

Removes a physics body from the simulation and destroys the object.

Parameters
objectPointer returned by createObject().

◆ getGravity()

kVec3 kemena::kPhysicsManager::getGravity ( ) const

Returns the current global gravity vector (m/s²).

◆ init()

bool kemena::kPhysicsManager::init ( )

Initialises the Jolt physics engine and internal systems.

Returns
true on success.

◆ raycast()

kPhysicsRaycastHit kemena::kPhysicsManager::raycast ( const kVec3 & origin,
const kVec3 & direction,
float maxDistance = 1000.0f )

Casts a ray into the physics world and returns the closest hit.

Intended for game-play use — requires objects to have physics bodies attached (created via createObject()). For editor picking without physics bodies, use kRenderer::pickObject() instead.

kVec3 origin, dir;
camera->screenToRay(mouseX, mouseY, vpW, vpH, origin, dir);
auto hit = physicsManager->raycast(origin, dir, 1000.0f);
if (hit.hit)
myObject->setPosition(hit.hitPoint);
Parameters
originRay origin in world space.
directionNormalised ray direction in world space.
maxDistanceMaximum distance along the ray to test.
Returns
kPhysicsRaycastHit with hit == false if no body was struck.

◆ setGravity()

void kemena::kPhysicsManager::setGravity ( const kVec3 & gravity)

Sets the global gravity vector (m/s²).

Parameters
gravityAcceleration vector — default is kVec3(0, -9.81, 0).

◆ shutdown()

void kemena::kPhysicsManager::shutdown ( )

Destroys all physics bodies and tears down the engine. Called automatically by the destructor.

◆ update()

void kemena::kPhysicsManager::update ( float deltaTime)

Advances the simulation by deltaTime seconds.

Parameters
deltaTimeTime since the last frame in seconds. Call this once per game-loop iteration before reading body transforms.

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