Debian Codegraph @

modules/jolt_physics/shapes/jolt_cylinder_shape_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* jolt_cylinder_shape_3d.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "jolt_cylinder_shape_3d.h"
32
33 #include "../jolt_project_settings.h"
34 #include "../misc/jolt_type_conversions.h"
35
36 #include "Jolt/Physics/Collision/Shape/CylinderShape.h"
37
38 JPH::ShapeRefC JoltCylinderShape3D::_build() const {
39 const float half_height = height / 2.0f;
40 const float min_half_extent = MIN(half_height, radius);
41 const float actual_margin = MIN(margin, min_half_extent * JoltProjectSettings::collision_margin_fraction);
42
43 const JPH::CylinderShapeSettings shape_settings(half_height, radius, actual_margin);
44 const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();
45 ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics cylinder shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string()));
46
47 return shape_result.Get();
48 }
49
50 Variant JoltCylinderShape3D::get_data() const {
51 Dictionary data;
52 data["height"] = height;
53 data["radius"] = radius;
54 return data;
55 }
56
57 void JoltCylinderShape3D::set_data(const Variant &p_data) {
58 ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
59
60 const Dictionary data = p_data;
61
62 const Variant maybe_height = data.get("height", Variant());
63 ERR_FAIL_COND(maybe_height.get_type() != Variant::FLOAT);
64
65 const Variant maybe_radius = data.get("radius", Variant());
66 ERR_FAIL_COND(maybe_radius.get_type() != Variant::FLOAT);
67
68 const float new_height = maybe_height;
69 const float new_radius = maybe_radius;
70
71 if (unlikely(new_height == height && new_radius == radius)) {
72 return;
73 }
74
75 height = new_height;
76 radius = new_radius;
77
78 destroy();
79 }
80
81 void JoltCylinderShape3D::set_margin(float p_margin) {
82 if (unlikely(margin == p_margin)) {
83 return;
84 }
85
86 margin = p_margin;
87
88 destroy();
89 }
90
91 AABB JoltCylinderShape3D::get_aabb() const {
92 const Vector3 half_extents(radius, height / 2.0f, radius);
93 return AABB(-half_extents, half_extents * 2.0f);
94 }
95
96 String JoltCylinderShape3D::to_string() const {
97 return vformat("{height=%f radius=%f margin=%f}", height, radius, margin);
98 }