Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
PhysicsEngine.cpp
Go to the documentation of this file.
5#include "glm/gtc/type_ptr.inl"
6
8
9namespace EngineCore
10{
12 {
13 broadphase = new btDbvtBroadphase();
14 collisionConfiguration = new btDefaultCollisionConfiguration();
15 dispatcher = new btCollisionDispatcher( collisionConfiguration );
16 solver = new btSequentialImpulseConstraintSolver();
17 dynamicsWorld = new btDiscreteDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration );
18 dynamicsWorld->setGravity( btVector3( 0.0f, -9.81f, 0.0f ) );
19
20 auto& registry = Ecs::RegistryManager::get();
21 registry.on_destroy<Ecs::SimulatesPhysics>( ).connect<&PhysicsEngine::onPhysicsComponentDestroyed>(this);
22 }
23
25 {
26 for ( int i = 0; i < dynamicsWorld->getNumCollisionObjects(); i++ )
27 {
28 btCollisionObject * object = dynamicsWorld->getCollisionObjectArray()[i];
29 if ( object != nullptr )
30 {
31 btRigidBody * body = btRigidBody::upcast( object );
32 if ( body && body->getMotionState() )
33 {
34 delete body->getMotionState();
35 }
36 dynamicsWorld->removeCollisionObject( object );
37 delete object;
38 }
39 }
40
41 delete dynamicsWorld;
42 delete solver;
43 delete dispatcher;
45 delete broadphase;
46 }
47
48 void PhysicsEngine::onPhysicsComponentDestroyed(entt::registry& registry, entt::entity entity)
49 {
50 auto& physics = registry.get<Ecs::SimulatesPhysics>(entity); // still valid in on_destroy
51 if (physics.rigidBody)
52 {
53 dynamicsWorld->removeRigidBody(physics.rigidBody);
54 delete physics.rigidBody->getMotionState();
55 delete physics.rigidBody;
56 }
57
58 if (physics.collisionShape)
59 {
60 delete physics.collisionShape;
61 }
62 }
63
64 void PhysicsEngine::update( float deltaTimeSeconds )
65 {
66 TRACY_ZONE_SCOPED_NAMED( "Physics Tick" );
67 dynamicsWorld->stepSimulation( deltaTimeSeconds, 10, 1.0f/120.0f);
68 }
69
70 btDiscreteDynamicsWorld * PhysicsEngine::getDynamicsWorld() const
71 {
72 return dynamicsWorld;
73 }
74
75 btCollisionShape * PhysicsEngine::createBoxShape( const glm::vec3 & halfExtents )
76 {
77 return new btBoxShape(bt::getVec3( halfExtents ));
78 }
79
80 btCollisionShape * PhysicsEngine::createSphereShape( float radius )
81 {
82 return new btSphereShape( radius );
83 }
84
85 btCollisionShape * PhysicsEngine::createCapsuleShape( float radius, float height )
86 {
87 return new btCapsuleShape( radius, height );
88 }
89
90 btCollisionShape * PhysicsEngine::createPlaneShape( const glm::vec3 & surfaceNormal, float distanceFromOrigin )
91 {
92 return new btStaticPlaneShape(bt::getVec3(surfaceNormal), distanceFromOrigin);
93 }
94
95 btRigidBody * PhysicsEngine::createRigidBody( const RigidBodyCreateInfo & info ) const
96 {
97 btTransform startTransform;
98 startTransform.setIdentity();
99 startTransform.setFromOpenGLMatrix(glm::value_ptr(info.transform));
100
101 btVector3 localInertia(0, 0, 0);
102 if (info.mass != 0.0f)
103 {
104 info.shape->calculateLocalInertia(info.mass, localInertia);
105 }
106
107 auto* motionState = new btDefaultMotionState(startTransform);
108 btRigidBody::btRigidBodyConstructionInfo rbInfo(info.mass, motionState, info.shape, localInertia);
109 rbInfo.m_friction = info.friction;
110 rbInfo.m_restitution = info.restitution;
111
112 auto* body = new btRigidBody(rbInfo);
113 dynamicsWorld->addRigidBody(body);
114
115 return body;
116 }
117
118 void PhysicsEngine::removeRigidBody( btRigidBody * body ) const
119 {
120 if (body)
121 {
122 dynamicsWorld->removeRigidBody(body);
123 if (body->getMotionState())
124 {
125 delete body->getMotionState();
126 }
127 delete body;
128 }
129 }
130} // namespace EngineCore
#define TRACY_ZONE_SCOPED_NAMED(name)
static entt::registry & get()
Gets the registry for all components.
btCollisionDispatcher * dispatcher
void update(float deltaTimeSeconds)
Steps the physics simulation by 1/120th of a second. The idea of the engine is that it should run on ...
void onPhysicsComponentDestroyed(entt::registry &registry, entt::entity entity)
Cleans up the hit box pointers and motion state if applicable.
static btCollisionShape * createBoxShape(const glm::vec3 &halfExtents)
Creates a collision box.
btCollisionConfiguration * collisionConfiguration
btBroadphaseInterface * broadphase
static btCollisionShape * createSphereShape(float radius)
Creates a collision sphere.
btRigidBody * createRigidBody(const RigidBodyCreateInfo &info) const
Creates a rigid body for the physics simulation.
btDiscreteDynamicsWorld * getDynamicsWorld() const
Gets the world where the physics simulation is performed inside.
static btCollisionShape * createCapsuleShape(float radius, float height)
creates a capsule with a height and a radius.
static btCollisionShape * createPlaneShape(const glm::vec3 &surfaceNormal, float distanceFromOrigin)
Creates a plane from.
btConstraintSolver * solver
void removeRigidBody(btRigidBody *body) const
Removes a rigid body from the physics simulation.
btDiscreteDynamicsWorld * dynamicsWorld
Log category system implementation.
btVector3 getVec3(glm::vec3 v)
Definition Converter.cpp:12
Tag for EnTT which tags entities which should simulate physics.
Definition EcsData.h:101
Rigid body create info is used to create a rigid body. It delivers all informations like the initial ...
glm::mat4 transform
initial location for the rigid body
float restitution
energy absorption 0.0f means full absorption on impact, 0.5f half of the energy is maintained after i...
float friction
friction 0.0f - 1.0f. 0 is ice 0.3f - 0.5f wood or plastic. 0.7f - 1.0f High friction
btCollisionShape * shape
pointer to the collision shape
float mass
mass in Kilograms. Mass of 0 is a static object --> immovable