vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
physics_system.h
1#pragma once
2
3#include <cstdint>
4#include <utility>
5#include <vector>
6
7#include <glm/glm.hpp>
8
9#include "core/system.h"
10#include "debug/fault_latch.h"
11#include "ecs/entity.h"
12#include "system/physics/physics_internal.h"
13#include "system/physics/collision/contact.h"
14#include "system/physics/collision/narrowphase.h"
15#include "system/physics/collision/support.h"
16#include "system/physics/solver/joint_solver.h"
17#include "system/physics/solver/solver.h"
18#include "system/physics/solver/contact_cache.h"
19
20namespace Vkm::Engine {
21
22class NetSession;
23
24struct PhysicsSettings;
25
26class EventBus;
27class ResourceManager;
28class Scene;
29
41class PhysicsSystem : public System {
42 public:
43 PhysicsSystem() = default;
44 ~PhysicsSystem() override = default;
45
46 PhysicsSystem(const PhysicsSystem& other) = delete;
47 PhysicsSystem& operator=(const PhysicsSystem& other) = delete;
48
49 PhysicsSystem(PhysicsSystem && other) = delete;
50 PhysicsSystem& operator=(PhysicsSystem && other) = delete;
51
52 public:
53 void fixedUpdate(FrameContext& ctx) override;
54
60 bool isReplayed() const override { return true; }
61
62 private:
64 struct HeldJoint {
65 EntityId owner;
66 glm::vec3 impulse;
67 glm::vec3 holdImpulse;
68 };
69
70 private:
82 bool gatherBodies(Scene& scene, const ResourceManager& resources, const NetSession& net);
83
92 void integrateForces(const PhysicsSettings& physics, float dt);
93
97 void broadphase();
98
109 void narrowphase(bool replaying);
110
119 void collidePair(uint32_t pair, NarrowphaseChunk& chunk) const;
120
129 void buildIslands();
130
137 bool isIslandMember(uint32_t body) const;
138
148 void gatherJoints(Scene& scene);
149
156 void solve(const PhysicsSettings& physics, float dt);
157
166 void sleepIslands(bool replaying);
167
177 void writeback(Scene& scene, bool replaying, float dt);
178
189 void reportContacts(const Scene& scene, EventBus& events);
190
201 void leaseContacts(Scene& scene, NetSession& net);
202
203 private:
204 std::vector<EntityId> m_bodies;
205
212 std::vector<PhysicsBody> m_solverBodies;
213 std::vector<EntityId> m_leased;
214 std::vector<uint8_t> m_leaseCarries;
215 BodyIslands m_leaseIslands;
216 std::vector<ContactManifold> m_manifolds;
218 ContactCache m_contactCache;
219 uint64_t m_worldEpoch = 0;
220 std::vector<JointConstraint> m_joints;
221
227 std::vector<HeldJoint> m_jointImpulses;
228
234 std::vector<uint32_t> m_bodyIndexBySlot;
235
242 std::vector<uint64_t> m_jointedPairs;
243
245 std::vector<ColliderProxy> m_proxies;
247 std::vector<BodyFrame> m_bodyFrames;
248
250 BodyIslands m_islands;
252 std::vector<uint8_t> m_islandHeld;
253 std::vector<uint8_t> m_islandDisturbed;
255 std::vector<uint8_t> m_islandReady;
257 std::vector<BodyContacts> m_contacts;
258
262 std::vector<TrackedPair> m_touchingThisTick;
263
269 std::vector<TrackedPair> m_touchingLastTick;
270
271 FaultLatch m_massless;
272
273 std::vector<uint32_t> m_sorted;
274 std::vector<std::pair<uint32_t, uint32_t>> m_pairs;
275
276 std::vector<BoxShape> m_shapeBoxes;
277 std::vector<Math::AABB> m_shapeBoxBounds;
278 std::vector<CapsuleShape> m_shapeCapsules;
280 std::vector<Math::AABB> m_shapeCapsuleBounds;
281
287 std::vector<NarrowphaseChunk> m_narrowphaseChunks;
288
289 std::vector<bool> m_posedBone;
290};
291
292} // namespace Vkm::Engine
Typed pub/sub event dispatcher.
Definition event_bus.h:22
One end of a game, whether it is the authority or a guest.
Definition net_session.h:116
bool isReplayed() const override
Re-run during a replay: it moves the world from state and command, deterministically.
Definition physics_system.h:60
void fixedUpdate(FrameContext &ctx) override
Execute this system at the fixed simulation rate.
Open type-erased resource registry with typed handles and generational lifetimes.
Definition resource_manager.h:46
Central registry managing entities and an open set of component types.
Definition scene.h:29
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
Per-frame state bundle passed to every system.
Definition system.h:55
The physics world's own parameters, read once per fixed step.
Definition physics_settings.h:14