Nebula
Loading...
Searching...
No Matches
physicsinterface.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
10#include "ids/id.h"
11#include "ids/idallocator.h"
12#include "timing/time.h"
13#include "util/bitfield.h"
14#include "util/color.h"
15#include "util/delegate.h"
16#include "util/set.h"
17#include "util/arraystack.h"
18#include "util/stringatom.h"
19#include "math/mat4.h"
20#include "math/transform.h"
22#include "flat/physics/material.h"
23
24#include <functional>
25#include "PxPhysicsAPI.h"
26
27//------------------------------------------------------------------------------
28
29namespace Physics
30{
31
36
43
45{
46 Sides = 0, // Character is colliding to the sides.
47 Up = 1, // Character has collision above.
48 Down = 2, // Character has collision below.
50};
51
53
55{
56 LockLinearX = 0, // Movement along the world X axis is locked.
57 LockLinearY = 1, // Movement along the world Y axis is locked.
58 LockLinearZ = 2, // Movement along the world Z axis is locked.
59 LockAngularX = 3, // Rotation around the world X axis is locked.
60 LockAngularY = 4, // Rotation around the world Y axis is locked.
61 LockAngularZ = 5, // Rotation around the world Z axis is locked.
63};
64
66
68{
69 physx::PxMaterial * material;
71 uint64_t serialId;
72 float density;
73};
74
75struct ActorId
76{
78 ActorId() : id(Ids::InvalidId32) {}
79 ActorId(uint32_t i) : id(i) {}
80};
81
83{
85 CharacterId() : id(Ids::InvalidId32) {}
86 CharacterId(uint32_t i) : id(i) {}
87};
88
90{
92 ConstraintId() : id(Ids::InvalidId32) {}
93 ConstraintId(uint32_t i) : id(i) {}
94};
95
97{
99 AggregateId() : id(Ids::InvalidId32) {}
100 AggregateId(uint32_t i) : id(i) {}
101};
102
104{
106 ShapeId() : id(Ids::InvalidId32) {}
107 ShapeId(uint32_t i) : id(i) {}
108 const bool operator==(const ShapeId& rhs) const { return id == rhs.id; }
109};
110
111struct Actor
112{
113 physx::PxActor* actor;
116
119 bool ownsActor = true;
120
122 float mass;
124 uint32_t feedback;
126 uint64_t userData;
127#ifdef NEBULA_DEBUG
128 Util::String debugName;
129#endif
130};
131
132
133struct Shape
134{
135 physx::PxShape* shape;
141 uint64_t geometryHash;
142 uint64_t userData;
143#ifdef NEBULA_DEBUG
144 Util::String debugName;
145#endif
146};
147
148//------------------------------------------------------------------------------
165
167
169{
170 physx::PxController* controller;
171 Physics::ColliderType type;
173
179
182
185
186 uint64_t userData;
187#ifdef NEBULA_DEBUG
188 Util::String debugName;
189#endif
190};
191
193{
196 physx::PxJoint* joint;
197 uint64_t userData;
198};
199
207
208using UpdateFunctionType = void (*) (const Actor&);
209
220
222
224{
226 {
228 float radius;
230 float height;
235 };
241
245 Physics::ColliderType type = Physics::ColliderType_Capsule;
246
248 float slopeLimit = 0.707f;
249
251 bool slideOnSlopes = false;
252
256 float contactOffset = 0.1f;
257
259 float stepOffset = 0.5f;
260
264 IndexT materialId = InvalidIndex;
265
268 uint32_t collisionGroup = 0;
269
273 uint32_t collisionMask = 0xFFFFFFFF;
274
275 union {
277 BoxInfo box;
279 CapsuleInfo capsule = {0.5f, 0.8, false};
280 };
281
283 uint64_t userData = 0;
284
288};
289
290//------------------------------------------------------------------------------
298{
299 Physics::ColliderType type = Physics::ColliderType_Box;
300 IndexT materialId = InvalidIndex;
301 bool isTrigger = false;
302
305
307 Math::vec3 boxExtents = Math::vec3(0.5f, 0.5f, 0.5f); // half extents
308 float sphereRadius = 0.5f;
309 float capsuleRadius = 0.5f;
310 float capsuleHalfHeight = 0.5f;
311
315 Physics::MeshTopology topology = Physics::MeshTopology_Triangles;
316 float heightRange = 1.0f;
317 float targetSizeX = 1.0f;
318 float targetSizeY = 1.0f;
319
320 uint64_t userData = 0;
321};
322
323//------------------------------------------------------------------------------
328{
332
334 float mass = 0.0f;
335 float linearDamping = 0.0f;
336 float angularDamping = 0.0f;
337 bool useGravity = true;
338 bool ccd = false;
339
340 uint32_t collisionGroup = 0;
341 Physics::CollisionFeedback feedback = Physics::CollisionFeedback_Full;
342
345
346 uint64_t userData = 0;
347};
348
351struct Scene
352{
353 physx::PxFoundation *foundation = nullptr;
354 physx::PxPhysics *physics = nullptr;
355 physx::PxScene *scene = nullptr;
356 physx::PxControllerManager *controllerManager = nullptr;
357 physx::PxDefaultCpuDispatcher *dispatcher = nullptr;
362};
363
365void Setup();
367void ShutDown();
368
370void Step(Timing::Time delta);
372void Step(Timing::Time delta, IndexT scene);
373
377void DestroyScene(IndexT scene);
378
381
383void SetActiveActorCallback(UpdateFunctionType callback, IndexT sceneId = 0);
385void SetEventCallback(EventCallbackType callback, IndexT sceneId = 0);
386
387
389void RenderDebug();
390
392void SetOnSleepCallback(Util::Delegate<void(ActorId* id, SizeT num)> const& callback);
394void SetOnWakeCallback(Util::Delegate<void(ActorId* id, SizeT num)> const& callback);
395
397IndexT SetPhysicsMaterial(Util::StringAtom name, float staticFriction, float dynamicFriction, float restition, float density);
404
406ActorId CreateActorInstance(Physics::ActorResourceId id, Math::transform const& trans, Physics::ActorType type, uint64_t userData, IndexT scene = 0);
409
410}
411//------------------------------------------------------------------------------
Simple transform using position, quaternion, and scale.
Definition transform.h:18
Nebula's dynamic array class.
Definition array.h:61
Implements large bit field.
Definition bitfield.h:43
Nebula delegate class, allows to store a function, method or lambda call into a C++ object for later ...
Definition delegate.h:39
A collection of unique values with quick lookup.
Definition set.h:34
A StringAtom.
Definition stringatom.h:22
Nebula's universal string class.
Definition string.h:50
This simple Id pool implements a set of free and used consecutive integers.
Definition id.h:135
uint32_t Id32
Definition id.h:138
Diverse functions for manipulating physics actors.
Definition actorcontext.cc:22
void SetActiveActorCallback(UpdateFunctionType callback, IndexT sceneId)
called once per step for every actor PhysX reported active on that step
Definition physicsinterface.cc:230
Util::BitField< CharacterCollisionBitsMax > CharacterCollision
Definition physicsinterface.h:52
Physics::Scene & GetScene(IndexT idx)
Definition physicsinterface.cc:210
void(*)(const CharacterHitEvent &) CharacterHitCallbackType
Definition physicsinterface.h:166
ActorType
Definition physicsinterface.h:38
@ Static
Definition physicsinterface.h:39
@ Dynamic
Definition physicsinterface.h:41
@ Kinematic
Definition physicsinterface.h:40
void RenderDebug()
render a debug visualization of the level
Definition physicsinterface.cc:240
SizeT GetNrMaterials()
Definition physicsinterface.cc:362
void ShutDown()
close the physics subsystem
Definition physicsinterface.cc:98
void SetEventCallback(EventCallbackType callback, IndexT sceneId)
Definition physicsinterface.cc:220
Physics::Material & GetMaterial(IndexT idx)
Definition physicsinterface.cc:290
AxisLockBits
Definition physicsinterface.h:55
@ LockLinearZ
Definition physicsinterface.h:58
@ LockAngularX
Definition physicsinterface.h:59
@ LockAngularY
Definition physicsinterface.h:60
@ LockLinearX
Definition physicsinterface.h:56
@ LockLinearY
Definition physicsinterface.h:57
@ AxisLockBitsMax
Definition physicsinterface.h:62
@ LockAngularZ
Definition physicsinterface.h:61
ActorId CreateActorInstance(Physics::ActorResourceId id, Math::transform const &trans, Physics::ActorType type, uint64_t userData, IndexT scene)
Definition physicsinterface.cc:390
void Setup()
initialize the physics subsystem and create a default scene
Definition physicsinterface.cc:67
void SetOnSleepCallback(Util::Delegate< void(ActorId *id, SizeT num)> const &callback)
Definition physicsinterface.cc:303
CharacterCollisionBits
Definition physicsinterface.h:45
@ Sides
Definition physicsinterface.h:46
@ Up
Definition physicsinterface.h:47
@ CharacterCollisionBitsMax
Definition physicsinterface.h:49
@ Down
Definition physicsinterface.h:48
Util::BitField< 16 > AxisLock
Definition physicsinterface.h:65
void DestroyActorInstance(Physics::ActorId id)
Definition physicsinterface.cc:399
void DestroyScene(IndexT sceneId)
Definition physicsinterface.cc:160
void(*)(const Actor &) UpdateFunctionType
Definition physicsinterface.h:208
void SetOnWakeCallback(Util::Delegate< void(ActorId *id, SizeT num)> const &callback)
Definition physicsinterface.cc:312
void Step(Timing::Time delta)
perform a sync simulation step on every scene
Definition physicsinterface.cc:372
IndexT SetPhysicsMaterial(Util::StringAtom name, float staticFriction, float dynamicFriction, float restitution, float density)
Definition physicsinterface.cc:321
IndexT CreateScene()
Definition physicsinterface.cc:107
IndexT LookupMaterial(Util::StringAtom name)
Definition physicsinterface.cc:353
void(*)(const Util::Array< ContactEvent > &) EventCallbackType
Definition physicsinterface.h:221
double Time
the time datatype
Definition time.h:18
#define RESOURCE_ID_TYPE(type)
Definition resourceid.h:41
Definition scalar.h:67
Represents a 3D point in space.
Definition point.h:22
A 3D vector.
Definition vec3.h:37
A vector is a 3D direction in space.
Definition vector.h:22
Definition physicsinterface.h:112
Util::Array< ShapeId > shapes
Definition physicsinterface.h:121
AxisLock axisLock
Definition physicsinterface.h:125
bool ownsActor
false when this context only holds a record for the actor and not the actor itself,...
Definition physicsinterface.h:119
physx::PxActor * actor
Definition physicsinterface.h:113
ActorResourceId res
Definition physicsinterface.h:115
uint64_t userData
Definition physicsinterface.h:126
ActorId id
Definition physicsinterface.h:114
float mass
Definition physicsinterface.h:122
uint32_t collisionGroup
Definition physicsinterface.h:123
uint32_t feedback
Definition physicsinterface.h:124
Definition physicsinterface.h:76
ActorId()
Definition physicsinterface.h:78
ActorId(uint32_t i)
Definition physicsinterface.h:79
Ids::Id32 id
Definition physicsinterface.h:77
Definition physicsinterface.h:32
Definition physicsinterface.h:201
AggregateResourceId res
Definition physicsinterface.h:203
AggregateId id
Definition physicsinterface.h:202
Util::Array< ActorId > actors
Definition physicsinterface.h:204
Util::Array< ConstraintId > constraints
Definition physicsinterface.h:205
Definition physicsinterface.h:97
AggregateId()
Definition physicsinterface.h:99
AggregateId(uint32_t i)
Definition physicsinterface.h:100
Ids::Id32 id
Definition physicsinterface.h:98
Definition physicsinterface.h:33
A physics body description.
Definition physicsinterface.h:328
Physics::CollisionFeedback feedback
Definition physicsinterface.h:341
float linearDamping
Definition physicsinterface.h:335
float mass
0 means the mass is derived from the shape volumes and the material densities
Definition physicsinterface.h:334
uint64_t userData
Definition physicsinterface.h:346
Physics::ActorType type
Definition physicsinterface.h:329
AxisLock axisLock
the world space axes the body is locked along or around.
Definition physicsinterface.h:344
bool ccd
Definition physicsinterface.h:338
IndexT scene
Definition physicsinterface.h:330
Math::transform transform
Definition physicsinterface.h:331
uint32_t collisionGroup
Definition physicsinterface.h:340
float angularDamping
Definition physicsinterface.h:336
bool useGravity
Definition physicsinterface.h:337
Definition physicsinterface.h:237
Math::float3 halfExtents
Half the width, height and depth of the characters AABB collider.
Definition physicsinterface.h:239
Definition physicsinterface.h:226
float radius
Radius of this capsule.
Definition physicsinterface.h:228
bool constrainClimbing
If set to false, the capsule will climb over surfaces according to impact normal.
Definition physicsinterface.h:234
float height
Height of this capsule. The total height will be height + 2 * radius.
Definition physicsinterface.h:230
Definition physicsinterface.h:224
float contactOffset
The contact offset used by the controller.
Definition physicsinterface.h:256
float slopeLimit
The maximum slope which the character can walk up. Defined as cosine of desired limit angle.
Definition physicsinterface.h:248
Physics::ColliderType type
Determines the type of collider that the character will use.
Definition physicsinterface.h:245
float stepOffset
The maximum height of an obstacle which the character can climb.
Definition physicsinterface.h:259
uint32_t collisionMask
The set of groups the controller accepts when it moves, matched against word0 of the hit shape's filt...
Definition physicsinterface.h:273
uint64_t userData
Can be used to store application side information in the character.
Definition physicsinterface.h:283
CharacterHitCallbackType reportCallback
Called for every shape the controller hits during move.
Definition physicsinterface.h:287
IndexT materialId
Optional physics material to use.
Definition physicsinterface.h:264
bool slideOnSlopes
Should the character slide down slopes?
Definition physicsinterface.h:251
uint32_t collisionGroup
word0 of the shape's filter data, which is what the simulation filter shader reads.
Definition physicsinterface.h:268
A shape a character controller hit during its move.
Definition physicsinterface.h:156
float distance
Definition physicsinterface.h:163
Math::vector normal
Definition physicsinterface.h:162
ShapeId shape
Definition physicsinterface.h:159
CharacterId character
Definition physicsinterface.h:157
ActorId actor
Definition physicsinterface.h:158
Math::point position
Definition physicsinterface.h:161
Definition physicsinterface.h:169
uint64_t userData
Definition physicsinterface.h:186
ShapeId shape
Definition physicsinterface.h:178
uint32_t collisionMask
Definition physicsinterface.h:181
uint32_t collisionGroup
Definition physicsinterface.h:180
ActorId actor
The controller owns a physx actor internally.
Definition physicsinterface.h:177
CharacterId id
Definition physicsinterface.h:172
CharacterHitCallbackType reportCallback
the report the create info carried, kept so the hit report can be forwarded without a lookup
Definition physicsinterface.h:184
physx::PxController * controller
Definition physicsinterface.h:170
Physics::ColliderType type
Definition physicsinterface.h:171
Definition physicsinterface.h:83
CharacterId()
Definition physicsinterface.h:85
CharacterId(uint32_t i)
Definition physicsinterface.h:86
Ids::Id32 id
Definition physicsinterface.h:84
A collider description / create info.
Definition physicsinterface.h:298
float targetSizeY
Definition physicsinterface.h:318
IndexT primGroup
Definition physicsinterface.h:314
float capsuleRadius
Definition physicsinterface.h:309
float targetSizeX
Definition physicsinterface.h:317
uint64_t userData
Definition physicsinterface.h:320
bool isTrigger
Definition physicsinterface.h:301
Math::transform transform
local transform of the shape, relative to the actor it belongs to
Definition physicsinterface.h:304
float sphereRadius
Definition physicsinterface.h:308
Util::StringAtom resource
mesh and heightfield geometry is read through the geometry loader, from a URN such as folder:file:msh
Definition physicsinterface.h:313
IndexT materialId
Definition physicsinterface.h:300
Physics::ColliderType type
Definition physicsinterface.h:299
float capsuleHalfHeight
Definition physicsinterface.h:310
Math::vec3 boxExtents
shape parameters. Only the ones matching the collider type are read
Definition physicsinterface.h:307
Physics::MeshTopology topology
Definition physicsinterface.h:315
float heightRange
Definition physicsinterface.h:316
Definition physicsinterface.h:193
ConstraintId id
Definition physicsinterface.h:194
Ids::Id32 res
Definition physicsinterface.h:195
uint64_t userData
Definition physicsinterface.h:197
physx::PxJoint * joint
Definition physicsinterface.h:196
Definition physicsinterface.h:90
Ids::Id32 id
Definition physicsinterface.h:91
ConstraintId(uint32_t i)
Definition physicsinterface.h:93
ConstraintId()
Definition physicsinterface.h:92
Definition physicsinterface.h:35
Definition physicsinterface.h:211
Math::vector normal
Definition physicsinterface.h:216
ActorId actor1
Definition physicsinterface.h:213
ActorId actor0
Definition physicsinterface.h:212
float separation
Definition physicsinterface.h:218
Math::vector impulse
Definition physicsinterface.h:217
Math::point position
Definition physicsinterface.h:215
Definition physicsinterface.h:68
Util::StringAtom name
Definition physicsinterface.h:70
physx::PxMaterial * material
Definition physicsinterface.h:69
uint64_t serialId
Definition physicsinterface.h:71
float density
Definition physicsinterface.h:72
Definition physicsinterface.h:34
physx scene classes, foundation and physics are duplicated here for convenience instead of static get...
Definition physicsinterface.h:352
physx::PxFoundation * foundation
Definition physicsinterface.h:353
UpdateFunctionType updateFunction
Definition physicsinterface.h:358
physx::PxPhysics * physics
Definition physicsinterface.h:354
Util::Set< Ids::Id32 > activeActors
Definition physicsinterface.h:361
EventCallbackType eventCallback
Definition physicsinterface.h:359
physx::PxDefaultCpuDispatcher * dispatcher
Definition physicsinterface.h:357
Util::Array< Physics::ContactEvent > eventBuffer
Definition physicsinterface.h:360
physx::PxScene * scene
Definition physicsinterface.h:355
physx::PxControllerManager * controllerManager
Definition physicsinterface.h:356
Definition physicsinterface.h:134
IndexT materialId
Definition physicsinterface.h:138
ActorId actor
Definition physicsinterface.h:137
uint64_t geometryHash
hash of the description the current geometry was built from, used to detect changes
Definition physicsinterface.h:141
physx::PxShape * shape
Definition physicsinterface.h:135
uint64_t userData
Definition physicsinterface.h:142
bool isTrigger
Definition physicsinterface.h:139
ShapeId id
Definition physicsinterface.h:136
Definition physicsinterface.h:104
Ids::Id32 id
Definition physicsinterface.h:105
ShapeId(uint32_t i)
Definition physicsinterface.h:107
const bool operator==(const ShapeId &rhs) const
Definition physicsinterface.h:108
ShapeId()
Definition physicsinterface.h:106
Typedefs for the Timing subsystem.
int SizeT
Definition types.h:50
int IndexT
Definition types.h:49
CapsuleInfo capsule
Capsule information. Make sure to set type to Capsule if using this.
BoxInfo box
AABB information. Make sure to set type to Box if using this.