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)
205 .data<mesh_collision_type::concave>("concave"_hs);
206}
207
209{
210 entt::meta_factory<physics_mesh_shape>{}
211 .type("physics_mesh_shape"_hs)
213 entt::attribute{"name", "physics_mesh_shape"},
214 entt::attribute{"pretty_name", "Mesh"},
215 })
216 .data<&physics_mesh_shape::center>("center"_hs)
218 entt::attribute{"pretty_name", "Center"},
219 entt::attribute{"tooltip", "Center offset of the mesh collision shape."},
220 })
221 .data<&physics_mesh_shape::mesh_asset>("mesh_asset"_hs)
223 entt::attribute{"pretty_name", "Mesh Asset"},
224 entt::attribute{"tooltip", "The mesh asset to use for collision."},
225 })
226 .data<&physics_mesh_shape::collision_type>("collision_type"_hs)
228 entt::attribute{"pretty_name", "Collision Type"},
229 entt::attribute{"tooltip", "Type of collision shape (convex for dynamic, concave for static)."},
230 });
231}
232
234{
235 try_save(ar, ser20::make_nvp("center", obj.center));
236 try_save(ar, ser20::make_nvp("mesh_asset", obj.mesh_asset));
237 try_save(ar, ser20::make_nvp("collision_type", obj.collision_type));
238}
239
242
244{
245 try_load(ar, ser20::make_nvp("center", obj.center));
246 try_load(ar, ser20::make_nvp("mesh_asset", obj.mesh_asset));
247 try_load(ar, ser20::make_nvp("collision_type", obj.collision_type));
248}
249
252
254{
255 static const auto& ps = entt::resolve<physics_box_shape>();
256 static const auto& ss = entt::resolve<physics_sphere_shape>();
257 static const auto& cs = entt::resolve<physics_capsule_shape>();
258 static const auto& cys = entt::resolve<physics_cylinder_shape>();
259 static const auto& ms = entt::resolve<physics_mesh_shape>();
260
261 std::vector<entt::meta_type> variant_types{ps, ss, cs, cys, ms};
262
263 // Register physics_compound_shape with entt
264 entt::meta_factory<physics_compound_shape>{}
265 .type("physics_compound_shape"_hs)
267 entt::attribute{"name", "physics_compound_shape"},
268 entt::attribute{"pretty_name", "Shape"},
269 entt::attribute{"variant_types", variant_types}
270 });
271
272}
273
275{
276 try_save(ar, ser20::make_nvp("shape", obj.shape));
277}
280
282{
283 try_load(ar, ser20::make_nvp("shape", obj.shape));
284}
285
288
290{
291 entt::meta_factory<physics_component>{}
292 .type("physics_component"_hs)
294 entt::attribute{"name", "physics_component"},
295 entt::attribute{"category", "PHYSICS"},
296 entt::attribute{"pretty_name", "Physics"},
297 })
298 .func<&component_meta<physics_component>::exists>("component_exists"_hs)
299 .func<&component_meta<physics_component>::add>("component_add"_hs)
300 .func<&component_meta<physics_component>::remove>("component_remove"_hs)
301 .func<&component_meta<physics_component>::save>("component_save"_hs)
302 .func<&component_meta<physics_component>::load>("component_load"_hs)
304 .custom<entt::attributes>(entt::attributes{
305 entt::attribute{"name", "is_using_gravity"},
306 entt::attribute{"pretty_name", "Use Gravity"},
307 entt::attribute{"tooltip", "Simulate gravity for this rigidbody."},
308 })
309 .data<&physics_component::set_is_kinematic, &physics_component::is_kinematic>("is_kinematic"_hs)
311 entt::attribute{"name", "is_kinematic"},
312 entt::attribute{"pretty_name", "Is Kinematic"},
313 entt::attribute{"tooltip", "Is the rigidbody kinematic(A rigid body that is not affected by others and can be moved directly.)"},
314 })
315 .data<&physics_component::set_is_sensor, &physics_component::is_sensor>("is_sensor"_hs)
317 entt::attribute{"name", "is_sensor"},
318 entt::attribute{"pretty_name", "Is Sensor"},
319 entt::attribute{"tooltip", "The rigidbody will not respond to collisions, i.e. it becomes a _sensor_."},
320 })
321 .data<&physics_component::set_is_autoscaled, &physics_component::is_autoscaled>("is_autoscaled"_hs)
323 entt::attribute{"name", "is_autoscaled"},
324 entt::attribute{"pretty_name", "Is Auto Scaled"},
325 entt::attribute{"tooltip", "Enables/Disables shape auto scale with transform."},
326 })
327 .data<&physics_component::set_mass, &physics_component::get_mass>("mass"_hs)
329 entt::attribute{"name", "mass"},
330 entt::attribute{"pretty_name", "Mass"},
331 entt::attribute{"tooltip", "Mass for dynamic rigidbodies."},
332 entt::attribute{"min", 0.0f},
333 })
334 .data<&physics_component::set_collision_include_mask, &physics_component::get_collision_include_mask>("include_layers"_hs)
336 entt::attribute{"name", "include_layers"},
337 entt::attribute{"pretty_name", "Include Layers"},
338 entt::attribute{"tooltip", "Layers to include when producing collisions."},
339 })
340 .data<&physics_component::set_collision_exclude_mask, &physics_component::get_collision_exclude_mask>("exclude_layers"_hs)
342 entt::attribute{"name", "exclude_layers"},
343 entt::attribute{"pretty_name", "Exclude Layers"},
344 entt::attribute{"tooltip", "Layers to exclude when producing collisions."},
345 })
346 .data<nullptr, &physics_component::get_collision_mask>("collision_layers"_hs)
348 entt::attribute{"name", "collision_layers"},
349 entt::attribute{"pretty_name", "Collision Layers"},
350 entt::attribute{"tooltip", "Layers (Include - Exclude) used when producing collisions."},
351 })
352 .data<nullptr, &physics_component::get_velocity>("velocity"_hs)
354 entt::attribute{"name", "velocity"},
355 entt::attribute{"pretty_name", "Velocity"},
356 })
357 .data<nullptr, &physics_component::get_angular_velocity>("angular_velocity"_hs)
359 entt::attribute{"name", "angular_velocity"},
360 entt::attribute{"pretty_name", "Angular Velocity"},
361 })
362 .data<&physics_component::set_freeze_position, &physics_component::get_freeze_position>("freeze_position"_hs)
364 entt::attribute{"name", "freeze_position"},
365 entt::attribute{"pretty_name", "Freeze Position"},
366 entt::attribute{"tooltip", "Freeze."},
367 })
368 .data<&physics_component::set_freeze_rotation, &physics_component::get_freeze_rotation>("freeze_rotation"_hs)
370 entt::attribute{"name", "freeze_rotation"},
371 entt::attribute{"pretty_name", "Freeze Rotation"},
372 entt::attribute{"tooltip", "Freeze."},
373 })
374 .data<&physics_component::set_material, &physics_component::get_material>("material"_hs)
376 entt::attribute{"name", "material"},
377 entt::attribute{"pretty_name", "Material"},
378 entt::attribute{"tooltip", "Physics material for the rigidbody."},
379 })
380 .data<&physics_component::set_shapes, &physics_component::get_shapes>("shapes"_hs)
382 entt::attribute{"name", "shapes"},
383 entt::attribute{"pretty_name", "Shapes"},
384 entt::attribute{"tooltip", "Shapes."},
385 });
386}
387
389{
390 try_save(ar, ser20::make_nvp("is_using_gravity", obj.is_using_gravity()));
391 try_save(ar, ser20::make_nvp("is_kinematic", obj.is_kinematic()));
392 try_save(ar, ser20::make_nvp("is_sensor", obj.is_sensor()));
393 try_save(ar, ser20::make_nvp("is_autoscaled", obj.is_autoscaled()));
394 try_save(ar, ser20::make_nvp("mass", obj.get_mass()));
395 try_save(ar, ser20::make_nvp("include_layers", obj.get_collision_include_mask()));
396 try_save(ar, ser20::make_nvp("exclude_layers", obj.get_collision_exclude_mask()));
397 try_save(ar, ser20::make_nvp("freeze_position", obj.get_freeze_position()));
398 try_save(ar, ser20::make_nvp("freeze_rotation", obj.get_freeze_rotation()));
399
400 try_save(ar, ser20::make_nvp("material", obj.get_material()));
401 try_save(ar, ser20::make_nvp("shapes", obj.get_shapes()));
402}
405
407{
408 bool is_using_gravity{};
409 if(try_load(ar, ser20::make_nvp("is_using_gravity", is_using_gravity)))
410 {
411 obj.set_is_using_gravity(is_using_gravity);
412 }
413
414 bool is_kinematic{};
415 if(try_load(ar, ser20::make_nvp("is_kinematic", is_kinematic)))
416 {
417 obj.set_is_kinematic(is_kinematic);
418 }
419
420 bool is_sensor{};
421 if(try_load(ar, ser20::make_nvp("is_sensor", is_sensor)))
422 {
423 obj.set_is_sensor(is_sensor);
424 }
425
426 bool is_autoscaled{true};
427 if(try_load(ar, ser20::make_nvp("is_autoscaled", is_autoscaled)))
428 {
429 obj.set_is_autoscaled(is_autoscaled);
430 }
431
432 float mass{1};
433 if(try_load(ar, ser20::make_nvp("mass", mass)))
434 {
435 obj.set_mass(mass);
436 }
437
438 layer_mask include_layers;
439 if(try_load(ar, ser20::make_nvp("include_layers", include_layers)))
440 {
441 obj.set_collision_include_mask(include_layers);
442 }
443 layer_mask exclude_layers;
444 if(try_load(ar, ser20::make_nvp("exclude_layers", exclude_layers)))
445 {
446 obj.set_collision_exclude_mask(exclude_layers);
447 }
448
449 math::bvec3 freeze_position{};
450 if(try_load(ar, ser20::make_nvp("freeze_position", freeze_position)))
451 {
452 obj.set_freeze_position(freeze_position);
453 }
454
455 math::bvec3 freeze_rotation{};
456 if(try_load(ar, ser20::make_nvp("freeze_rotation", freeze_rotation)))
457 {
458 obj.set_freeze_rotation(freeze_rotation);
459 }
460
462 if(try_load(ar, ser20::make_nvp("material", material)))
463 {
464 obj.set_material(material);
465 }
466
467 std::vector<physics_compound_shape> shapes;
468 if(try_load(ar, ser20::make_nvp("shapes", shapes)))
469 {
470 obj.set_shapes(shapes);
471 }
472}
473
476
477} // 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
mesh_collision_type
Specifies the type of mesh collision shape.
@ concave
Concave mesh collision (static only, slower but accurate)
#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 mesh shape for physics calculations.
Represents a sphere shape for physics calculations.
Support for std::variant.