Unravel Engine C++ Reference
Loading...
Searching...
No Matches
physics_component.cpp
Go to the documentation of this file.
6
10
11namespace unravel
12{
13
15{
16 entt::meta_factory<physics_box_shape>{}
17 .type("physics_box_shape"_hs)
19 entt::attribute{"name", "physics_box_shape"},
20 entt::attribute{"pretty_name", "Box"},
21 })
22 .data<&physics_box_shape::center>("center"_hs)
24 entt::attribute{"name", "center"},
25 entt::attribute{"pretty_name", "Center"},
26 entt::attribute{"tooltip", "The center of the collider."},
27 })
28 .data<&physics_box_shape::extends>("extends"_hs)
30 entt::attribute{"name", "extends"},
31 entt::attribute{"pretty_name", "Extends"},
32 entt::attribute{"tooltip", "The extends of the collider."},
33 });
34}
35
37{
38 try_save(ar, ser20::make_nvp("center", obj.center));
39 try_save(ar, ser20::make_nvp("extends", obj.extends));
40}
43
45{
46 try_load(ar, ser20::make_nvp("center", obj.center));
47 try_load(ar, ser20::make_nvp("extends", obj.extends));
48}
49
52
54{
55 entt::meta_factory<physics_sphere_shape>{}
56 .type("physics_sphere_shape"_hs)
58 entt::attribute{"name", "physics_sphere_shape"},
59 entt::attribute{"pretty_name", "Sphere"},
60 })
61 .data<&physics_sphere_shape::center>("center"_hs)
63 entt::attribute{"name", "center"},
64 entt::attribute{"pretty_name", "Center"},
65 entt::attribute{"tooltip", "The center of the collider."},
66 })
67 .data<&physics_sphere_shape::radius>("radius"_hs)
69 entt::attribute{"name", "radius"},
70 entt::attribute{"pretty_name", "Radius"},
71 entt::attribute{"tooltip", "The radius of the collider."},
72 entt::attribute{"min", 0.0f},
73 entt::attribute{"step", 0.1f},
74 });
75}
76
78{
79 try_save(ar, ser20::make_nvp("center", obj.center));
80 try_save(ar, ser20::make_nvp("radius", obj.radius));
81}
84
86{
87 try_load(ar, ser20::make_nvp("center", obj.center));
88 try_load(ar, ser20::make_nvp("radius", obj.radius));
89}
90
93
95{
96 entt::meta_factory<physics_capsule_shape>{}
97 .type("physics_capsule_shape"_hs)
99 entt::attribute{"name", "physics_capsule_shape"},
100 entt::attribute{"pretty_name", "Capsule"},
101 })
102 .data<&physics_capsule_shape::center>("center"_hs)
104 entt::attribute{"name", "center"},
105 entt::attribute{"pretty_name", "Center"},
106 entt::attribute{"tooltip", "The center of the collider."},
107 })
108 .data<&physics_capsule_shape::radius>("radius"_hs)
110 entt::attribute{"name", "radius"},
111 entt::attribute{"pretty_name", "Radius"},
112 entt::attribute{"tooltip", "The radius of the collider."},
113 entt::attribute{"min", 0.0f},
114 entt::attribute{"step", 0.1f},
115 })
116 .data<&physics_capsule_shape::length>("length"_hs)
118 entt::attribute{"name", "length"},
119 entt::attribute{"pretty_name", "Length"},
120 entt::attribute{"tooltip", "The length of the collider."},
121 entt::attribute{"min", 0.0f},
122 entt::attribute{"step", 0.1f},
123 });
124}
125
127{
128 try_save(ar, ser20::make_nvp("center", obj.center));
129 try_save(ar, ser20::make_nvp("radius", obj.radius));
130 try_save(ar, ser20::make_nvp("length", obj.length));
131}
134
136{
137 try_load(ar, ser20::make_nvp("center", obj.center));
138 try_load(ar, ser20::make_nvp("radius", obj.radius));
139 try_load(ar, ser20::make_nvp("length", obj.length));
140}
141
144
146{
147 entt::meta_factory<physics_cylinder_shape>{}
148 .type("physics_cylinder_shape"_hs)
150 entt::attribute{"name", "physics_cylinder_shape"},
151 entt::attribute{"pretty_name", "Cylinder"},
152 })
153 .data<&physics_cylinder_shape::center>("center"_hs)
155 entt::attribute{"name", "center"},
156 entt::attribute{"pretty_name", "Center"},
157 entt::attribute{"tooltip", "The center of the collider."},
158 })
159 .data<&physics_cylinder_shape::radius>("radius"_hs)
161 entt::attribute{"name", "radius"},
162 entt::attribute{"pretty_name", "Radius"},
163 entt::attribute{"tooltip", "The radius of the collider."},
164 entt::attribute{"min", 0.0f},
165 entt::attribute{"step", 0.1f},
166 })
167 .data<&physics_cylinder_shape::length>("length"_hs)
169 entt::attribute{"name", "length"},
170 entt::attribute{"pretty_name", "Length"},
171 entt::attribute{"tooltip", "The length of the collider."},
172 entt::attribute{"min", 0.0f},
173 entt::attribute{"step", 0.1f},
174 });
175}
176
178{
179 try_save(ar, ser20::make_nvp("center", obj.center));
180 try_save(ar, ser20::make_nvp("radius", obj.radius));
181 try_save(ar, ser20::make_nvp("length", obj.length));
182}
185
187{
188 try_load(ar, ser20::make_nvp("center", obj.center));
189 try_load(ar, ser20::make_nvp("radius", obj.radius));
190 try_load(ar, ser20::make_nvp("length", obj.length));
191}
192
195
197{
198 entt::meta_factory<mesh_collision_type>{}
199 .type("mesh_collision_type"_hs)
201 entt::attribute{"name", "mesh_collision_type"},
202 entt::attribute{"pretty_name", "Mesh Collision Type"},
203 })
204 .data<mesh_collision_type::convex>("convex"_hs)
206 entt::attribute{"name", "convex"},
207 entt::attribute{"pretty_name", "Convex"},
208 })
209 .data<mesh_collision_type::concave>("concave"_hs)
211 entt::attribute{"name", "concave"},
212 entt::attribute{"pretty_name", "Concave"},
213 });
214}
215
217{
218 entt::meta_factory<physics_mesh_shape>{}
219 .type("physics_mesh_shape"_hs)
221 entt::attribute{"name", "physics_mesh_shape"},
222 entt::attribute{"pretty_name", "Mesh"},
223 })
224 .data<&physics_mesh_shape::center>("center"_hs)
226 entt::attribute{"pretty_name", "Center"},
227 entt::attribute{"tooltip", "Center offset of the mesh collision shape."},
228 })
229 .data<&physics_mesh_shape::mesh_asset>("mesh_asset"_hs)
231 entt::attribute{"pretty_name", "Mesh Asset"},
232 entt::attribute{"tooltip", "The mesh asset to use for collision."},
233 })
234 .data<&physics_mesh_shape::collision_type>("collision_type"_hs)
236 entt::attribute{"pretty_name", "Collision Type"},
237 entt::attribute{"tooltip", "Type of collision shape (convex for dynamic, concave for static)."},
238 });
239}
240
242{
243 try_save(ar, ser20::make_nvp("center", obj.center));
244 try_save(ar, ser20::make_nvp("mesh_asset", obj.mesh_asset));
245 try_save(ar, ser20::make_nvp("collision_type", obj.collision_type));
246}
247
250
252{
253 try_load(ar, ser20::make_nvp("center", obj.center));
254 try_load(ar, ser20::make_nvp("mesh_asset", obj.mesh_asset));
255 try_load(ar, ser20::make_nvp("collision_type", obj.collision_type));
256}
257
260
262{
263 static const auto& ps = entt::resolve<physics_box_shape>();
264 static const auto& ss = entt::resolve<physics_sphere_shape>();
265 static const auto& cs = entt::resolve<physics_capsule_shape>();
266 static const auto& cys = entt::resolve<physics_cylinder_shape>();
267 static const auto& ms = entt::resolve<physics_mesh_shape>();
268
269 std::vector<entt::meta_type> variant_types{ps, ss, cs, cys, ms};
270
271 // Register physics_compound_shape with entt
272 entt::meta_factory<physics_compound_shape>{}
273 .type("physics_compound_shape"_hs)
275 entt::attribute{"name", "physics_compound_shape"},
276 entt::attribute{"pretty_name", "Shape"},
277 entt::attribute{"variant_types", variant_types}
278 });
279
280}
281
283{
284 try_save(ar, ser20::make_nvp("shape", obj.shape));
285}
288
290{
291 try_load(ar, ser20::make_nvp("shape", obj.shape));
292}
293
296
298{
299 entt::meta_factory<physics_component>{}
300 .type("physics_component"_hs)
302 entt::attribute{"name", "physics_component"},
303 entt::attribute{"category", "PHYSICS"},
304 entt::attribute{"pretty_name", "Physics"},
305 })
306 .func<&component_meta<physics_component>::exists>("component_exists"_hs)
307 .func<&component_meta<physics_component>::add>("component_add"_hs)
308 .func<&component_meta<physics_component>::remove>("component_remove"_hs)
309 .func<&component_meta<physics_component>::save>("component_save"_hs)
310 .func<&component_meta<physics_component>::load>("component_load"_hs)
312 .custom<entt::attributes>(entt::attributes{
313 entt::attribute{"name", "is_using_gravity"},
314 entt::attribute{"pretty_name", "Use Gravity"},
315 entt::attribute{"tooltip", "Simulate gravity for this rigidbody."},
316 })
317 .data<&physics_component::set_is_kinematic, &physics_component::is_kinematic>("is_kinematic"_hs)
319 entt::attribute{"name", "is_kinematic"},
320 entt::attribute{"pretty_name", "Is Kinematic"},
321 entt::attribute{"tooltip", "Is the rigidbody kinematic(A rigid body that is not affected by others and can be moved directly.)"},
322 })
323 .data<&physics_component::set_is_sensor, &physics_component::is_sensor>("is_sensor"_hs)
325 entt::attribute{"name", "is_sensor"},
326 entt::attribute{"pretty_name", "Is Sensor"},
327 entt::attribute{"tooltip", "The rigidbody will not respond to collisions, i.e. it becomes a _sensor_."},
328 })
329 .data<&physics_component::set_is_autoscaled, &physics_component::is_autoscaled>("is_autoscaled"_hs)
331 entt::attribute{"name", "is_autoscaled"},
332 entt::attribute{"pretty_name", "Is Auto Scaled"},
333 entt::attribute{"tooltip", "Enables/Disables shape auto scale with transform."},
334 })
335 .data<&physics_component::set_mass, &physics_component::get_mass>("mass"_hs)
337 entt::attribute{"name", "mass"},
338 entt::attribute{"pretty_name", "Mass"},
339 entt::attribute{"tooltip", "Mass for dynamic rigidbodies."},
340 entt::attribute{"min", 0.0f},
341 })
342 .data<&physics_component::set_collision_include_mask, &physics_component::get_collision_include_mask>("include_layers"_hs)
344 entt::attribute{"name", "include_layers"},
345 entt::attribute{"pretty_name", "Include Layers"},
346 entt::attribute{"tooltip", "Layers to include when producing collisions."},
347 })
348 .data<&physics_component::set_collision_exclude_mask, &physics_component::get_collision_exclude_mask>("exclude_layers"_hs)
350 entt::attribute{"name", "exclude_layers"},
351 entt::attribute{"pretty_name", "Exclude Layers"},
352 entt::attribute{"tooltip", "Layers to exclude when producing collisions."},
353 })
354 .data<nullptr, &physics_component::get_collision_mask>("collision_layers"_hs)
356 entt::attribute{"name", "collision_layers"},
357 entt::attribute{"pretty_name", "Collision Layers"},
358 entt::attribute{"tooltip", "Layers (Include - Exclude) used when producing collisions."},
359 })
360 .data<nullptr, &physics_component::get_velocity>("velocity"_hs)
362 entt::attribute{"name", "velocity"},
363 entt::attribute{"pretty_name", "Velocity"},
364 })
365 .data<nullptr, &physics_component::get_angular_velocity>("angular_velocity"_hs)
367 entt::attribute{"name", "angular_velocity"},
368 entt::attribute{"pretty_name", "Angular Velocity"},
369 })
370 .data<&physics_component::set_freeze_position, &physics_component::get_freeze_position>("freeze_position"_hs)
372 entt::attribute{"name", "freeze_position"},
373 entt::attribute{"pretty_name", "Freeze Position"},
374 entt::attribute{"tooltip", "Freeze."},
375 })
376 .data<&physics_component::set_freeze_rotation, &physics_component::get_freeze_rotation>("freeze_rotation"_hs)
378 entt::attribute{"name", "freeze_rotation"},
379 entt::attribute{"pretty_name", "Freeze Rotation"},
380 entt::attribute{"tooltip", "Freeze."},
381 })
382 .data<&physics_component::set_material, &physics_component::get_material>("material"_hs)
384 entt::attribute{"name", "material"},
385 entt::attribute{"pretty_name", "Material"},
386 entt::attribute{"tooltip", "Physics material for the rigidbody."},
387 })
388 .data<&physics_component::set_shapes, &physics_component::get_shapes>("shapes"_hs)
390 entt::attribute{"name", "shapes"},
391 entt::attribute{"pretty_name", "Shapes"},
392 entt::attribute{"tooltip", "Shapes."},
393 });
394}
395
397{
398 try_save(ar, ser20::make_nvp("is_using_gravity", obj.is_using_gravity()));
399 try_save(ar, ser20::make_nvp("is_kinematic", obj.is_kinematic()));
400 try_save(ar, ser20::make_nvp("is_sensor", obj.is_sensor()));
401 try_save(ar, ser20::make_nvp("is_autoscaled", obj.is_autoscaled()));
402 try_save(ar, ser20::make_nvp("mass", obj.get_mass()));
403 try_save(ar, ser20::make_nvp("include_layers", obj.get_collision_include_mask()));
404 try_save(ar, ser20::make_nvp("exclude_layers", obj.get_collision_exclude_mask()));
405 try_save(ar, ser20::make_nvp("freeze_position", obj.get_freeze_position()));
406 try_save(ar, ser20::make_nvp("freeze_rotation", obj.get_freeze_rotation()));
407
408 try_save(ar, ser20::make_nvp("material", obj.get_material()));
409 try_save(ar, ser20::make_nvp("shapes", obj.get_shapes()));
410}
413
415{
416 bool is_using_gravity{};
417 if(try_load(ar, ser20::make_nvp("is_using_gravity", is_using_gravity)))
418 {
419 obj.set_is_using_gravity(is_using_gravity);
420 }
421
422 bool is_kinematic{};
423 if(try_load(ar, ser20::make_nvp("is_kinematic", is_kinematic)))
424 {
425 obj.set_is_kinematic(is_kinematic);
426 }
427
428 bool is_sensor{};
429 if(try_load(ar, ser20::make_nvp("is_sensor", is_sensor)))
430 {
431 obj.set_is_sensor(is_sensor);
432 }
433
434 bool is_autoscaled{true};
435 if(try_load(ar, ser20::make_nvp("is_autoscaled", is_autoscaled)))
436 {
437 obj.set_is_autoscaled(is_autoscaled);
438 }
439
440 float mass{1};
441 if(try_load(ar, ser20::make_nvp("mass", mass)))
442 {
443 obj.set_mass(mass);
444 }
445
446 layer_mask include_layers;
447 if(try_load(ar, ser20::make_nvp("include_layers", include_layers)))
448 {
449 obj.set_collision_include_mask(include_layers);
450 }
451 layer_mask exclude_layers;
452 if(try_load(ar, ser20::make_nvp("exclude_layers", exclude_layers)))
453 {
454 obj.set_collision_exclude_mask(exclude_layers);
455 }
456
457 math::bvec3 freeze_position{};
458 if(try_load(ar, ser20::make_nvp("freeze_position", freeze_position)))
459 {
460 obj.set_freeze_position(freeze_position);
461 }
462
463 math::bvec3 freeze_rotation{};
464 if(try_load(ar, ser20::make_nvp("freeze_rotation", freeze_rotation)))
465 {
466 obj.set_freeze_rotation(freeze_rotation);
467 }
468
470 if(try_load(ar, ser20::make_nvp("material", material)))
471 {
472 obj.set_material(material);
473 }
474
475 std::vector<physics_compound_shape> shapes;
476 if(try_load(ar, ser20::make_nvp("shapes", shapes)))
477 {
478 obj.set_shapes(shapes);
479 }
480}
481
484
485} // namespace unravel
Base class for materials used in rendering.
Definition material.h:44
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
mesh_collision_type
Specifies the type of mesh collision shape.
#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
Thread-safe handle to an asset.
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 mesh shape for physics calculations.
Represents a sphere shape for physics calculations.
Support for std::variant.