Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Physics3DContext.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
20#pragma once
21
22#include <Jolt/Jolt.h>
23#include <Jolt/Core/Factory.h>
24#include <Jolt/Core/JobSystemSingleThreaded.h>
25#include <Jolt/Core/TempAllocator.h>
26#include <Jolt/Physics/PhysicsSystem.h>
27#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
28#include <Jolt/Physics/Character/CharacterVirtual.h>
29
30#include <cstdint>
31#include <memory>
32#include <unordered_map>
33#include <unordered_set>
34#include <vector>
35
36#ifdef __EMSCRIPTEN__
37#include <emscripten/emscripten.h>
38#else
39#define EMSCRIPTEN_KEEPALIVE
40#endif
41
43
44namespace Layers {
47static constexpr JPH::uint COUNT = 16;
50static constexpr JPH::ObjectLayer of(JPH::uint layer, bool moving) {
51 return static_cast<JPH::ObjectLayer>((layer << 1) | (moving ? 1u : 0u));
52}
53static constexpr JPH::uint indexOf(JPH::ObjectLayer layer) { return layer >> 1; }
54static constexpr bool isMoving(JPH::ObjectLayer layer) { return (layer & 1u) != 0; }
55static constexpr JPH::ObjectLayer OBJECT_LAYER_COUNT = COUNT * 2;
56} // namespace Layers
57
58namespace BroadPhase {
59static constexpr JPH::BroadPhaseLayer STATIC(0);
60static constexpr JPH::BroadPhaseLayer MOVING(1);
61static constexpr JPH::uint COUNT(2);
62} // namespace BroadPhase
63
68 JPH::uint32 masks[Layers::COUNT];
69 LayerMatrix() { for (JPH::uint32& m : masks) m = 0xFFFFFFFFu; }
70 bool collide(JPH::uint a, JPH::uint b) const {
71 if (a >= Layers::COUNT || b >= Layers::COUNT) return false;
72 return (masks[a] & (1u << b)) != 0 && (masks[b] & (1u << a)) != 0;
73 }
74};
75
76LayerMatrix& layerMatrix();
77
80class ObjectLayerPairFilterImpl final : public JPH::ObjectLayerPairFilter {
81public:
82 bool ShouldCollide(JPH::ObjectLayer a, JPH::ObjectLayer b) const override {
83 if (!Layers::isMoving(a) && !Layers::isMoving(b)) return false;
85 }
86};
87
88class BroadPhaseLayerInterfaceImpl final : public JPH::BroadPhaseLayerInterface {
89public:
90 JPH::uint GetNumBroadPhaseLayers() const override { return BroadPhase::COUNT; }
91 JPH::BroadPhaseLayer GetBroadPhaseLayer(JPH::ObjectLayer layer) const override {
93 }
94#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
95 const char* GetBroadPhaseLayerName(JPH::BroadPhaseLayer layer) const override {
96 return layer == BroadPhase::STATIC ? "static" : "moving";
97 }
98#endif
99};
100
101class ObjectVsBroadPhaseLayerFilterImpl final : public JPH::ObjectVsBroadPhaseLayerFilter {
102public:
103 bool ShouldCollide(JPH::ObjectLayer a, JPH::BroadPhaseLayer b) const override {
104 // A static body only ever needs the moving tree; a moving one needs both.
105 return Layers::isMoving(a) || b == BroadPhase::MOVING;
106 }
107};
108
110struct Context {
111 JPH::PhysicsSystem* system = nullptr;
112 JPH::TempAllocatorImpl* temp = nullptr;
113 JPH::JobSystemSingleThreaded* jobs = nullptr;
114 JPH::Factory* factory = nullptr;
115
119
121 std::unordered_map<uint32_t, uint32_t> entityOf;
123 std::vector<float> transformBuffer;
125 std::vector<float> queryBuffer;
128 float solverStep = 1.0f / 60.0f;
129
133 std::vector<float> contactEnterBuffer;
135 std::vector<float> contactEnterSpeedBuffer;
137 std::vector<float> contactEnterMassBuffer;
139 std::vector<float> characterHitBuffer;
140 bool characterHitsStale = false;
141 std::vector<float> contactExitBuffer;
142 std::vector<float> sensorEnterBuffer;
143 std::vector<float> sensorExitBuffer;
144
148 std::unordered_set<uint32_t> sensorBodies;
149
152 std::unordered_map<uint32_t, JPH::Ref<JPH::TwoBodyConstraint>> constraints;
156 std::unordered_map<uint32_t, uint32_t> jointGroupOf;
157 std::unordered_map<uint32_t, std::vector<uint32_t>> jointGroupMembers;
160 std::unordered_map<uint32_t, uint32_t> jointHolds;
161 uint32_t nextJointGroup = 1;
162
166 std::unordered_map<uint32_t, std::unique_ptr<JPH::CharacterVirtual>> characters;
168 std::unordered_map<uint32_t, uint32_t> characterLayers;
170 std::unordered_map<uint32_t, std::vector<uint32_t>> characterTouching;
171 uint32_t nextCharacterId = 1;
175 JPH::CharacterVsCharacterCollisionSimple characterVsCharacter;
176
177 bool isValid() const { return system != nullptr; }
178};
179
180Context& ctx();
181
182} // namespace esengine::physics3d
JPH::BroadPhaseLayer GetBroadPhaseLayer(JPH::ObjectLayer layer) const override
bool ShouldCollide(JPH::ObjectLayer a, JPH::ObjectLayer b) const override
bool ShouldCollide(JPH::ObjectLayer a, JPH::BroadPhaseLayer b) const override
static constexpr JPH::uint COUNT(2)
static constexpr JPH::BroadPhaseLayer STATIC(0)
static constexpr JPH::BroadPhaseLayer MOVING(1)
static constexpr JPH::ObjectLayer of(JPH::uint layer, bool moving)
static constexpr JPH::ObjectLayer OBJECT_LAYER_COUNT
static constexpr JPH::uint indexOf(JPH::ObjectLayer layer)
static constexpr bool isMoving(JPH::ObjectLayer layer)
static constexpr JPH::uint COUNT
Everything one world owns. Built by physics3d_init, torn down by shutdown.
std::unordered_map< uint32_t, uint32_t > entityOf
Body -> the entity that owns it, so a readback names who moved.
std::vector< float > contactEnterBuffer
JPH::JobSystemSingleThreaded * jobs
std::vector< float > contactEnterSpeedBuffer
std::vector< float > contactExitBuffer
ObjectVsBroadPhaseLayerFilterImpl objectVsBroadPhase
std::vector< float > contactEnterMassBuffer
std::unordered_map< uint32_t, uint32_t > jointHolds
std::vector< float > sensorExitBuffer
std::vector< float > queryBuffer
The last query's hits, in the shape its getter documents.
std::unordered_map< uint32_t, uint32_t > jointGroupOf
JPH::CharacterVsCharacterCollisionSimple characterVsCharacter
std::vector< float > characterHitBuffer
std::vector< float > transformBuffer
Refilled every step: (entity, px,py,pz, qx,qy,qz,qw) per active body.
std::unordered_map< uint32_t, std::unique_ptr< JPH::CharacterVirtual > > characters
std::unordered_map< uint32_t, uint32_t > characterLayers
A character is not a body, so its layer is not stored in one.
BroadPhaseLayerInterfaceImpl broadPhaseLayers
std::unordered_map< uint32_t, JPH::Ref< JPH::TwoBodyConstraint > > constraints
std::vector< float > sensorEnterBuffer
std::unordered_set< uint32_t > sensorBodies
JPH::TempAllocatorImpl * temp
std::unordered_map< uint32_t, std::vector< uint32_t > > jointGroupMembers
std::unordered_map< uint32_t, std::vector< uint32_t > > characterTouching
ObjectLayerPairFilterImpl objectPairs
bool collide(JPH::uint a, JPH::uint b) const
JPH::uint32 masks[Layers::COUNT]