Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
PhysicsComponent.cpp
Go to the documentation of this file.
1#include "scene/components/PhysicsComponent.h"
2
3#include "Engine.h"
4#include "scene/GameObject.h"
5
6namespace mnd
7{
8PhysicsComponent::PhysicsComponent(const std::shared_ptr<RigidBody> &body)
9 : m_rigidBody(body)
10{
11}
12
13void PhysicsComponent::LoadProperties(const nlohmann::json &json)
14{
15 std::shared_ptr<Collider> collider;
16
17 // Collider
18 if (json.contains("collider"))
19 {
20 const auto &col = json["collider"];
21 std::string type = col.value("type", "");
22
23 if (type == "box")
24 {
25 glm::vec3 extents(col.value("x", 1.0f), col.value("y", 1.0f), col.value("z", 1.0f));
26 collider = std::make_shared<BoxCollider>(extents);
27
28 } else if (type == "sphere")
29 {
30 float radius = col.value("r", 1.0f);
31 collider = std::make_shared<SphereCollider>(radius);
32
33 } else if (type == "capsule")
34 {
35 float radius = col.value("r", 1.0f);
36 float height = col.value("h", 1.0f);
37 collider = std::make_shared<CapsuleCollider>(radius, height);
38 }
39 }
40
41 if (!collider)
42 {
43 return;
44 }
45
46 // RigidBody
47 std::shared_ptr<RigidBody> rigidbody;
48 if (json.contains("body"))
49 {
50 const auto &body = json["body"];
51
52 float mass = body.value("mass", 0.0f);
53 float friction = body.value("friction", 0.5f);
54 std::string typeStr = body.value("type", "static");
55
57 if (typeStr == "dynamic")
58 {
59 type = BodyType::Dynamic;
60 } else if (typeStr == "kinematic")
61 {
63 }
64
65 rigidbody = std::make_shared<RigidBody>(type, collider, mass, friction);
66 }
67
68 if (rigidbody)
69 {
70 SetRigidBody(rigidbody);
71 }
72}
73
75{
76 if (!m_rigidBody)
77 {
78 return;
79 }
80
81 const auto pos = m_owner->GetWorldPosition();
82 const auto rot = m_owner->GetWorldRotation();
83
84 m_rigidBody->SetPosition(pos);
85 m_rigidBody->SetRotation(rot);
86 m_rigidBody->SetOwner(m_owner);
87
89}
90
91void PhysicsComponent::Update(float deltaTime)
92{
93 if (!m_rigidBody)
94 {
95 return;
96 }
97
98 if (m_rigidBody->GetType() == BodyType::Dynamic)
99 {
100 m_owner->SetWorldPosition(m_rigidBody->GetPosition());
101 m_owner->SetWorldRotation(m_rigidBody->GetRotation());
102 }
103}
104
105void PhysicsComponent::SetRigidBody(const std::shared_ptr<RigidBody> &body)
106{
107 m_rigidBody = body;
108}
109
110const std::shared_ptr<RigidBody> &PhysicsComponent::GetRigidBody()
111{
112 return m_rigidBody;
113}
114
115} // namespace mnd
GameObject * m_owner
Definition Component.h:57
static Engine & GetInstance()
Returns the single Engine instance (created on first call).
Definition Engine.cpp:59
PhysicsManager & GetPhysicsManager()
Definition Engine.cpp:443
vec3 GetWorldPosition() const
Extract the world-space position from GetWorldTransform().
quat GetWorldRotation()
void SetWorldRotation(const quat &rot)
void SetWorldPosition(const vec3 &pos)
const std::shared_ptr< RigidBody > & GetRigidBody()
void LoadProperties(const nlohmann::json &json) override
Parse collider + body sub-objects and build the RigidBody.
void Update(float deltaTime) override
Copy the simulated pose onto the owning GameObject.
void SetRigidBody(const std::shared_ptr< RigidBody > &body)
Replace the component's rigid body at runtime.
PhysicsComponent()=default
void Init() override
Register the rigid body with the PhysicsManager.
void AddRigidBody(RigidBody *body)
Register a rigid body so it participates in simulation and collision.
BodyType
Simulation category for a RigidBody.
Definition RigidBody.h:29