Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
PhysicsEngine.h
Go to the documentation of this file.
1#pragma once
2
3#include <btBulletDynamicsCommon.h>
4#include <glm/mat4x4.hpp>
5#include <glm/vec3.hpp>
6
7namespace Engine
8{
9 namespace Components
10 {
11 class PhysicsBody;
12 }
13
14 namespace Physics
15 {
20 {
24 glm::vec3 point{ 0.0f };
25
29 glm::vec3 normal{ 0.0f };
30
34 float fraction = 0.0f;
35
39 float distance = 0.0f;
40
44 const btCollisionObject* collisionObject = nullptr;
45
51 };
52
58 {
60 glm::mat4 transform {1.0f};
62 float mass = 1.0f;
64 btCollisionShape* shape = nullptr;
66 float friction = 0.5f;
68 float restitution = 0.0f;
69 };
70 }
71
72 namespace Core
73 {
78 {
79 public:
82
88 void onPhysicsComponentDestroyed( entt::registry & registry, entt::entity entity );
89
95 void update(float deltaTimeSeconds);
96
101 [[nodiscard]] btDiscreteDynamicsWorld * getDynamicsWorld() const;
102
111 [[nodiscard]] bool raycastClosest(
112 const glm::vec3& origin,
113 const glm::vec3& direction,
114 float maxDistance,
115 Physics::RaycastHit& outHit
116 ) const;
117
127 [[nodiscard]] bool sphereSweepClosest(
128 const glm::vec3& from,
129 const glm::vec3& to,
130 float radius,
131 Physics::RaycastHit& outHit,
132 const btCollisionObject* ignoredObject = nullptr
133 ) const;
134
143 [[nodiscard]] bool sphereClosest(
144 const glm::vec3& position,
145 float radius,
146 Physics::RaycastHit& outHit,
147 const btCollisionObject* ignoredObject = nullptr
148 ) const;
149
155 static btCollisionShape * createBoxShape(const glm::vec3& halfExtents);
161 static btCollisionShape * createSphereShape(float radius);
168 static btCollisionShape * createCapsuleShape(float radius, float height);
175 static btCollisionShape * createPlaneShape(const glm::vec3& surfaceNormal, float distanceFromOrigin);
176
182 static btCollisionShape * createConvexHullShape(const std::vector<glm::vec3>& vertices);
183
193 static btCollisionShape * createTriangleMeshShape(
194 const std::vector<glm::vec3>& vertices,
195 const std::vector<uint32_t>& indices,
196 btTriangleMesh*& triangleMeshOut);
197
202 static btCompoundShape * createCompoundShape();
203
209 [[nodiscard]] btRigidBody * createRigidBody( const Physics::RigidBodyCreateInfo & info ) const;
210
215 void removeRigidBody(btRigidBody* body ) const;
216
223 void debugDrawAll() const;
224
225 private:
226 btBroadphaseInterface* broadphase;
227 btCollisionConfiguration* collisionConfiguration;
228 btCollisionDispatcher* dispatcher;
229 btConstraintSolver* solver;
230 btDiscreteDynamicsWorld* dynamicsWorld;
231 };
232 }
233}
Component that manages a physics rigid body for an entity.
bool raycastClosest(const glm::vec3 &origin, const glm::vec3 &direction, float maxDistance, Physics::RaycastHit &outHit) const
Casts a ray through the physics world and returns the closest hit, if any.
btCollisionDispatcher * dispatcher
void debugDrawAll() const
Draws debug visualization of all collision shapes in the world.
btConstraintSolver * solver
btRigidBody * createRigidBody(const Physics::RigidBodyCreateInfo &info) const
Creates a rigid body for the physics simulation.
static btCollisionShape * createConvexHullShape(const std::vector< glm::vec3 > &vertices)
Creates a convex hull collision shape from a set of vertices.
btDiscreteDynamicsWorld * getDynamicsWorld() const
Gets the world where the physics simulation is performed inside.
bool sphereClosest(const glm::vec3 &position, float radius, Physics::RaycastHit &outHit, const btCollisionObject *ignoredObject=nullptr) const
Tests a sphere at one position and returns the closest overlapping collision object,...
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.
btCollisionConfiguration * collisionConfiguration
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 ...
btBroadphaseInterface * broadphase
void removeRigidBody(btRigidBody *body) const
Removes a rigid body from the physics simulation.
static btCollisionShape * createBoxShape(const glm::vec3 &halfExtents)
Creates a collision box.
void onPhysicsComponentDestroyed(entt::registry &registry, entt::entity entity)
Cleans up the hit box pointers and motion state if applicable.
static btCompoundShape * createCompoundShape()
Creates an empty compound shape for combining multiple collision shapes.
bool sphereSweepClosest(const glm::vec3 &from, const glm::vec3 &to, float radius, Physics::RaycastHit &outHit, const btCollisionObject *ignoredObject=nullptr) const
Sweeps a sphere through the physics world and returns the closest overlap hit, if any.
static btCollisionShape * createSphereShape(float radius)
Creates a collision sphere.
static btCollisionShape * createTriangleMeshShape(const std::vector< glm::vec3 > &vertices, const std::vector< uint32_t > &indices, btTriangleMesh *&triangleMeshOut)
Creates a triangle mesh collision shape from vertices and indices.
btDiscreteDynamicsWorld * dynamicsWorld
Manages IBL (Image-Based Lighting) resources for specular reflections.
Result data for physics raycasts and sweep tests.
glm::vec3 normal
World-space surface normal at the impact point.
float distance
Distance from query origin to impact point in meters.
Components::PhysicsBody * getPhysicsBody() const
Gets the engine physics body attached to collisionObject.
const btCollisionObject * collisionObject
Bullet collision object hit by the query.
float fraction
Normalized hit fraction along the query segment.
glm::vec3 point
World-space impact point.
Rigid body create info is used to create a rigid body. It delivers all informations like the initial ...
btCollisionShape * shape
pointer to the collision shape
float friction
friction 0.0f - 1.0f. 0 is ice 0.3f - 0.5f wood or plastic. 0.7f - 1.0f High friction
float restitution
energy absorption 0.0f means full absorption on impact, 0.5f half of the energy is maintained after i...
glm::mat4 transform
initial location for the rigid body
float mass
mass in Kilograms. Mass of 0 is a static object --> immovable