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#include "scene/GameObject.h"
3#include "Engine.h"
4
5namespace eng
6{
7 PhysicsComponent::PhysicsComponent(const std::shared_ptr<RigidBody>& body)
8 : m_rigidBody(body)
9 {
10 }
11
12 void PhysicsComponent::LoadProperties(const nlohmann::json& json)
13 {
14 std::shared_ptr<Collider> collider;
15
16 if (json.contains("collider"))
17 {
18 const auto& colliderObj = json["collider"];
19 std::string type = colliderObj.value("type", "");
20
21 if (type == "box")
22 {
23 glm::vec3 extents(
24 colliderObj.value("x", 1.0f),
25 colliderObj.value("y", 1.0f),
26 colliderObj.value("z", 1.0f));
27 collider = std::make_shared<BoxCollider>(extents);
28 }
29 else if (type == "sphere")
30 {
31 float radius = colliderObj.value("r", 1.0f);
32 collider = std::make_shared<SphereCollider>(radius);
33 }
34 else if (type == "capsule")
35 {
36 float radius = colliderObj.value("r", 1.0f);
37 float height = colliderObj.value("h", 1.0f);
38 collider = std::make_shared<CapsuleCollider>(radius, height);
39 }
40
41 if (!collider)
42 {
43 return;
44 }
45
46 std::shared_ptr<RigidBody> rigidBody;
47 if (json.contains("body"))
48 {
49 const auto& bodyObj = json["body"];
50
51 float mass = bodyObj.value("mass", 0.0f);
52 float friction = bodyObj.value("friction", 0.5f);
53 std::string typeStr = bodyObj.value("type", "static");
54
56 if (typeStr == "dynamic")
57 {
58 type = BodyType::Dynamic;
59 }
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 }
74
76 {
77 if (!m_rigidBody)
78 {
79 return;
80 }
81
82 const auto pos = m_owner->GetWorldPosition();
83 const auto rot = m_owner->GetWorldRotation();
84
85 m_rigidBody->SetPosition(pos);
86 m_rigidBody->SetRotation(rot);
87
89 }
90
91 void 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
105 void PhysicsComponent::SetRigidBody(const std::shared_ptr<RigidBody>& body)
106 {
107 m_rigidBody = body;
108 }
109
110 const std::shared_ptr<RigidBody>& PhysicsComponent::GetRigidBody()
111 {
112 return m_rigidBody;
113 }
114}
GameObject * m_owner
Definition Component.h:33
static Engine & GetInstance()
Definition Engine.cpp:50
PhysicsManager & GetPhysicsManager()
Definition Engine.cpp:215
glm::quat GetWorldRotation()
void SetWorldRotation(const glm::quat &rot)
glm::vec3 GetWorldPosition() const
void SetWorldPosition(const glm::vec3 &pos)
void LoadProperties(const nlohmann::json &json) override
PhysicsComponent()=default
void SetRigidBody(const std::shared_ptr< RigidBody > &body)
const std::shared_ptr< RigidBody > & GetRigidBody()
void Update(float deltaTime) override
void AddRigidBody(RigidBody *body)
BodyType
Definition RigidBody.h:14