Hymn to Beauty
C++ 3D Engine
RigidBody.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <glm/glm.hpp>
4#include <memory>
5#include "SuperComponent.hpp"
6
7class btCollisionObject;
8class btGhostObject;
9class btRigidBody;
10class PhysicsManager;
11
12namespace Physics {
13class Shape;
14class Trigger;
15} // namespace Physics
16
17namespace Component {
19class RigidBody : public SuperComponent {
20 friend class ::PhysicsManager;
21 friend class ::Physics::Trigger;
22 friend class Shape;
23
24 public:
26 RigidBody() = default;
27
29 ~RigidBody();
30
31 void Serialize(Json::Value& node, bool load) override;
32
36
39 bool IsKinematic() const;
40
45
48 bool IsGhost() const;
49
53
56 float GetFriction() const;
57
59
62 float GetRollingFriction() const;
63
65
68 float GetSpinningFriction() const;
69
71
74 float GetRestitution() const;
75
77
80 float GetLinearDamping() const;
81
83
86 float GetAngularDamping() const;
87
88 private:
89 // Get the underlying Bullet rigid body. If none has been set,
90 // nullptr is returned.
91 btRigidBody* GetBulletRigidBody();
92
93 // Get the underlying Bullet collision object. If none has been
94 // set, nullptr is returned. If the rigid body is not a ghost,
95 // the returned collision object is the rigid body.
96 btCollisionObject* GetBulletCollisionObject();
97
98 // Creates the underlying Bullet rigid body. Mass is provided in
99 // units of kilograms.
100 void NewBulletRigidBody(float mass);
101
102 // Destroy resources completely.
103 void Destroy();
104
105 void SetCollisionShape(std::shared_ptr<Physics::Shape> shape);
106
107 // Get the position of a rigid body.
108 glm::vec3 GetPosition() const;
109
110 // Set the position of a rigid body.
111 void SetPosition(const glm::vec3& pos);
112
113 // Get the orientation of a rigid body.
114 glm::quat GetOrientation() const;
115
116 // Set the orientation of a rigid body.
117 void SetOrientation(const glm::quat& rotation);
118
119 // Get the mass in kilograms of a rigid body.
120 float GetMass();
121
122 // Set the mass in kilograms of a rigid body.
123 void SetMass(float mass);
124
125 void SetFriction(float friction);
126 void SetRollingFriction(float friction);
127 void SetSpinningFriction(float friction);
128
129 void SetRestitution(float cor);
130
131 void SetLinearDamping(float damping);
132 void SetAngularDamping(float damping);
133
134 void MakeKinematic();
135 void MakeDynamic();
136 void SetGhost(bool ghost);
137
138 // Get/set whether a dynamic rigid body should synchronize its
139 // transform against the owning entity during the next simulation.
140 bool GetForceTransformSync() const;
141 void SetForceTransformSync(bool sync);
142
143 // Get/set whether a kinematic rigid body should halt its movement
144 // during the next simulation frame.
145 bool GetHaltMovement() const;
146 void SetHaltMovement(bool halt);
147
148 float mass = 1.0f;
149 btRigidBody* rigidBody = nullptr;
150 btGhostObject* ghostObject = nullptr;
151 bool kinematic = false;
152 bool forceTransformSync = true; // For first frame
153 bool haltMovement = true; // True for first frame
154 float friction = 0.0f;
155 float rollingFriction = 0.0f;
156 float spinningFriction = 0.0f;
157 float restitution = 0.0f;
158 float linearDamping = 0.0f;
159 float angularDamping = 0.0f;
160 bool ghost = false;
161 std::shared_ptr<Physics::Shape> shape;
162};
163} // namespace Component
Component that allows interacting with other physics components.
Definition: RigidBody.hpp:19
float GetLinearDamping() const
Get the linear damping of the rigid body.
Definition: RigidBody.cpp:76
float GetFriction() const
Definition: RigidBody.cpp:60
void Serialize(Json::Value &node, bool load) override
Save or load component values to/from JSON.
Definition: RigidBody.cpp:18
float GetSpinningFriction() const
Get the spinning friction coefficient of the rigid body.
Definition: RigidBody.cpp:68
bool IsGhost() const
Definition: RigidBody.cpp:56
~RigidBody()
Destructor.
Definition: RigidBody.cpp:14
float GetRestitution() const
Get the coefficient of restitution of the rigid body.
Definition: RigidBody.cpp:72
float GetRollingFriction() const
Get the rolling friction coefficient of the rigid body.
Definition: RigidBody.cpp:64
float GetAngularDamping() const
Get the angular damping of the rigid body.
Definition: RigidBody.cpp:80
bool IsKinematic() const
Definition: RigidBody.cpp:52
RigidBody()=default
Constructor.
Definition: Shape.hpp:15
Component which all components inherit.
Definition: SuperComponent.hpp:9
Updates the physics of the world.
Definition: PhysicsManager.hpp:28
Definition: BoxShapeEditor.hpp:5
Definition: IShapeEditor.hpp:7