Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
RigidBody.cpp
Go to the documentation of this file.
1#include "physics/RigidBody.h"
2
3#include <btBulletCollisionCommon.h>
4#include <btBulletDynamicsCommon.h>
5
6#include "Engine.h"
7#include "LinearMath/btVector3.h"
8#include "physics/CollisionObject.h"
9
10namespace mnd
11{
12RigidBody::RigidBody(BodyType type, const std::shared_ptr<Collider> &collider, float mass, float friction)
13 : m_type(type)
14 , m_collider(collider)
15 , m_mass(mass)
16 , m_friction(friction)
17{
18 if (!collider)
19 {
20 return;
21 }
22
24
25 btVector3 inertia(0, 0, 0);
26 if (m_type == BodyType::Dynamic && mass > 0.0f && m_collider->GetShape())
27 {
28 m_collider->GetShape()->calculateLocalInertia(btScalar(mass), inertia);
29 }
30
31 btTransform transform;
32 transform.setIdentity();
33 btDefaultMotionState *motionState = new btDefaultMotionState(transform);
34
35 btRigidBody::btRigidBodyConstructionInfo info(
36 (m_type == BodyType::Dynamic) ? btScalar(mass) : btScalar(0), motionState, m_collider->GetShape(), inertia
37 );
38
39 m_body = std::make_unique<btRigidBody>(info);
40 m_body->setFriction(friction);
41 m_body->setUserPointer(this);
42
43 if (m_type == BodyType::Kinematic)
44 {
45 m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
46 m_body->setActivationState(DISABLE_DEACTIVATION);
47 }
48}
49
51{
52 if (m_addedToWorld)
53 {
55 }
56}
57
58btRigidBody *RigidBody::GetBody()
59{
60 return m_body.get();
61}
62
64{
65 m_addedToWorld = added;
66}
67
69{
70 return m_addedToWorld;
71}
72
74{
75 return m_type;
76}
77
78void RigidBody::SetPosition(const glm::vec3 &pos)
79{
80 if (!m_body)
81 {
82 return;
83 }
84
85 auto &tr = m_body->getWorldTransform();
86 tr.setOrigin(btVector3(btScalar(pos.x), btScalar(pos.y), btScalar(pos.z)));
87 if (m_body->getMotionState())
88 {
89 m_body->getMotionState()->setWorldTransform(tr);
90 }
91 m_body->setWorldTransform(tr);
92}
93
94glm::vec3 RigidBody::GetPosition() const
95{
96 if (!m_body)
97 {
98 return vec3(0.0f, 0.0f, 0.0f);
99 }
100 const auto &pos = m_body->getWorldTransform().getOrigin();
101 return glm::vec3(pos.x(), pos.y(), pos.z());
102}
103
104void RigidBody::SetRotation(const glm::quat &rot)
105{
106 if (!m_body)
107 {
108 return;
109 }
110
111 auto &tr = m_body->getWorldTransform();
112 tr.setRotation(btQuaternion(btScalar(rot.x), btScalar(rot.y), btScalar(rot.z), btScalar(rot.w)));
113 if (m_body->getMotionState())
114 {
115 m_body->getMotionState()->setWorldTransform(tr);
116 }
117 m_body->setWorldTransform(tr);
118}
119
120glm::quat RigidBody::GetRotation() const
121{
122 if (!m_body)
123 {
124 return quat(1.0f, 0.0f, 0.0f, 0.0f);
125 }
126 const auto &rot = m_body->getWorldTransform().getRotation();
127 return glm::quat(rot.w(), rot.x(), rot.y(), rot.z());
128}
129
130void RigidBody::ApplyImpulse(const vec3 &impulse)
131{
132 if (!m_body)
133 {
134 return;
135 }
136 m_body->applyCentralImpulse(btVector3(btScalar(impulse.x), btScalar(impulse.y), btScalar(impulse.z)));
137}
138
139void RigidBody::EnableCcd(float motionThreshold, float sweptRadius)
140{
141 if (!m_body)
142 {
143 return;
144 }
145 m_body->setCcdMotionThreshold(btScalar(motionThreshold));
146 m_body->setCcdSweptSphereRadius(btScalar(sweptRadius));
147}
148
149} // namespace mnd
CollisionObjectType m_collisionObjType
static Engine & GetInstance()
Returns the single Engine instance (created on first call).
Definition Engine.cpp:59
PhysicsManager & GetPhysicsManager()
Definition Engine.cpp:443
void RemoveRigidBody(RigidBody *body)
Unregister a rigid body (e.g. when its owning component is destroyed).
glm::quat GetRotation() const
glm::vec3 GetPosition() const
Definition RigidBody.cpp:94
void EnableCcd(float motionThreshold, float sweptRadius)
void SetPosition(const glm::vec3 &pos)
Teleport the body (bypasses the solver — use sparingly on Dynamic bodies).
Definition RigidBody.cpp:78
RigidBody(BodyType type, const std::shared_ptr< Collider > &collider, float mass, float friction)
Definition RigidBody.cpp:12
bool IsAddedToWorld() const
Definition RigidBody.cpp:68
void SetRotation(const glm::quat &rot)
btRigidBody * GetBody()
Underlying Bullet body (borrowed, do not delete).
Definition RigidBody.cpp:58
BodyType GetType() const
Definition RigidBody.cpp:73
void ApplyImpulse(const vec3 &impulse)
void SetAddedToWorld(bool added)
Track whether this body is currently registered with a PhysicsManager.
Definition RigidBody.cpp:63
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
Definition Types.h:51
glm::quat quat
Unit quaternion for rotation (avoids gimbal lock).
Definition Types.h:65
BodyType
Simulation category for a RigidBody.
Definition RigidBody.h:29