Skylicht Engine
Loading...
Searching...
No Matches
CPhysicsEngine.h
1/*
2!@
3MIT License
4
5Copyright (c) 2024 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "Utils/CSingleton.h"
28#include "Transform/CTransformMatrix.h"
29#include "CPhysicsRaycast.h"
30#include "CDrawDebug.h"
31
32#ifdef USE_BULLET_PHYSIC_ENGINE
33#include <btBulletCollisionCommon.h>
34#include <btBulletDynamicsCommon.h>
35#include <BulletDynamics/Character/btKinematicCharacterController.h>
36#endif
37
38namespace Skylicht
39{
40 namespace Physics
41 {
42 class CRigidbody;
44
50 {
51 CTransform* Transform;
52 CRigidbody* Body;
53#ifdef USE_BULLET_PHYSIC_ENGINE
54 btRigidBody* BulletBody;
55#endif
56 };
57
63 {
64 CTransform* Transform;
65 CCharacterController* Controller;
66#ifdef USE_BULLET_PHYSIC_ENGINE
67 btKinematicCharacterController* BulletCharacter;
68 btPairCachingGhostObject* GhostObject;
69#endif
70 };
71
113 class CPhysicsEngine
114 {
115 friend class CRigidbody;
116 friend class CCharacterController;
117
118 public:
119 DECLARE_SINGLETON(CPhysicsEngine);
120
125
126 protected:
127#ifdef USE_BULLET_PHYSIC_ENGINE
128 btOverlappingPairCallback* m_overlapCB;
129 btBroadphaseInterface* m_broadphase;
130 btCollisionDispatcher* m_dispatcher;
131 btConstraintSolver* m_solver;
132 btDefaultCollisionConfiguration* m_collisionConfiguration;
133 btDiscreteDynamicsWorld* m_dynamicsWorld;
134
135 CDrawDebug* m_drawDebug;
136#endif
137 float m_gravity;
138
140 core::array<SCharacterData*> m_characters;
141
142 bool m_enableDrawDebug;
143
144 public:
145 CPhysicsEngine();
146
147 virtual ~CPhysicsEngine();
148
153
158
164
170 void updatePhysics(float timestepSec, int step = 1);
171
176
181
187 void enableDrawDebug(bool b, bool alwayDraw = false);
188
193 inline float getGravity()
194 {
195 return m_gravity;
196 }
197
202 void setGravity(float g);
203
208
216 bool rayTest(const core::vector3df& from, const core::vector3df& to, SAllRaycastResult& result);
217
225 bool rayTest(const core::vector3df& from, const core::vector3df& to, SClosestRaycastResult& result);
226
232 {
233 return m_bodies;
234 }
235
241 {
242 return m_characters;
243 }
244
245 private:
246
247#ifdef USE_BULLET_PHYSIC_ENGINE
248 inline btDiscreteDynamicsWorld* getDynamicsWorld()
249 {
250 return m_dynamicsWorld;
251 }
252#endif
253 void addBody(CRigidbody* body);
254
255 void removeBody(CRigidbody* body);
256
257 void addCharacter(CCharacterController* character);
258
259 void removeCharacter(CCharacterController* character);
260
261 void syncTransforms();
262
263 void checkCollision();
264 };
265 }
266}
An abstract object class for classes that describe a GameObject's Transform.
Definition CTransform.h:47
A kinematic character controller for handling player movement.
Definition CCharacterController.h:69
bool rayTest(const core::vector3df &from, const core::vector3df &to, SClosestRaycastResult &result)
Performs a raycast test returning only the closest hit.
bool IsInEditor
Indicates if the engine is running within the editor.
Definition CPhysicsEngine.h:124
bool isInitialized()
Checks if the physics engine has been initialized.
core::array< SRigidbodyData * > & getBodies()
Gets all registered rigid bodies.
Definition CPhysicsEngine.h:231
void updatePhysics(float timestepSec, int step=1)
Steps the physics simulation.
void syncTransformToPhysics()
Synchronizes Skylicht transforms to the physics engine.
void initPhysics()
Initializes the physics world and configuration.
void exitPhysics()
Cleans up and releases physics world resources.
void setGravity(float g)
Sets the gravity on the Y-axis.
core::array< SCharacterData * > & getCharacters()
Gets all registered character controllers.
Definition CPhysicsEngine.h:240
bool rayTest(const core::vector3df &from, const core::vector3df &to, SAllRaycastResult &result)
Performs a raycast test returning all hits.
void debugDrawWorld()
Draws the physics world for debugging.
void updateAABBs()
Updates the Axis-Aligned Bounding Boxes (AABBs) for all objects.
void enableDrawDebug(bool b, bool alwayDraw=false)
Enables or disables physics debug visualization.
float getGravity()
Gets the current gravity value.
Definition CPhysicsEngine.h:193
Represents a physical rigid body in the physics simulation.
Definition CRigidbody.h:77
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
#define DECLARE_SINGLETON(className)
Declare the standard singleton accessors for a class.
Definition CSingleton.h:36
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
Result structure for a raycast that returns all hits.
Definition CPhysicsRaycast.h:39
Data structure for managing character controller within the physics engine.
Definition CPhysicsEngine.h:63
Result structure for a raycast that returns only the closest hit.
Definition CPhysicsRaycast.h:56
Data structure for managing rigid body within the physics engine.
Definition CPhysicsEngine.h:50