Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
PhysXWorld.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
23#pragma once
24
25#include <PxPhysicsAPI.h>
26
27#include <cstdint>
28#include <memory>
29#include <unordered_map>
30#include <vector>
31
32#ifdef __EMSCRIPTEN__
33#include <emscripten/emscripten.h>
34#else
35#define EMSCRIPTEN_KEEPALIVE
36#endif
37
39
41constexpr uint32_t LAYER_COUNT = 16;
42
45enum FilterWord : uint32_t { kLayerWord = 0, kKindWord = 1 };
47constexpr uint32_t KIND_CHARACTER = 1u;
48
50struct Body {
51 uint32_t id = 0;
52 uint32_t entity = 0;
53 uint32_t layer = 0;
54 physx::PxRigidActor* actor = nullptr;
55 bool sensor = false;
56 bool character = false;
60 float gravityScale = 1.0f;
63 physx::PxVec3 kinematicVelocity{0.0f};
65};
66
67struct Character {
68 physx::PxController* controller = nullptr;
69 uint32_t entity = 0;
70 uint32_t layer = 0;
72 float verticalSpeed = 0.0f;
73 bool grounded = false;
74 physx::PxVec3 groundNormal{0.0f, 1.0f, 0.0f};
75 float pushForce = 0.0f;
76 float mass = 70.0f;
78 std::vector<const physx::PxRigidActor*> touching;
79 std::unique_ptr<Body> body; // the kinematic actor's record, so hits name the entity
80};
81
84enum class JointKind : uint8_t { Point, Hinge, Slider, Distance, Fixed, SwingTwist, SixDOF };
85
86struct Joint {
87 physx::PxJoint* joint = nullptr;
90 float maxForce = 0.0f;
91};
92
93struct World {
94 physx::PxScene* scene = nullptr;
95 physx::PxDefaultCpuDispatcher* dispatcher = nullptr;
96 physx::PxControllerManager* controllers = nullptr;
97
98 uint32_t masks[LAYER_COUNT];
99 std::unordered_map<uint32_t, std::unique_ptr<Body>> bodies;
100 uint32_t nextBodyId = 1;
101
102 std::unordered_map<uint32_t, std::unique_ptr<Character>> characters;
103 uint32_t nextCharacterId = 1;
104
105 std::unordered_map<uint32_t, Joint> joints;
108 std::unordered_map<uint64_t, physx::PxMaterial*> materials;
109
110 std::vector<float> transformBuffer;
111 std::vector<float> queryBuffer;
112 std::vector<float> contactEnterBuffer;
114 std::vector<float> contactEnterSpeedBuffer;
116 std::vector<float> contactEnterMassBuffer;
118 std::vector<float> characterHitBuffer;
119 bool characterHitsStale = false;
120 std::vector<float> contactExitBuffer;
121 std::vector<float> sensorEnterBuffer;
122 std::vector<float> sensorExitBuffer;
123
124 World() { for (uint32_t& m : masks) m = 0xFFFFFFFFu; }
125 bool isValid() const { return scene != nullptr; }
126
127 bool collide(uint32_t a, uint32_t b) const {
128 if (a >= LAYER_COUNT || b >= LAYER_COUNT) return false;
129 return (masks[a] & (1u << b)) != 0 && (masks[b] & (1u << a)) != 0;
130 }
131 Body* body(uint32_t id) {
132 auto found = bodies.find(id);
133 return found == bodies.end() ? nullptr : found->second.get();
134 }
135};
136
137World& world();
138physx::PxPhysics& physics();
140physx::PxMaterial* defaultMaterial();
141
143inline uint32_t entityOf(const physx::PxActor* actor) {
144 if (actor == nullptr || actor->userData == nullptr) return 0;
145 return static_cast<const Body*>(actor->userData)->entity;
146}
147
148} // namespace esengine::physics3d_physx
constexpr uint32_t LAYER_COUNT
Sixteen layers, the same number the Jolt backend and the 2D world name.
constexpr uint32_t KIND_CHARACTER
kKindWord bits.
PxMaterial * defaultMaterial()
Friction 0.6, no bounce: the default physics material.
uint32_t entityOf(const physx::PxActor *actor)
The entity an actor speaks for, or 0.
Context passed to every System::update call.
Definition World.hpp:40
What the module knows about a body beyond what PhysX holds.
physx::PxRigidActor * actor
std::vector< const physx::PxRigidActor * > touching
float verticalSpeed
The vertical speed between moves; a controller does not fall on its own.
float maxForce
A slider's motor force, kept so turning the motor back on restores it.
physx::PxDefaultCpuDispatcher * dispatcher
std::vector< float > transformBuffer
physx::PxControllerManager * controllers
uint32_t masks[LAYER_COUNT]
std::vector< float > contactExitBuffer
bool collide(uint32_t a, uint32_t b) const
std::vector< float > contactEnterSpeedBuffer
std::vector< float > queryBuffer
std::vector< float > contactEnterBuffer
std::unordered_map< uint32_t, std::unique_ptr< Character > > characters
std::vector< float > sensorEnterBuffer
std::unordered_map< uint32_t, Joint > joints
std::vector< float > characterHitBuffer
std::unordered_map< uint64_t, physx::PxMaterial * > materials
std::vector< float > contactEnterMassBuffer
std::unordered_map< uint32_t, std::unique_ptr< Body > > bodies
std::vector< float > sensorExitBuffer