vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
physics_internal.h
1#pragma once
2
3#include <cstdint>
4#include <utility>
5#include <vector>
6
7#include <glm/glm.hpp>
8#include <glm/gtc/quaternion.hpp>
9
10#include "core/math/bounds.h"
11#include "ecs/entity.h"
12#include "ecs/component/physics/collider.h"
13#include "ecs/component/physics/rigidbody.h"
14#include "system/physics/body_pose.h"
15#include "system/physics/collision/contact.h"
16
17namespace Vkm::Engine {
18
26 uint32_t body = 0;
27
34 uint32_t boxFirst = 0;
35 uint32_t boxCount = 0;
36 uint32_t capsuleFirst = 0;
37 uint32_t capsuleCount = 0;
38
45 const Collider* collider = nullptr;
47 bool immovable = false;
48 bool fixed = false;
49
50 // Copied at gather, so the pair loop does not reach back into the scene.
51 bool isTrigger = false;
52 int layer = 1;
53 int collidesWith = ~0;
54};
55
62inline constexpr uint32_t NARROWPHASE_CHUNK_PAIRS = 64;
63
68 uint32_t pair = 0;
70 uint32_t manifoldCount = 0;
72 glm::vec3 point = glm::vec3(0.0f);
73 glm::vec3 normal = glm::vec3(0.0f, 1.0f, 0.0f);
74};
75
84 EntityId b;
85 bool aTrigger = false;
86 bool bTrigger = false;
87 glm::vec3 point = glm::vec3(0.0f);
88 glm::vec3 normal = glm::vec3(0.0f, 1.0f, 0.0f);
89
100 static bool before(const TrackedPair& x, const TrackedPair& y) {
101 if (x.a.slot() != y.a.slot()) return x.a.slot() < y.a.slot();
102 if (x.b.slot() != y.b.slot()) return x.b.slot() < y.b.slot();
103 if (x.a.generation() != y.a.generation()) return x.a.generation() < y.a.generation();
104 if (x.b.generation() != y.b.generation()) return x.b.generation() < y.b.generation();
105 if (x.aTrigger != y.aTrigger) return y.aTrigger;
106 return !x.bTrigger && y.bTrigger;
107 }
108};
109
117 std::vector<TouchingPair> touching;
118 std::vector<ContactManifold> manifolds;
119 std::vector<uint32_t> meshCandidates;
120};
121
127class BodyIslands {
128 public:
129 BodyIslands() = default;
130 ~BodyIslands() = default;
131
132 BodyIslands(const BodyIslands& other) = delete;
133 BodyIslands& operator=(const BodyIslands& other) = delete;
134
135 BodyIslands(BodyIslands && other) = delete;
136 BodyIslands& operator=(BodyIslands && other) = delete;
137
138 public:
140 void reset(size_t count) {
141 m_parent.resize(count);
142 m_size.assign(count, 1);
143 for (size_t i = 0; i < count; ++i) m_parent[i] = static_cast<uint32_t>(i);
144 }
145
146 uint32_t find(uint32_t i) {
147 while (m_parent[i] != i) {
148 m_parent[i] = m_parent[m_parent[i]];
149 i = m_parent[i];
150 }
151 return i;
152 }
153
154 void join(uint32_t a, uint32_t b) {
155 a = find(a);
156 b = find(b);
157 if (a == b) return;
158 if (m_size[a] < m_size[b]) std::swap(a, b);
159 m_parent[b] = a;
160 m_size[a] += m_size[b];
161 }
162
163 private:
164 std::vector<uint32_t> m_parent;
165 std::vector<uint32_t> m_size;
166};
167
174struct BodyFrame {
176 Rigidbody* rb = nullptr;
179 RigidbodyMotion motion = RigidbodyMotion::Dynamic;
180 bool decided = false;
181 float invMass = 0.0f;
182 float reach = 0.0f;
184 glm::mat3 invInertiaLocal = glm::mat3(0.0f);
185};
186
194 // Below any unit normal, so the first contact replaces it, even a vertical wall's.
195 glm::vec3 support = {0.0f, -2.0f, 0.0f};
196
197 // World up has no horizontal part, so level floor leaves "nothing in the way".
198 glm::vec3 block = {0.0f, 1.0f, 0.0f};
199
200 bool touched = false;
201};
202
203} // namespace Vkm::Engine
void reset(size_t count)
Every body its own island again.
Definition physics_internal.h:140
What one tick's contacts add up to for one body, in the two reductions Rigidbody publishes.
Definition physics_internal.h:193
glm::vec3 block
Most horizontal contact normal.
Definition physics_internal.h:198
bool touched
A resolved, non-trigger contact reached this body.
Definition physics_internal.h:200
glm::vec3 support
Most upward contact normal.
Definition physics_internal.h:195
Per-tick body state cached at gather: mass properties, and the frame writeback maps through.
Definition physics_internal.h:174
glm::mat3 invInertiaLocal
Body-local inverse inertia; 0 = no rotational response.
Definition physics_internal.h:184
RigidbodyMotion motion
Rigidbody::motion, or Kinematic for a bone a ragdoll poses (isPosedByAnimation)
Definition physics_internal.h:179
float reach
Definition physics_internal.h:182
bool decided
This end decides where it goes; always true offline.
Definition physics_internal.h:180
float invMass
1/mass this tick; 0 = nothing the solver does moves it.
Definition physics_internal.h:181
Rigidbody * rb
Looked up at gather; valid for the tick, which adds and removes no Rigidbody.
Definition physics_internal.h:176
BodyPose pose
Definition physics_internal.h:177
Where a body sits in the world, and the frame that maps back to local.
Definition body_pose.h:20
Broadphase and narrowphase view of one collidable body, cached per tick.
Definition physics_internal.h:25
bool fixed
RigidbodyMotion::Static: never moves at all, unlike a kinematic body.
Definition physics_internal.h:48
Math::AABB bounds
World bound of every part together.
Definition physics_internal.h:46
const Collider * collider
The component this proxy was built from: its parts, and a mesh part's triangles and tree.
Definition physics_internal.h:45
uint32_t boxFirst
This body's parts already placed in world space, as two spans.
Definition physics_internal.h:34
Collision geometry attached to an entity, evaluated in its Transform frame.
Definition collider.h:72
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
An axis-aligned box.
Definition bounds.h:14
Everything one narrowphase chunk writes, so chunks on different threads share nothing.
Definition physics_internal.h:116
std::vector< ContactManifold > manifolds
The touching pairs', end to end, same order.
Definition physics_internal.h:118
std::vector< uint32_t > meshCandidates
Scratch: the triangles a tree walk found.
Definition physics_internal.h:119
std::vector< TouchingPair > touching
In pair order.
Definition physics_internal.h:117
Dynamics state for a physics body: motion, material response and mass.
Definition rigidbody.h:31
A broadphase pair that touched this tick, as its chunk hands it to the merge.
Definition physics_internal.h:67
uint32_t pair
Definition physics_internal.h:68
glm::vec3 point
First contact found, the event's representative.
Definition physics_internal.h:72
glm::vec3 normal
That contact's normal, A -> B.
Definition physics_internal.h:73
uint32_t manifoldCount
Its manifolds, next in the chunk's list; none for a trigger.
Definition physics_internal.h:70
Two colliders touching on a live tick, as the contact events track them across ticks.
Definition physics_internal.h:82
glm::vec3 point
The event's representative contact.
Definition physics_internal.h:87
static bool before(const TrackedPair &x, const TrackedPair &y)
The order tracked pairs are kept and reported in: slot pair, generation, trigger flags.
Definition physics_internal.h:100
bool aTrigger
a's collider is a trigger
Definition physics_internal.h:85
EntityId a
The lower slot.
Definition physics_internal.h:83
glm::vec3 normal
That contact's normal, a -> b.
Definition physics_internal.h:88
bool bTrigger
b's collider is a trigger
Definition physics_internal.h:86