PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
rigid_body_component.h
1#pragma once
2
3#include "pixelbullet/physics/physics_types.h"
4#include "pixelbullet/serialization/node.h"
5
6namespace pixelbullet
7{
9{
10 bool enabled = true;
11 RigidBodyMotionType motion_type = RigidBodyMotionType::Dynamic;
12 float mass = 1.0f;
13 float linear_damping = 0.05f;
14 float angular_damping = 0.05f;
15 float gravity_scale = 1.0f;
16
17 [[nodiscard]] bool operator==(const RigidBodyComponent&) const noexcept = default;
18};
19
20inline Node& operator<<(Node& node, const RigidBodyComponent& component)
21{
22 node["enabled"] << component.enabled;
23 switch (component.motion_type)
24 {
25 case RigidBodyMotionType::Kinematic:
26 node["motionType"] << std::string("Kinematic");
27 break;
28 case RigidBodyMotionType::Dynamic:
29 node["motionType"] << std::string("Dynamic");
30 break;
31 default:
32 node["motionType"] << std::string("Static");
33 break;
34 }
35 node["mass"] << component.mass;
36 node["linearDamping"] << component.linear_damping;
37 node["angularDamping"] << component.angular_damping;
38 node["gravityScale"] << component.gravity_scale;
39 return node;
40}
41
42inline const Node& operator>>(const Node& node, RigidBodyComponent& component)
43{
44 if (node.HasProperty("enabled"))
45 {
46 node["enabled"] >> component.enabled;
47 }
48 if (node.HasProperty("motionType"))
49 {
50 std::string serialized_motion_type;
51 node["motionType"] >> serialized_motion_type;
52 if (serialized_motion_type == "Kinematic")
53 {
54 component.motion_type = RigidBodyMotionType::Kinematic;
55 }
56 else if (serialized_motion_type == "Dynamic")
57 {
58 component.motion_type = RigidBodyMotionType::Dynamic;
59 }
60 else
61 {
62 component.motion_type = RigidBodyMotionType::Static;
63 }
64 }
65 if (node.HasProperty("mass"))
66 {
67 node["mass"] >> component.mass;
68 }
69 if (node.HasProperty("linearDamping"))
70 {
71 node["linearDamping"] >> component.linear_damping;
72 }
73 if (node.HasProperty("angularDamping"))
74 {
75 node["angularDamping"] >> component.angular_damping;
76 }
77 if (node.HasProperty("gravityScale"))
78 {
79 node["gravityScale"] >> component.gravity_scale;
80 }
81 return node;
82}
83} // namespace pixelbullet
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45
Definition rigid_body_component.h:9