Skip to content

Engine → Physics → physics_world.h

physics_world

Syntax

class CONJURE_API physics_world

Constructors

AccessSignatureDescription
publicphysics_world( const physics_settings &settings, core::jobs::job_system &jobs);Builds the Jolt system from validated settings (physics_settings::validate first; an invalid value is clamped with one ERROR) and sizes every buffer from them. Never fails.

Important

Copy constructor and assignment has been deleted. Move constructors have been deleted. Move assignment has been deleted.

Destructors

AccessSignatureDescription
public~physics_world();Detaches when attached, destroys every body and releases the process-wide Jolt state.

Variables

AccessNameTypeDefaultDescription
privatemStatestd::unique_ptr<state>{}

Functions

Public

active_body_count

[[nodiscard]] u32 active_body_count() const noexcept;

The dynamic and kinematic bodies that are awake, the inner bodies of characters among them. Static bodies never count, although every static trigger costs the step what an awake body costs: Jolt keeps it active so that it sees sleeping bodies (contact_events.h).

add_angular_impulse

[[nodiscard]] core::result<void> add_angular_impulse(
    ecs::entity target,
    const math::vec3 &impulse);

An angular impulse. DYNAMIC bodies only, as add_force.

add_force

[[nodiscard]] core::result<void> add_force(
    ecs::entity target,
    const math::vec3 &force);

A force at the centre of mass for the next fixed step. DYNAMIC bodies only: WRONG_BODY_KIND for a static or kinematic body and for a character.

add_force_at

[[nodiscard]] core::result<void> add_force_at(
    ecs::entity target,
    const math::vec3 &force,
    const math::vec3 &point);

A force at a world point for the next fixed step. DYNAMIC bodies only, as add_force.

add_impulse

[[nodiscard]] core::result<void> add_impulse(
    ecs::entity target,
    const math::vec3 &impulse);

An impulse at the centre of mass. DYNAMIC bodies only, as add_force.

add_impulse_at

[[nodiscard]] core::result<void> add_impulse_at(
    ecs::entity target,
    const math::vec3 &impulse,
    const math::vec3 &point);

An impulse at a world point. DYNAMIC bodies only, as add_force.

add_torque

[[nodiscard]] core::result<void> add_torque(
    ecs::entity target,
    const math::vec3 &torque);

A torque for the next fixed step. DYNAMIC bodies only, as add_force.

attach

[[nodiscard]] core::result<void> attach(
    ecs::world &target);

Installs the component observers and queues every entity that already carries a body component. ALREADY_ATTACHED; INVALID_STATE when the registry lacks the physics or scene components (register_components / scene::register_components first); WRONG_THREAD (and an assert) off the owner thread.

body_count

[[nodiscard]] u32 body_count() const noexcept;

body_pose

[[nodiscard]] std::optional<math::transform> body_pose(
    ecs::entity target) const noexcept;

The body's pose straight from Jolt (unit scale), nullopt without a body or while a push, step or pull runs.

bounds

[[nodiscard]] math::aabb bounds() const noexcept;

Bounds of every body (PhysicsSystem::GetBounds; invalid when empty).

character_ground

[[nodiscard]] std::optional<ground_state> character_ground(
    ecs::entity target) const noexcept;

The ground state of a character, nullopt for anything else.

character_move

[[nodiscard]] core::result<void> character_move(
    ecs::entity target,
    const math::vec3 &velocity);

