Unravel Engine C++ Reference
Loading...
Searching...
No Matches
physics_material.cpp
Go to the documentation of this file.
2
3#include <fstream>
7
8namespace unravel
9{
10
12{
13
14 entt::meta_factory<combine_mode>{}
15 .type("combine_mode"_hs)
17 entt::attribute{"name", "combine_mode"},
18 entt::attribute{"pretty_name", "Combine Mode"},
19 })
20 .data<combine_mode::average>("average"_hs)
22 entt::attribute{"name", "average"},
23 entt::attribute{"pretty_name", "Average"},
24 })
25 .data<combine_mode::minimum>("minimum"_hs)
27 entt::attribute{"name", "minimum"},
28 entt::attribute{"pretty_name", "Minimum"},
29 })
30 .data<combine_mode::multiply>("multiply"_hs)
32 entt::attribute{"name", "multiply"},
33 entt::attribute{"pretty_name", "Multiply"},
34 })
35 .data<combine_mode::maximum>("maximum"_hs)
37 entt::attribute{"name", "maximum"},
38 entt::attribute{"pretty_name", "Maximum"},
39 });
40
41 // Register physics_material with entt
42 entt::meta_factory<physics_material>{}
43 .type("physics_material"_hs)
45 entt::attribute{"name", "physics_material"},
46 entt::attribute{"pretty_name", "Physics Material"},
47 })
48 .data<&physics_material::restitution>("restitution"_hs)
50 entt::attribute{"name", "restitution"},
51 entt::attribute{"pretty_name", "Restitution (Bounce)"},
52 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)."},
53 entt::attribute{"min", 0.0f},
54 entt::attribute{"max", 1.0f},
55 })
56 .data<&physics_material::friction>("friction"_hs)
58 entt::attribute{"name", "friction"},
59 entt::attribute{"pretty_name", "Friction"},
60 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."},
61 entt::attribute{"min", 0.0f},
62 entt::attribute{"max", 1.0f},
63 })
64 .data<&physics_material::stiffness>("stiffness"_hs)
66 entt::attribute{"name", "stiffness"},
67 entt::attribute{"pretty_name", "Stiffness"},
68 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)."},
69 entt::attribute{"min", 0.0f},
70 entt::attribute{"max", 1.0f},
71 })
72 .data<&physics_material::damping>("damping"_hs)
74 entt::attribute{"name", "damping"},
75 entt::attribute{"pretty_name", "Damping"},
76 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."},
77 entt::attribute{"min", 0.0f},
78 entt::attribute{"max", 1.0f},
79 })
80 .data<&physics_material::restitution_combine>("restitution_combine"_hs)
82 entt::attribute{"name", "restitution_combine"},
83 entt::attribute{"pretty_name", "Restitution Combine"},
84 entt::attribute{"tooltip", "How to combine the restitution(bounce) values of both colliders in a collision pair tocalculate the total restitution(bounce) between them."},
85 })
86 .data<&physics_material::friction_combine>("friction_combine"_hs)
88 entt::attribute{"name", "friction_combine"},
89 entt::attribute{"pretty_name", "Friction Combine"},
90 entt::attribute{"tooltip", "How to combine the friction values of both colliders in a collision pair tocalculate the total friction between them."},
91 });
92}
93
95{
96 try_save(ar, ser20::make_nvp("restitution", obj.restitution));
97 try_save(ar, ser20::make_nvp("friction", obj.friction));
98 try_save(ar, ser20::make_nvp("stiffness", obj.stiffness));
99 try_save(ar, ser20::make_nvp("damping", obj.damping));
100 try_save(ar, ser20::make_nvp("restitution_combine", obj.restitution_combine));
101 try_save(ar, ser20::make_nvp("friction_combine", obj.friction_combine));
102
103 // Changes here should be reflected in ex::get_format_version<physics_material>() in asset_extensions.h
104}
107
109{
110 try_load(ar, ser20::make_nvp("restitution", obj.restitution));
111 try_load(ar, ser20::make_nvp("friction", obj.friction));
112 try_load(ar, ser20::make_nvp("stiffness", obj.stiffness));
113 try_load(ar, ser20::make_nvp("damping", obj.damping));
114 try_load(ar, ser20::make_nvp("restitution_combine", obj.restitution_combine));
115 try_load(ar, ser20::make_nvp("friction_combine", obj.friction_combine));
116
117 // Changes here should be reflected in ex::get_format_version<physics_material>() in asset_extensions.h
118}
121
122void save_to_file(const std::string& absolute_path, const physics_material::sptr& obj)
123{
124 std::ofstream stream(absolute_path);
125 if(stream.good())
126 {
127 auto ar = ser20::create_oarchive_associative(stream);
128 try_save(ar, ser20::make_nvp("physics_material", *obj));
129 }
130}
131
132void save_to_file_bin(const std::string& absolute_path, const physics_material::sptr& obj)
133{
134 std::ofstream stream(absolute_path, std::ios::binary);
135 if(stream.good())
136 {
137 ser20::oarchive_binary_t ar(stream);
138 try_save(ar, ser20::make_nvp("physics_material", *obj));
139 }
140}
141
142void load_from_file(const std::string& absolute_path, physics_material::sptr& obj)
143{
144 fs::file_istream input(absolute_path);
145 if(!input.is_open())
146 {
147 return;
148 }
150 try_load(ar, ser20::make_nvp("physics_material", *obj));
151}
152
153void load_from_file_bin(const std::string& absolute_path, physics_material::sptr& obj)
154{
155 fs::file_istream input(absolute_path, std::ios::binary);
156 if(!input.is_open())
157 {
158 return;
159 }
161 try_load(ar, ser20::make_nvp("physics_material", *obj));
162}
163} // namespace unravel
stdio-backed input stream. Accepts std::ios open flags (e.g. std::ios::binary) when opening a
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:149
#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.