Unravel Engine C++ Reference
Loading...
Searching...
No Matches
physics_material.cpp
Go to the documentation of this file.
2
3#include <fstream>
6
7namespace unravel
8{
9
11{
12
13 entt::meta_factory<combine_mode>{}
14 .type("combine_mode"_hs)
16 entt::attribute{"name", "combine_mode"},
17 entt::attribute{"pretty_name", "Combine Mode"},
18 })
19 .data<combine_mode::average>("average"_hs)
21 entt::attribute{"name", "average"},
22 entt::attribute{"pretty_name", "Average"},
23 })
24 .data<combine_mode::minimum>("minimum"_hs)
26 entt::attribute{"name", "minimum"},
27 entt::attribute{"pretty_name", "Minimum"},
28 })
29 .data<combine_mode::multiply>("multiply"_hs)
31 entt::attribute{"name", "multiply"},
32 entt::attribute{"pretty_name", "Multiply"},
33 })
34 .data<combine_mode::maximum>("maximum"_hs)
36 entt::attribute{"name", "maximum"},
37 entt::attribute{"pretty_name", "Maximum"},
38 });
39
40 // Register physics_material with entt
41 entt::meta_factory<physics_material>{}
42 .type("physics_material"_hs)
44 entt::attribute{"name", "physics_material"},
45 entt::attribute{"pretty_name", "Physics Material"},
46 })
47 .data<&physics_material::restitution>("restitution"_hs)
49 entt::attribute{"name", "restitution"},
50 entt::attribute{"pretty_name", "Restitution (Bounce)"},
51 entt::attribute{"tooltip", "Restitution represents the bounciness of the material. A value of 0.0 means no bounce (perfectly inelastic collision), while 1.0 means perfect bounce (perfectly elastic collision)."},
52 entt::attribute{"min", 0.0f},
53 entt::attribute{"max", 1.0f},
54 })
55 .data<&physics_material::friction>("friction"_hs)
57 entt::attribute{"name", "friction"},
58 entt::attribute{"pretty_name", "Friction"},
59 entt::attribute{"tooltip", "Friction represents the resistance to sliding motion. A value of 0.0 means no friction (perfectly slippery), while values around 1.0 represent typical real-world friction. Values slightly above 1.0 can simulate very high friction surfaces but should be used cautiously."},
60 entt::attribute{"min", 0.0f},
61 entt::attribute{"max", 1.0f},
62 })
63 .data<&physics_material::stiffness>("stiffness"_hs)
65 entt::attribute{"name", "stiffness"},
66 entt::attribute{"pretty_name", "Stiffness"},
67 entt::attribute{"tooltip", "Stiffness represents how much force is required to deform the material. A high value means the material is very stiff (resists deformation)."},
68 entt::attribute{"min", 0.0f},
69 entt::attribute{"max", 1.0f},
70 })
71 .data<&physics_material::damping>("damping"_hs)
73 entt::attribute{"name", "damping"},
74 entt::attribute{"pretty_name", "Damping"},
75 entt::attribute{"tooltip", "Damping represents energy loss in motion (e.g., through internal friction). A value of 0.0 means no damping (energy is conserved), while 1.0 represents very high damping (rapid energy loss). Typical values range from 0.01 to 0.3 for realistic simulations."},
76 entt::attribute{"min", 0.0f},
77 entt::attribute{"max", 1.0f},
78 })
79 .data<&physics_material::restitution_combine>("restitution_combine"_hs)
81 entt::attribute{"name", "restitution_combine"},
82 entt::attribute{"pretty_name", "Restitution Combine"},
83 entt::attribute{"tooltip", "How to combine the restitution(bounce) values of both colliders in a collision pair tocalculate the total restitution(bounce) between them."},
84 })
85 .data<&physics_material::friction_combine>("friction_combine"_hs)
87 entt::attribute{"name", "friction_combine"},
88 entt::attribute{"pretty_name", "Friction Combine"},
89 entt::attribute{"tooltip", "How to combine the friction values of both colliders in a collision pair tocalculate the total friction between them."},
90 });
91}
92
94{
95 try_save(ar, ser20::make_nvp("restitution", obj.restitution));
96 try_save(ar, ser20::make_nvp("friction", obj.friction));
97 try_save(ar, ser20::make_nvp("stiffness", obj.stiffness));
98 try_save(ar, ser20::make_nvp("damping", obj.damping));
99 try_save(ar, ser20::make_nvp("restitution_combine", obj.restitution_combine));
100 try_save(ar, ser20::make_nvp("friction_combine", obj.friction_combine));
101}
104
106{
107 try_load(ar, ser20::make_nvp("restitution", obj.restitution));
108 try_load(ar, ser20::make_nvp("friction", obj.friction));
109 try_load(ar, ser20::make_nvp("stiffness", obj.stiffness));
110 try_load(ar, ser20::make_nvp("damping", obj.damping));
111 try_load(ar, ser20::make_nvp("restitution_combine", obj.restitution_combine));
112 try_load(ar, ser20::make_nvp("friction_combine", obj.friction_combine));
113}
116
117void save_to_file(const std::string& absolute_path, const physics_material::sptr& obj)
118{
119 std::ofstream stream(absolute_path);
120 if(stream.good())
121 {
122 auto ar = ser20::create_oarchive_associative(stream);
123 try_save(ar, ser20::make_nvp("physics_material", *obj));
124 }
125}
126
127void save_to_file_bin(const std::string& absolute_path, const physics_material::sptr& obj)
128{
129 std::ofstream stream(absolute_path, std::ios::binary);
130 if(stream.good())
131 {
132 ser20::oarchive_binary_t ar(stream);
133 try_save(ar, ser20::make_nvp("physics_material", *obj));
134 }
135}
136
137void load_from_file(const std::string& absolute_path, physics_material::sptr& obj)
138{
139 std::ifstream stream(absolute_path);
140 if(stream.good())
141 {
142 auto ar = ser20::create_iarchive_associative(stream);
143 try_load(ar, ser20::make_nvp("physics_material", *obj));
144 }
145}
146
147void load_from_file_bin(const std::string& absolute_path, physics_material::sptr& obj)
148{
149 std::ifstream stream(absolute_path, std::ios::binary);
150 if(stream.good())
151 {
152 ser20::iarchive_binary_t ar(stream);
153 try_load(ar, ser20::make_nvp("physics_material", *obj));
154 }
155}
156} // namespace unravel
attributes::value_type attribute
Definition reflection.h:19
std::map< std::string, meta_any > attributes
Definition reflection.h:18
auto create_oarchive_associative(std::ostream &stream)
BinaryInputArchive iarchive_binary_t
auto create_iarchive_associative(std::istream &stream)
simd::JSONOutputArchive oarchive_associative_t
BinaryOutputArchive oarchive_binary_t
simd::JSONInputArchive iarchive_associative_t
void save_to_file_bin(const std::string &absolute_path, const animation_clip &obj)
void load_from_file(const std::string &absolute_path, animation_clip &obj)
void save_to_file(const std::string &absolute_path, const animation_clip &obj)
void load_from_file_bin(const std::string &absolute_path, animation_clip &obj)
#define REFLECT(cls)
Definition reflection.h:133
#define SAVE_INSTANTIATE(cls, Archive)
#define LOAD(cls)
auto try_save(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
#define LOAD_INSTANTIATE(cls, Archive)
#define SAVE(cls)
auto try_load(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
Represents the physical properties of a material.
std::shared_ptr< physics_material > sptr
Shared pointer to a physics material.