Unravel Engine C++ Reference
Loading...
Searching...
No Matches
physics_component.cpp
Go to the documentation of this file.
5
9
10namespace unravel
11{
12
14{
15 entt::meta_factory<physics_box_shape>{}
16 .type("physics_box_shape"_hs)
18 entt::attribute{"name", "physics_box_shape"},
19 entt::attribute{"pretty_name", "Box"},
20 })
21 .data<&physics_box_shape::center>("center"_hs)
23 entt::attribute{"name", "center"},
24 entt::attribute{"pretty_name", "Center"},
25 entt::attribute{"tooltip", "The center of the collider."},
26 })
27 .data<&physics_box_shape::extends>("extends"_hs)
29 entt::attribute{"name", "extends"},
30 entt::attribute{"pretty_name", "Extends"},
31 entt::attribute{"tooltip", "The extends of the collider."},
32 });
33}
34
36{
37 try_save(ar, ser20::make_nvp("center", obj.center));
38 try_save(ar, ser20::make_nvp("extends", obj.extends));
39}
42
44{
45 try_load(ar, ser20::make_nvp("center", obj.center));
46 try_load(ar, ser20::make_nvp("extends", obj.extends));
47}
48
51
53{
54 entt::meta_factory<physics_sphere_shape>{}
55 .type("physics_sphere_shape"_hs)
57 entt::attribute{"name", "physics_sphere_shape"},
58 entt::attribute{"pretty_name", "Sphere"},
59 })
60 .data<&physics_sphere_shape::center>("center"_hs)
62 entt::attribute{"name", "center"},
63 entt::attribute{"pretty_name", "Center"},
64 entt::attribute{"tooltip", "The center of the collider."},
65 })
66 .data<&physics_sphere_shape::radius>("radius"_hs)
68 entt::attribute{"name", "radius"},
69 entt::attribute{"pretty_name", "Radius"},
70 entt::attribute{"tooltip", "The radius of the collider."},
71 entt::attribute{"min", 0.0f},
72 entt::attribute{"step", 0.1f},
73 });
74}
75
77{
78 try_save(ar, ser20::make_nvp("center", obj.center));
79 try_save(ar, ser20::make_nvp("radius", obj.radius));
80}
83
85{
86 try_load(ar, ser20::make_nvp("center", obj.center));
87 try_load(ar, ser20::make_nvp("radius", obj.radius));
88}
89
92
94{
95 entt::meta_factory<physics_capsule_shape>{}
96 .type("physics_capsule_shape"_hs)
98 entt::attribute{"name", "physics_capsule_shape"},
99 entt::attribute{"pretty_name", "Capsule"},
100 })
101 .data<&physics_capsule_shape::center>("center"_hs)
103 entt::attribute{"name", "center"},
104 entt::attribute{"pretty_name", "Center"},
105 entt::attribute{"tooltip", "The center of the collider."},
106 })
107 .data<&physics_capsule_shape::radius>("radius"_hs)
109 entt::attribute{"name", "radius"},
110 entt::attribute{"pretty_name", "Radius"},
111 entt::attribute{"tooltip", "The radius of the collider."},
112 entt::attribute{"min", 0.0f},
113 entt::attribute{"step", 0.1f},
114 })
115 .data<&physics_capsule_shape::length>("length"_hs)
117 entt::attribute{"name", "length"},
118 entt::attribute{"pretty_name", "Length"},
119 entt::attribute{"tooltip", "The length of the collider."},
120 entt::attribute{"min", 0.0f},
121 entt::attribute{"step", 0.1f},
122 });
123}
124
126{
127 try_save(ar, ser20::make_nvp("center", obj.center));
128 try_save(ar, ser20::make_nvp("radius", obj.radius));
129 try_save(ar, ser20::make_nvp("length", obj.length));
130}
133
135{
136 try_load(ar, ser20::make_nvp("center", obj.center));
137 try_load(ar, ser20::make_nvp("radius", obj.radius));
138 try_load(ar, ser20::make_nvp("length", obj.length));
139}
140
143
145{
146 entt::meta_factory<physics_cylinder_shape>{}
147 .type("physics_cylinder_shape"_hs)
149 entt::attribute{"name", "physics_cylinder_shape"},
150 entt::attribute{"pretty_name", "Cylinder"},
151 })
152 .data<&physics_cylinder_shape::center>("center"_hs)
154 entt::attribute{"name", "center"},
155 entt::attribute{"pretty_name", "Center"},
156 entt::attribute{"tooltip", "The center of the collider."},
157 })
158 .data<&physics_cylinder_shape::radius>("radius"_hs)
160 entt::attribute{"name", "radius"},
161 entt::attribute{"pretty_name", "Radius"},
162 entt::attribute{"tooltip", "The radius of the collider."},
163 entt::attribute{"min", 0.0f},
164 entt::attribute{"step", 0.1f},
165 })
166 .data<&physics_cylinder_shape::length>("length"_hs)
168 entt::attribute{"name", "length"},
169 entt::attribute{"pretty_name", "Length"},
170 entt::attribute{"tooltip", "The length of the collider."},
171 entt::attribute{"min", 0.0f},
172 entt::attribute{"step", 0.1f},
173 });
174}
175
177{
178 try_save(ar, ser20::make_nvp("center", obj.center));
179 try_save(ar, ser20::make_nvp("radius", obj.radius));
180 try_save(ar, ser20::make_nvp("length", obj.length));
181}
184
186{
187 try_load(ar, ser20::make_nvp("center", obj.center));
188 try_load(ar, ser20::make_nvp("radius", obj.radius));
189 try_load(ar, ser20::make_nvp("length", obj.length));
190}
191
194
196{
197 static const auto& ps = entt::resolve<physics_box_shape>();
198 static const auto& ss = entt::resolve<physics_sphere_shape>();
199 static const auto& cs = entt::resolve<physics_capsule_shape>();
200 static const auto& cys = entt::resolve<physics_cylinder_shape>();
201
202 std::vector<entt::meta_type> variant_types{ps, ss, cs, cys};
203
204 // Register physics_compound_shape with entt
205 entt::meta_factory<physics_compound_shape>{}
206 .type("physics_compound_shape"_hs)
208 entt::attribute{"name", "physics_compound_shape"},
209 entt::attribute{"pretty_name", "Shape"},
210 entt::attribute{"variant_types", variant_types}
211 });
212
213}
214
216{
217 try_save(ar, ser20::make_nvp("shape", obj.shape));
218}
221
223{
224 try_load(ar, ser20::make_nvp("shape", obj.shape));
225}
226
229
231{
232 entt::meta_factory<physics_component>{}
233 .type("physics_component"_hs)
235 entt::attribute{"name", "physics_component"},
236 entt::attribute{"category", "PHYSICS"},
237 entt::attribute{"pretty_name", "Physics"},
238 })
239 .func<&component_meta<physics_component>::exists>("component_exists"_hs)
240 .func<&component_meta<physics_component>::add>("component_add"_hs)
241 .func<&component_meta<physics_component>::remove>("component_remove"_hs)
242 .func<&component_meta<physics_component>::save>("component_save"_hs)
243 .func<&component_meta<physics_component>::load>("component_load"_hs)
245 .custom<entt::attributes>(entt::attributes{
246 entt::attribute{"name", "is_using_gravity"},
247 entt::attribute{"pretty_name", "Use Gravity"},
248 entt::attribute{"tooltip", "Simulate gravity for this rigidbody."},
249 })
250 .data<&physics_component::set_is_kinematic, &physics_component::is_kinematic>("is_kinematic"_hs)
252 entt::attribute{"name", "is_kinematic"},
253 entt::attribute{"pretty_name", "Is Kinematic"},
254 entt::attribute{"tooltip", "Is the rigidbody kinematic(A rigid body that is not affected by others and can be moved directly.)"},
255 })
256 .data<&physics_component::set_is_sensor, &physics_component::is_sensor>("is_sensor"_hs)
258 entt::attribute{"name", "is_sensor"},
259 entt::attribute{"pretty_name", "Is Sensor"},
260 entt::attribute{"tooltip", "The rigidbody will not respond to collisions, i.e. it becomes a _sensor_."},
261 })
262 .data<&physics_component::set_is_autoscaled, &physics_component::is_autoscaled>("is_autoscaled"_hs)
264 entt::attribute{"name", "is_autoscaled"},
265 entt::attribute{"pretty_name", "Is Auto Scaled"},
266 entt::attribute{"tooltip", "Enables/Disables shape auto scale with transform."},
267 })
268 .data<&physics_component::set_mass, &physics_component::get_mass>("mass"_hs)
270 entt::attribute{"name", "mass"},
271 entt::attribute{"pretty_name", "Mass"},
272 entt::attribute{"tooltip", "Mass for dynamic rigidbodies."},
273 entt::attribute{"min", 0.0f},
274 })
275 .data<&physics_component::set_collision_include_mask, &physics_component::get_collision_include_mask>("include_layers"_hs)
277 entt::attribute{"name", "include_layers"},
278 entt::attribute{"pretty_name", "Include Layers"},
279 entt::attribute{"tooltip", "Layers to include when producing collisions."},
280 })
281 .data<&physics_component::set_collision_exclude_mask, &physics_component::get_collision_exclude_mask>("exclude_layers"_hs)
283 entt::attribute{"name", "exclude_layers"},
284 entt::attribute{"pretty_name", "Exclude Layers"},
285 entt::attribute{"tooltip", "Layers to exclude when producing collisions."},
286 })
287 .data<nullptr, &physics_component::get_collision_mask>("collision_layers"_hs)
289 entt::attribute{"name", "collision_layers"},
290 entt::attribute{"pretty_name", "Collision Layers"},
291 entt::attribute{"tooltip", "Layers (Include - Exclude) used when producing collisions."},
292 })
293 .data<nullptr, &physics_component::get_velocity>("velocity"_hs)
295 entt::attribute{"name", "velocity"},
296 entt::attribute{"pretty_name", "Velocity"},
297 })
298 .data<nullptr, &physics_component::get_angular_velocity>("angular_velocity"_hs)
300 entt::attribute{"name", "angular_velocity"},
301 entt::attribute{"pretty_name", "Angular Velocity"},
302 })
303 .data<&physics_component::set_freeze_position, &physics_component::get_freeze_position>("freeze_position"_hs)
305 entt::attribute{"name", "freeze_position"},
306 entt::attribute{"pretty_name", "Freeze Position"},
307 entt::attribute{"tooltip", "Freeze."},
308 })
309 .data<&physics_component::set_freeze_rotation, &physics_component::get_freeze_rotation>("freeze_rotation"_hs)
311 entt::attribute{"name", "freeze_rotation"},
312 entt::attribute{"pretty_name", "Freeze Rotation"},
313 entt::attribute{"tooltip", "Freeze."},
314 })
315 .data<&physics_component::set_material, &physics_component::get_material>("material"_hs)
317 entt::attribute{"name", "material"},
318 entt::attribute{"pretty_name", "Material"},
319 entt::attribute{"tooltip", "Physics material for the rigidbody."},
320 })
321 .data<&physics_component::set_shapes, &physics_component::get_shapes>("shapes"_hs)
323 entt::attribute{"name", "shapes"},
324 entt::attribute{"pretty_name", "Shapes"},
325 entt::attribute{"tooltip", "Shapes."},
326 });
327}
328
330{
331 try_save(ar, ser20::make_nvp("is_using_gravity", obj.is_using_gravity()));
332 try_save(ar, ser20::make_nvp("is_kinematic", obj.is_kinematic()));
333 try_save(ar, ser20::make_nvp("is_sensor", obj.is_sensor()));
334 try_save(ar, ser20::make_nvp("is_autoscaled", obj.is_autoscaled()));
335 try_save(ar, ser20::make_nvp("mass", obj.get_mass()));
336 try_save(ar, ser20::make_nvp("include_layers", obj.get_collision_include_mask()));
337 try_save(ar, ser20::make_nvp("exclude_layers", obj.get_collision_exclude_mask()));
338 try_save(ar, ser20::make_nvp("freeze_position", obj.get_freeze_position()));
339 try_save(ar, ser20::make_nvp("freeze_rotation", obj.get_freeze_rotation()));
340
341 try_save(ar, ser20::make_nvp("material", obj.get_material()));
342 try_save(ar, ser20::make_nvp("shapes", obj.get_shapes()));
343}
346
348{
349 bool is_using_gravity{};
350 if(try_load(ar, ser20::make_nvp("is_using_gravity", is_using_gravity)))
351 {
352 obj.set_is_using_gravity(is_using_gravity);
353 }
354
355 bool is_kinematic{};
356 if(try_load(ar, ser20::make_nvp("is_kinematic", is_kinematic)))
357 {
358 obj.set_is_kinematic(is_kinematic);
359 }
360
361 bool is_sensor{};
362 if(try_load(ar, ser20::make_nvp("is_sensor", is_sensor)))
363 {
364 obj.set_is_sensor(is_sensor);
365 }
366
367 bool is_autoscaled{true};
368 if(try_load(ar, ser20::make_nvp("is_autoscaled", is_autoscaled)))
369 {
370 obj.set_is_autoscaled(is_autoscaled);
371 }
372
373 float mass{1};
374 if(try_load(ar, ser20::make_nvp("mass", mass)))
375 {
376 obj.set_mass(mass);
377 }
378
379 layer_mask include_layers;
380 if(try_load(ar, ser20::make_nvp("include_layers", include_layers)))
381 {
382 obj.set_collision_include_mask(include_layers);
383 }
384 layer_mask exclude_layers;
385 if(try_load(ar, ser20::make_nvp("exclude_layers", exclude_layers)))
386 {
387 obj.set_collision_exclude_mask(exclude_layers);
388 }
389
390 math::bvec3 freeze_position{};
391 if(try_load(ar, ser20::make_nvp("freeze_position", freeze_position)))
392 {
393 obj.set_freeze_position(freeze_position);
394 }
395
396 math::bvec3 freeze_rotation{};
397 if(try_load(ar, ser20::make_nvp("freeze_rotation", freeze_rotation)))
398 {
399 obj.set_freeze_rotation(freeze_rotation);
400 }
401
403 if(try_load(ar, ser20::make_nvp("material", material)))
404 {
405 obj.set_material(material);
406 }
407
408 std::vector<physics_compound_shape> shapes;
409 if(try_load(ar, ser20::make_nvp("shapes", shapes)))
410 {
411 obj.set_shapes(shapes);
412 }
413}
414
417
418} // namespace unravel
Base class for materials used in rendering.
Definition material.h:32
Component that handles physics properties and behaviors.
void set_is_using_gravity(bool use_gravity)
Sets whether the component uses gravity.
auto is_using_gravity() const noexcept -> bool
Checks if the component uses gravity.
attributes::value_type attribute
Definition reflection.h:19
std::map< std::string, meta_any > attributes
Definition reflection.h:18
BinaryInputArchive iarchive_binary_t
simd::JSONOutputArchive oarchive_associative_t
BinaryOutputArchive oarchive_binary_t
simd::JSONInputArchive iarchive_associative_t
#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 a handle to an asset, providing access and management functions.
Represents a box shape for physics calculations.
Represents a capsule shape for physics calculations.
Represents a compound shape that can contain multiple types of shapes.
Represents a cylinder shape for physics calculations.
Represents a sphere shape for physics calculations.
Support for std::variant.