Sets the desired velocity of a character for the next step (it replaces the component's velocity for that step; the pull then writes the achieved velocity back). Like the component's velocity it is the character's own: the step adds the horizontal velocity of the ground that carries the character (character_controller.h). NOT_A_CHARACTER, in place of NO_BODY and WRONG_BODY_KIND, when the entity has no body and is not pending or has a body that is no character; the other refusals are those of the body commands, in their order.

collect_contact_events

void collect_contact_events();

Merges and sorts the step's raw contacts into contact_events().

collect_shapes

void collect_shapes();

Drops cached shapes no body references.

contact_events

[[nodiscard]] std::span<const contact_event> contact_events() const noexcept;

The events of the last step, valid until the next push_transforms.

debug_draw

void debug_draw(
    physics_debug_draw &sink,
    physics_debug_draw_flags flags) const;

Draws the bodies into the sink. Draws nothing when Jolt was built without its debug renderer (Distribution).

detach

void detach();

Removes the observers, destroys every body and removes the interpolated_transform components physics added; pending synthesised exits are discarded. The ECS world must not be locked.

gravity

[[nodiscard]] math::vec3 gravity() const noexcept;

The gravity in effect (after the last applied set_gravity, else settings().gravity).

has_body

[[nodiscard]] bool has_body(
    ecs::entity target) const noexcept;

True when the entity has a body or a character now; false while it is still pending.

is_attached

[[nodiscard]] bool is_attached() const noexcept;

is_sleeping

[[nodiscard]] bool is_sleeping(
    ecs::entity target) const noexcept;

True when the body is inactive, which a static body always is (a static trigger too); false when the entity has no body.

layers

[[nodiscard]] const physics_layer_table &layers() const noexcept;

move_kinematic

[[nodiscard]] core::result<void> move_kinematic(
    ecs::entity target,
    const math::transform &pose);

MoveKinematic towards the pose over the next fixed step. KINEMATIC bodies without KINEMATIC_MOVES_WITH_TRANSFORM only: WRONG_BODY_KIND for a static or dynamic body, for a character, and for a kinematic body that follows its transform (write its local_transform).

on_asset_reloaded

void on_asset_reloaded(
    assets::asset_id id);

Marks every body whose colliders reference the asset for a shape rebuild.

overlap_box

[[nodiscard]] u32 overlap_box(
    const math::vec3 &center,
    const math::vec3 &halfExtents,
    const math::quat &rotation,
    std::span<ecs::entity> out,
    const query_filter &filter = {}) const;

Entities whose bodies overlap the oriented box, one entry per body. Returns the number written, at most out.size(); a result equal to out.size() may be truncated.

overlap_sphere

[[nodiscard]] u32 overlap_sphere(
    const math::vec3 &center,
    f32 radius,
    std::span<ecs::entity> out,
    const query_filter &filter = {}) const;

Entities whose bodies overlap the sphere, one entry per body. Returns the number written, at most out.size(); a result equal to out.size() may be truncated.

pull_transforms

void pull_transforms(
    ecs::system_context *context = nullptr);

Writes Jolt poses and velocities into local_transform, interpolated_transform, rigid_body and character_controller. With a system_context the chunks run through parallel_for_chunks; nullptr runs the loop inline on the caller.

push_transforms

void push_transforms(
    f32 fixedDeltaTime,
    ecs::command_buffer *deferred = nullptr);

Clears the previous step's events, creates pending bodies, applies rebuilds, pushes changed components and transforms into Jolt and applies the queued commands. deferred receives the interpolated_transform add/remove of bodies whose motion type changed; nullptr uses an internal buffer flushed when the world is unlocked.

raycast

[[nodiscard]] std::optional<raycast_hit> raycast(
    const math::ray &ray,
    f32 maxDistance,
    const query_filter &filter = {}) const;

Closest hit along the ray within maxDistance. maxDistance is clamped to 1e6 m (infinity means unlimited); a non-finite origin or direction hits nothing.

raycast_all

[[nodiscard]] u32 raycast_all(
    const math::ray &ray,
    f32 maxDistance,
    std::span<raycast_hit> out,
    const query_filter &filter = {}) const;

The out.size() nearest hits along the ray, sorted by distance. Returns the number written, at most out.size(); a result equal to out.size() may be truncated. maxDistance is clamped to 1e6 m (infinity means unlimited); a non-finite origin or direction hits nothing.

set_angular_velocity

[[nodiscard]] core::result<void> set_angular_velocity(
    ecs::entity target,
    const math::vec3 &velocity);

Sets the angular velocity. DYNAMIC and KINEMATIC bodies, as set_linear_velocity.

set_gravity

[[nodiscard]] core::result<void> set_gravity(
    const math::vec3 &gravity);

Queued PhysicsSystem::SetGravity at the next push.

set_linear_velocity

[[nodiscard]] core::result<void> set_linear_velocity(
    ecs::entity target,
    const math::vec3 &velocity);

Sets the linear velocity. DYNAMIC and KINEMATIC bodies: WRONG_BODY_KIND for a static body and for a character (character_move sets a character's velocity). A kinematic body that follows its transform takes it for one step: the next push moves it back to its local_transform.

settings

[[nodiscard]] const physics_settings &settings() const noexcept;

shape_cast

[[nodiscard]] std::optional<shape_cast_hit> shape_cast(
    const cast_shape_desc &shape,
    const math::transform &start,
    const math::vec3 &direction,
    f32 maxDistance,
    const query_filter &filter = {}) const;

Sweeps the shape from start along direction for maxDistance metres, as raycast casts its ray; the closest hit, with hit.distance = hit.fraction * maxDistance. direction is normalised (its length does not matter; a nearly zero one sweeps along -z, math::ray's default direction) and maxDistance is clamped to 1e6 m (infinity means unlimited). A maxDistance that is not positive (zero, negative or NaN) and a non-finite start or direction hit nothing. A shape that starts overlapping a body is reported at fraction 0 for some directions only, and the normal and penetrationDepth of that hit are approximate (often a depth of 0): the deepest penetration is not computed, and a sweep that leads away from the body can skip it as a back face. Test a start position with overlap_sphere or overlap_box instead.

sleep

[[nodiscard]] core::result<void> sleep(
    ecs::entity target);

Puts a body to sleep at the next push. Every kind of body takes it; it does nothing on a static body, which is never awake, and on a character, which never sleeps.

stats

[[nodiscard]] physics_stats stats() const noexcept;

step

void step(
    f32 fixedDeltaTime);

Updates the characters, then PhysicsSystem::Update once. Owner thread.

teleport

[[nodiscard]] core::result<void> teleport(
    ecs::entity target,
    const math::transform &pose);

Moves the body instantly; scale is ignored, velocities and the interpolated pose are reset. The entity's local_transform follows: at the next pull for bodies the pull writes, at the push that applies the command for static and KINEMATIC_MOVES_WITH_TRANSFORM bodies. Every kind of body takes it, characters included. A character keeps its own velocity (its component's) and loses what moving ground carried it with; the step after the teleport adds no horizontal ground velocity. The vertical part of that velocity is the ground's while it stands (character_controller.h), so a character teleported off ground that moves vertically takes that vertical velocity along.

validate

[[nodiscard]] core::result<void> validate() const;

Checks that rows, entities and Jolt agree; the message names the first violated invariant. UNKNOWN_LAYER for a body whose component names a missing layer.

wake

[[nodiscard]] core::result<void> wake(
    ecs::entity target);

Wakes a sleeping body at the next push. Every kind of body takes it; it does nothing on a static body, which is never awake (is_sleeping is true for it), and on a character, which never sleeps.