0.2.0-dev Download ZIP
160 lines ·6.7 KB Raw
| 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 || has_surfaces(); |
| 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 shape_result.Get(); |
| 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 | if (!friction.is_empty()) { |
| 102 | data["friction"] = friction; |
| 103 | } |
| 104 | if (!bounce.is_empty()) { |
| 105 | data["bounce"] = bounce; |
| 106 | } |
| 107 | return data; |
| 108 | } |
| 109 | |
| 110 | void JoltConcavePolygonShape3D::set_data(const Variant &p_data) { |
| 111 | ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY); |
| 112 | |
| 113 | const Dictionary data = p_data; |
| 114 | |
| 115 | const Variant maybe_faces = data.get("faces", Variant()); |
| 116 | ERR_FAIL_COND(maybe_faces.get_type() != Variant::PACKED_VECTOR3_ARRAY); |
| 117 | |
| 118 | const Variant maybe_back_face_collision = data.get("backface_collision", Variant()); |
| 119 | ERR_FAIL_COND(maybe_back_face_collision.get_type() != Variant::BOOL); |
| 120 | |
| 121 | const Variant maybe_friction = data.get("friction", PackedFloat32Array()); |
| 122 | ERR_FAIL_COND(maybe_friction.get_type() != Variant::PACKED_FLOAT32_ARRAY); |
| 123 | |
| 124 | const Variant maybe_bounce = data.get("bounce", PackedFloat32Array()); |
| 125 | ERR_FAIL_COND(maybe_bounce.get_type() != Variant::PACKED_FLOAT32_ARRAY); |
| 126 | |
| 127 | const PackedVector3Array new_faces = maybe_faces; |
| 128 | const PackedFloat32Array new_friction = maybe_friction; |
| 129 | const PackedFloat32Array new_bounce = maybe_bounce; |
| 130 | const int face_count = new_faces.size() / 3; |
| 131 | ERR_FAIL_COND_MSG(!new_friction.is_empty() && new_friction.size() != face_count, "A concave polygon shape's friction holds one value per triangle."); |
| 132 | ERR_FAIL_COND_MSG(!new_bounce.is_empty() && new_bounce.size() != face_count, "A concave polygon shape's bounce holds one value per triangle."); |
| 133 | |
| 134 | faces = new_faces; |
| 135 | friction = new_friction; |
| 136 | bounce = new_bounce; |
| 137 | back_face_collision = maybe_back_face_collision; |
| 138 | |
| 139 | aabb = _calculate_aabb(); |
| 140 | |
| 141 | destroy(); |
| 142 | } |
| 143 | |
| 144 | void JoltConcavePolygonShape3D::read_surface(int p_face, float &r_friction, float &r_bounce) const { |
| 145 | if (p_face < 0) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if (p_face < friction.size() && !Math::is_nan(friction[p_face])) { |
| 150 | r_friction = friction[p_face]; |
| 151 | } |
| 152 | |
| 153 | if (p_face < bounce.size() && !Math::is_nan(bounce[p_face])) { |
| 154 | r_bounce = bounce[p_face]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | String JoltConcavePolygonShape3D::to_string() const { |
| 159 | return vformat("{vertex_count=%d}", faces.size()); |
| 160 | } |