Avara3D 0.2.0
C++ API reference
PhysicsWorld.h
1//
2// PhysicsWorld.h
3// avara3d
4//
5// Created by Morgan Davis on 1/26/18.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_PHYSICS_PHYSICSWORLD_H
10#define AVARA3D_PHYSICS_PHYSICSWORLD_H
11
12#include <functional>
13#include <memory>
14#include <optional>
15#include <vector>
16
17#include "a3d/Math.h"
18#include "a3d/scene/HitTestResult.h"
19
20namespace a3d {
21
22 class HitTestResult;
23 class Line;
24 class PhysicsBody;
25 class PhysicsContact;
26 class PhysicsShape;
27 class PhysicsWorldProxy;
28 class Profiler;
29 class Scene;
30
40
41 public:
42 // [Public Types]
43
47 HitTestSearchMode searchMode {HitTestSearchMode::Closest};
48 };
49
53 HitTestSearchMode searchMode {HitTestSearchMode::Closest};
54 };
55
63 std::function<void(PhysicsWorld& physicsWorld, const PhysicsContact& contact)>;
64
71 std::function<void(PhysicsWorld& physicsWorld, const PhysicsContact& contact)>;
72
81 std::function<void(PhysicsWorld& physicsWorld, const PhysicsContact& contact)>;
82
83 // [Public Lifecycle Functions]
84
87
88 PhysicsWorld(const PhysicsWorld&) = delete;
89 PhysicsWorld& operator=(const PhysicsWorld&) = delete;
90
91 PhysicsWorld(PhysicsWorld&&) = delete;
92 PhysicsWorld& operator=(PhysicsWorld&&) = delete;
93
95
96 // [Public Member Functions]
97
99 const math::vec3& gravity() const;
100
103
113 std::optional<PhysicsContact> contactTest(const PhysicsBody& bodyA, const PhysicsBody& bodyB) const;
114
120 std::vector<PhysicsContact> contactTest(const PhysicsBody& body) const;
121
128 std::vector<HitTestResult> rayTest(const math::vec3& from,
129 const math::vec3& to,
130 const RayTestOptions& options) const;
131
137 std::vector<HitTestResult> rayTest(const math::vec3& from, const math::vec3& to) const;
138
139 // ! NOT IMPLEMENTED !
145 std::vector<PhysicsContact> convexSweepTest(const PhysicsShape& shape,
146 const math::mat4& fromMat,
147 const math::mat4& toMat,
148 const ConvexSweepTestOptions& options) const;
149
150 // ! NOT IMPLEMENTED !
156 std::vector<PhysicsContact> convexSweepTest(const PhysicsShape& shape,
157 const math::mat4& fromMat,
158 const math::mat4& toMat) const;
159
161 Scene* scene() const;
162
165
168
171
174
177
180
181 // [Internal Types]
182
183 struct Inventory {
184 unsigned staticBodies {0};
185 unsigned dynamicBodies {0};
186 unsigned kinematicBodies {0};
187 unsigned primitiveShapes {0};
188 unsigned boundingBoxShapes {0};
189 unsigned convexHullShapes {0};
190 unsigned concavePolyhedronShapes {0};
191 unsigned activeContacts {0};
192 };
193
194 // [Internal Member Functions]
195
196 void attachedToScene(Scene& scene);
197 void detachedFromScene(Scene& scene);
198
199 void add(PhysicsBody& body);
200 void remove(PhysicsBody& body);
201
202 void step(double deltaTime, Profiler& profiler);
203
204 Inventory inventory() const;
205
206 void appendDebugLines(std::vector<Line>& out) const;
207
208 PhysicsWorldProxy* proxy() const;
209
210 private:
211 // [Private Member Variables]
212
213 math::vec3 _gravity;
214 std::unique_ptr<PhysicsWorldProxy> _proxy;
215 Scene* _scene;
216 DidBeginContactCallback _didBeginContactCallback;
217 DidContinueContactCallback _didContinueContactCallback;
218 DidEndContactCallback _didEndContactCallback;
219 };
220
221}
222
223#endif // AVARA3D_PHYSICS_PHYSICSWORLD_H
Rigid-body simulation state attached to a Node.
Definition: PhysicsBody.h:39
Describes a contact between two physics bodies.
Describes collision geometry used by one or more PhysicsBody objects.
Definition: PhysicsShape.h:37
Simulates rigid bodies and performs physics collision queries for a Scene.
Definition: PhysicsWorld.h:39
std::optional< PhysicsContact > contactTest(const PhysicsBody &bodyA, const PhysicsBody &bodyB) const
Tests for contact between bodyA and bodyB.
Scene * scene() const
Returns the Scene containing this PhysicsWorld, or nullptr when it is not installed in a Scene.
DidBeginContactCallback didBeginContactCallback() const
Returns the callback invoked when a body pair begins contact.
void didEndContactCallback(DidEndContactCallback function)
Replaces the end-contact callback; an empty callback disables it.
void didContinueContactCallback(DidContinueContactCallback function)
Replaces the continuing-contact callback; an empty callback disables it.
void didBeginContactCallback(DidBeginContactCallback function)
Replaces the begin-contact callback; an empty callback disables it.
const math::vec3 & gravity() const
Returns the world gravity acceleration vector.
DidContinueContactCallback didContinueContactCallback() const
Returns the callback invoked while a body pair remains in contact.
std::function< void(PhysicsWorld &physicsWorld, const PhysicsContact &contact)> DidBeginContactCallback
Callback invoked when a pair of physics bodies begins a logical contact.
Definition: PhysicsWorld.h:63
std::vector< PhysicsContact > convexSweepTest(const PhysicsShape &shape, const math::mat4 &fromMat, const math::mat4 &toMat) const
Sweeps shape between two transforms.
std::vector< HitTestResult > rayTest(const math::vec3 &from, const math::vec3 &to) const
Tests the world-space segment from from to to and returns the closest physics hit.
PhysicsWorld()
Creates a PhysicsWorld with Earth-like gravity of (0, -9.807, 0).
std::vector< PhysicsContact > convexSweepTest(const PhysicsShape &shape, const math::mat4 &fromMat, const math::mat4 &toMat, const ConvexSweepTestOptions &options) const
Sweeps shape between two transforms.
std::vector< PhysicsContact > contactTest(const PhysicsBody &body) const
Returns current contacts between body and other physics bodies.
std::function< void(PhysicsWorld &physicsWorld, const PhysicsContact &contact)> DidEndContactCallback
Callback invoked when a tracked body pair ceases to be in contact.
Definition: PhysicsWorld.h:81
DidEndContactCallback didEndContactCallback() const
Returns the callback invoked when a tracked body pair ends contact.
std::function< void(PhysicsWorld &physicsWorld, const PhysicsContact &contact)> DidContinueContactCallback
Callback invoked once per simulation step while a body pair remains in contact.
Definition: PhysicsWorld.h:71
void gravity(const math::vec3 &gravity)
Sets the world gravity and immediately applies it to affected dynamic bodies.
std::vector< HitTestResult > rayTest(const math::vec3 &from, const math::vec3 &to, const RayTestOptions &options) const
Tests the world-space segment from from to to against physics bodies.
Owns a scene graph and the worlds used to simulate and render it.
Definition: Scene.h:44
Options controlling a convex sweep collision query.
Definition: PhysicsWorld.h:51
HitTestSearchMode searchMode
How matching sweep hits are selected.
Definition: PhysicsWorld.h:53
Options controlling a ray collision query.
Definition: PhysicsWorld.h:45
HitTestSearchMode searchMode
How matching ray hits are selected.
Definition: PhysicsWorld.h:47