Skylicht Engine
Loading...
Searching...
No Matches
CRigidbody.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 "PhysicsEngine/ICollisionObject.h"
28
29#ifdef USE_BULLET_PHYSIC_ENGINE
30#include <btBulletCollisionCommon.h>
31#include <btBulletDynamicsCommon.h>
32#include "Bullet/CBulletUtils.h"
33
34#include "Collider/CCollider.h"
35#endif
36
37namespace Skylicht
38{
39 namespace Physics
40 {
76 class CRigidbody : public ICollisionObject
77 {
78 friend class CPhysicsEngine;
79 public:
80
92
93 protected:
94 bool m_isDynamic;
95 float m_mass;
96 float m_restitution;
97
98 float m_friction;
99 float m_rollingFriction;
100 float m_spinningFriction;
101
102 bool m_needUpdateTransform;
103
104 bool m_drawDebug;
105
106#ifdef USE_BULLET_PHYSIC_ENGINE
107 btRigidBody* m_rigidBody;
108 btCollisionShape* m_shape;
109#endif
110 int m_tag;
111 public:
112 CRigidbody();
113
114 virtual ~CRigidbody();
115
116 virtual void initComponent();
117
118 virtual void startComponent();
119
120 virtual void updateComponent();
121
122 virtual CObjectSerializable* createSerializable();
123
124 virtual void loadSerializable(CObjectSerializable* object);
125
126 virtual void setCollisionGroupAndFilter(int group, int filter);
127
132 inline void setTag(int i)
133 {
134 m_tag = i;
135 }
136
141 inline int getTag()
142 {
143 return m_tag;
144 }
145
150 void setDynamic(bool b);
151
156 inline bool isDynamic()
157 {
158 return m_isDynamic;
159 }
160
165 void setFriction(float f);
166
171 inline float getFriction()
172 {
173 return m_friction;
174 }
175
180 void setRollingFriction(float f);
181
186 inline float getRollingFriction()
187 {
188 return m_rollingFriction;
189 }
190
195 void setSpinningFriction(float f);
196
201 inline float getSpinningFriction()
202 {
203 return m_spinningFriction;
204 }
205
210 void setMass(float m);
211
216 inline float getMass()
217 {
218 return m_mass;
219 }
220
225 void setRestitution(float s);
226
231 inline float getRestitution()
232 {
233 return m_restitution;
234 }
235
241 void setSleepingThresholds(float linear, float angular);
242
247 void setCcdSweptSphereRadius(float r = 0.0f);
248
253 void setCcdMotionThreshold(float m = 0.0f);
254
260
265
270 void setLocalScale(const core::vector3df& scale);
271
276 core::vector3df getLocalScale();
277
282 core::vector3df getPosition();
283
288 void setPosition(const core::vector3df& pos);
289
294 core::vector3df getRotationEuler();
295
300 core::quaternion getRotation();
301
306 void setRotation(const core::vector3df& eulerDeg);
307
312 void setRotation(const core::quaternion& q);
313
318
324
328 void activate();
329
335
340 const char* getStateName();
341
346 void applyCenterForce(const core::vector3df& force);
347
353 void applyForce(const core::vector3df& force, const core::vector3df& localPosition);
354
359 void applyTorque(const core::vector3df& torque);
360
365
370 void applyCenterImpulse(const core::vector3df& impulse);
371
377 void applyImpulse(const core::vector3df& impulse, const core::vector3df& localPosition);
378
383 void applyTorqueImpulse(const core::vector3df& torqueImpulse);
384
389 void applyCenterPushImpulse(const core::vector3df& impulse);
390
396 void applyPushImpulse(const core::vector3df& impulse, const core::vector3df& localPosition);
397
402 void applyTorqueTurnImpulse(const core::vector3df& torqueImpulse);
403
408 core::vector3df getLinearVelocity();
409
414 core::vector3df getAngularVelocity();
415
420 void setLinearVelocity(const core::vector3df& v);
421
426 void setAngularVelocity(const core::vector3df& v);
427
433 {
434 return m_needUpdateTransform;
435 }
436
441 inline void notifyUpdateTransform(bool b)
442 {
443 m_needUpdateTransform = b;
444 }
445
450 inline void setDrawDebug(bool b)
451 {
452 m_drawDebug = b;
453 }
454
459 inline bool enableDrawDebug()
460 {
461 return m_drawDebug;
462 }
463
464#ifdef USE_BULLET_PHYSIC_ENGINE
469 inline btRigidBody* getBody()
470 {
471 return m_rigidBody;
472 }
473
478 inline btCollisionShape* getShape()
479 {
480 return m_shape;
481 }
482#endif
487 core::matrix4 getWorldTransform();
488
489 DECLARE_GETTYPENAME(CRigidbody)
490 };
491 }
492}
Utility functions for converting between Skylicht and Bullet data types.
Definition CObjectSerializable.h:36
Represents a physical rigid body in the physics simulation.
Definition CRigidbody.h:77
void setFriction(float f)
Sets the friction coefficient.
void setMass(float m)
Sets the mass of the rigid body.
void applyForce(const core::vector3df &force, const core::vector3df &localPosition)
Applies a force at a specific local position.
float getRestitution()
Gets the restitution coefficient.
Definition CRigidbody.h:231
void applyImpulse(const core::vector3df &impulse, const core::vector3df &localPosition)
Applies an instantaneous impulse at a specific local position.
core::quaternion getRotation()
Gets the world rotation as a quaternion.
virtual void setCollisionGroupAndFilter(int group, int filter)
Sets the collision group and filter mask.
float getRollingFriction()
Gets the rolling friction coefficient.
Definition CRigidbody.h:186
float getSpinningFriction()
Gets the spinning friction coefficient.
Definition CRigidbody.h:201
void releaseRigidbody()
Releases the rigid body from the physics world.
core::matrix4 getWorldTransform()
Gets the current world transform matrix.
void setState(EActivationState state)
Sets the activation state of the rigid body.
void applyCenterPushImpulse(const core::vector3df &impulse)
Applies a push impulse at the center of mass.
EActivationState getState()
Gets the current activation state.
void applyCenterImpulse(const core::vector3df &impulse)
Applies an instantaneous impulse at the center of mass.
void setRollingFriction(float f)
Sets the rolling friction coefficient.
void setCcdSweptSphereRadius(float r=0.0f)
Sets the radius for Continuous Collision Detection (CCD).
void applyPushImpulse(const core::vector3df &impulse, const core::vector3df &localPosition)
Applies a push impulse at a specific local position.
int getTag()
Gets the custom tag of the rigid body.
Definition CRigidbody.h:141
void setLinearVelocity(const core::vector3df &v)
Sets the linear velocity.
void setCcdMotionThreshold(float m=0.0f)
Sets the motion threshold for Continuous Collision Detection (CCD).
void setRotation(const core::quaternion &q)
Sets the world rotation using a quaternion.
void setDynamic(bool b)
Sets whether the body is dynamic.
bool isDynamic()
Checks if the body is dynamic.
Definition CRigidbody.h:156
float getMass()
Gets the mass of the rigid body.
Definition CRigidbody.h:216
core::vector3df getLinearVelocity()
Gets the current linear velocity.
core::vector3df getLocalScale()
Gets the local scale of the collision shape.
void setSpinningFriction(float f)
Sets the spinning friction coefficient.
void setRotation(const core::vector3df &eulerDeg)
Sets the world rotation using Euler angles.
core::vector3df getPosition()
Gets the world position of the rigid body.
void setRestitution(float s)
Sets the restitution (bounciness) coefficient.
void setSleepingThresholds(float linear, float angular)
Sets the thresholds for linear and angular velocity to enter sleep mode.
float getFriction()
Gets the friction coefficient.
Definition CRigidbody.h:171
bool enableDrawDebug()
Checks if debug drawing is enabled for this body.
Definition CRigidbody.h:459
core::vector3df getAngularVelocity()
Gets the current angular velocity.
bool initRigidbody()
Initializes the rigid body in the physics world.
void applyTorque(const core::vector3df &torque)
Applies a torque (rotational force).
void activate()
Manually activates the rigid body (wakes it up).
void applyTorqueImpulse(const core::vector3df &torqueImpulse)
Applies a torque impulse (instantaneous rotation).
void setTag(int i)
Sets a custom tag for the rigid body.
Definition CRigidbody.h:132
void applyCenterForce(const core::vector3df &force)
Applies a force at the center of mass.
void setDrawDebug(bool b)
Enables or disables debug drawing for this specific rigid body.
Definition CRigidbody.h:450
bool needUpdateTransform()
Checks if the transform needs updating.
Definition CRigidbody.h:432
void setAngularVelocity(const core::vector3df &v)
Sets the angular velocity.
void clearForce()
Clears all active forces and torques.
void setPosition(const core::vector3df &pos)
Sets the world position of the rigid body.
void setLocalScale(const core::vector3df &scale)
Sets the local scale of the collision shape.
void applyTorqueTurnImpulse(const core::vector3df &torqueImpulse)
Applies a torque turn impulse.
core::vector3df getRotationEuler()
Gets the world rotation as Euler angles.
void notifyUpdateTransform(bool b)
Notifies the component about transform update status.
Definition CRigidbody.h:441
const char * getStateName()
Gets the name of the current activation state as a string.
void syncTransform()
Synchronizes the GameObject's transform with the physics body's transform.
EActivationState
Activation states for the rigid body.
Definition CRigidbody.h:86
@ Sleep
The body is currently inactive (sleeping) to save performance.
Definition CRigidbody.h:88
@ Disable
Simulation is disabled for this body.
Definition CRigidbody.h:90
@ Activate
The body is active and simulating.
Definition CRigidbody.h:87
@ Alway
The body is always active and will not sleep.
Definition CRigidbody.h:89
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29