Debian Codegraph @

modules/jolt_physics/shapes/jolt_concave_polygon_shape_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* jolt_concave_polygon_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_concave_polygon_shape_3d.h"
32
33 #include "../jolt_project_settings.h"
34 #include "../misc/jolt_type_conversions.h"
35
36 #include "Jolt/Physics/Collision/Shape/MeshShape.h"
37
38 JPH::ShapeRefC JoltConcavePolygonShape3D::_build() const {
39 const int vertex_count = (int)faces.size();
40 const int face_count = vertex_count / 3;
41 const int excess_vertex_count = vertex_count % 3;
42
43 if (unlikely(vertex_count == 0)) {
44 return nullptr;
45 }
46
47 ERR_FAIL_COND_V_MSG(vertex_count < 3, nullptr, vformat("Failed to build Jolt Physics concave polygon shape with %s. It must have a vertex count of at least 3. This shape belongs to %s.", to_string(), _owners_to_string()));
48 ERR_FAIL_COND_V_MSG(excess_vertex_count != 0, nullptr, vformat("Failed to build Jolt Physics concave polygon shape with %s. It must have a vertex count that is divisible by 3. This shape belongs to %s.", to_string(), _owners_to_string()));
49
50 JPH::TriangleList jolt_faces;
51 jolt_faces.reserve((size_t)face_count);
52
53 const Vector3 *faces_begin = &faces[0];
54 const Vector3 *faces_end = faces_begin + vertex_count;
55 JPH::uint32 triangle_index = 0;
56
57 for (const Vector3 *vertex = faces_begin; vertex != faces_end; vertex += 3) {
58 const Vector3 *v0 = vertex + 0;
59 const Vector3 *v1 = vertex + 1;
60 const Vector3 *v2 = vertex + 2;
61
62 // Jolt uses a different winding order, so we swizzle the vertices to account for that.
63 jolt_faces.emplace_back(
64 JPH::Float3((float)v2->x, (float)v2->y, (float)v2->z),
65 JPH::Float3((float)v1->x, (float)v1->y, (float)v1->z),
66 JPH::Float3((float)v0->x, (float)v0->y, (float)v0->z),
67 0,
68 triangle_index++);
69 }
70
71 JPH::MeshShapeSettings shape_settings(jolt_faces);
72 shape_settings.mActiveEdgeCosThresholdAngle = JoltProjectSettings::active_edge_threshold_cos;
73 shape_settings.mPerTriangleUserData = JoltProjectSettings::enable_ray_cast_face_index;
74
75 const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();
76 ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics concave polygon shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string()));
77
78 return JoltShape3D::with_double_sided(shape_result.Get(), back_face_collision);
79 }
80
81 AABB JoltConcavePolygonShape3D::_calculate_aabb() const {
82 AABB result;
83
84 for (int i = 0; i < faces.size(); ++i) {
85 const Vector3 &vertex = faces[i];
86
87 if (i == 0) {
88 result.position = vertex;
89 } else {
90 result.expand_to(vertex);
91 }
92 }
93
94 return result;
95 }
96
97 Variant JoltConcavePolygonShape3D::get_data() const {
98 Dictionary data;
99 data["faces"] = faces;
100 data["backface_collision"] = back_face_collision;
101 return data;
102 }
103
104 void JoltConcavePolygonShape3D::set_data(const Variant &p_data) {
105 ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
106
107 const Dictionary data = p_data;
108
109 const Variant maybe_faces = data.get("faces", Variant());
110 ERR_FAIL_COND(maybe_faces.get_type() != Variant::PACKED_VECTOR3_ARRAY);
111
112 const Variant maybe_back_face_collision = data.get("backface_collision", Variant());
113 ERR_FAIL_COND(maybe_back_face_collision.get_type() != Variant::BOOL);
114
115 faces = maybe_faces;
116 back_face_collision = maybe_back_face_collision;
117
118 aabb = _calculate_aabb();
119
120 destroy();
121 }
122
123 String JoltConcavePolygonShape3D::to_string() const {
124 return vformat("{vertex_count=%d}", faces.size());
125 }