PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
collider_component.h
1#pragma once
2
3#include "pixelbullet/physics/physics_types.h"
4#include "pixelbullet/serialization/glm_node.h"
5#include "pixelbullet/serialization/node.h"
6
7namespace pixelbullet
8{
10{
11 bool enabled = true;
12 ColliderShapeType shape = ColliderShapeType::Box;
13 glm::vec3 box_half_extents = glm::vec3(0.5f);
14 float sphere_radius = 0.5f;
15 float capsule_radius = 0.5f;
16 float capsule_half_height = 0.5f;
17 glm::vec3 center = glm::vec3(0.0f);
18 float friction = 0.5f;
19 float restitution = 0.0f;
20 bool is_trigger = false;
21
22 [[nodiscard]] bool operator==(const ColliderComponent&) const noexcept = default;
23};
24
25inline Node& operator<<(Node& node, const ColliderComponent& component)
26{
27 node["enabled"] << component.enabled;
28 std::string shape = "Box";
29 if (component.shape == ColliderShapeType::Sphere)
30 {
31 shape = "Sphere";
32 }
33 else if (component.shape == ColliderShapeType::Capsule)
34 {
35 shape = "Capsule";
36 }
37 node["shape"] << shape;
38 node["boxHalfExtents"] << component.box_half_extents;
39 node["sphereRadius"] << component.sphere_radius;
40 node["capsuleRadius"] << component.capsule_radius;
41 node["capsuleHalfHeight"] << component.capsule_half_height;
42 node["center"] << component.center;
43 node["friction"] << component.friction;
44 node["restitution"] << component.restitution;
45 node["isTrigger"] << component.is_trigger;
46 return node;
47}
48
49inline const Node& operator>>(const Node& node, ColliderComponent& component)
50{
51 if (node.HasProperty("enabled"))
52 {
53 node["enabled"] >> component.enabled;
54 }
55 if (node.HasProperty("shape"))
56 {
57 std::string serialized_shape;
58 node["shape"] >> serialized_shape;
59 if (serialized_shape == "Sphere")
60 {
61 component.shape = ColliderShapeType::Sphere;
62 }
63 else if (serialized_shape == "Capsule")
64 {
65 component.shape = ColliderShapeType::Capsule;
66 }
67 else
68 {
69 component.shape = ColliderShapeType::Box;
70 }
71 }
72 if (node.HasProperty("boxHalfExtents"))
73 {
74 node["boxHalfExtents"] >> component.box_half_extents;
75 }
76 if (node.HasProperty("sphereRadius"))
77 {
78 node["sphereRadius"] >> component.sphere_radius;
79 }
80 if (node.HasProperty("capsuleRadius"))
81 {
82 node["capsuleRadius"] >> component.capsule_radius;
83 }
84 if (node.HasProperty("capsuleHalfHeight"))
85 {
86 node["capsuleHalfHeight"] >> component.capsule_half_height;
87 }
88 if (node.HasProperty("center"))
89 {
90 node["center"] >> component.center;
91 }
92 if (node.HasProperty("friction"))
93 {
94 node["friction"] >> component.friction;
95 }
96 if (node.HasProperty("restitution"))
97 {
98 node["restitution"] >> component.restitution;
99 }
100 if (node.HasProperty("isTrigger"))
101 {
102 node["isTrigger"] >> component.is_trigger;
103 }
104 else
105 {
106 component.is_trigger = false;
107 }
108 return node;
109}
110} // namespace pixelbullet
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45
Definition collider_component.h:10