Path: blob/master/modules/jolt_physics/shapes/jolt_concave_polygon_shape_3d.cpp
10278 views
/**************************************************************************/1/* jolt_concave_polygon_shape_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "jolt_concave_polygon_shape_3d.h"3132#include "../jolt_project_settings.h"33#include "../misc/jolt_type_conversions.h"3435#include "Jolt/Physics/Collision/Shape/MeshShape.h"3637JPH::ShapeRefC JoltConcavePolygonShape3D::_build() const {38const int vertex_count = (int)faces.size();39const int face_count = vertex_count / 3;40const int excess_vertex_count = vertex_count % 3;4142if (unlikely(vertex_count == 0)) {43return nullptr;44}4546ERR_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()));47ERR_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()));4849JPH::TriangleList jolt_faces;50jolt_faces.reserve((size_t)face_count);5152const Vector3 *faces_begin = &faces[0];53const Vector3 *faces_end = faces_begin + vertex_count;54JPH::uint32 triangle_index = 0;5556for (const Vector3 *vertex = faces_begin; vertex != faces_end; vertex += 3) {57const Vector3 *v0 = vertex + 0;58const Vector3 *v1 = vertex + 1;59const Vector3 *v2 = vertex + 2;6061// Jolt uses a different winding order, so we swizzle the vertices to account for that.62jolt_faces.emplace_back(63JPH::Float3((float)v2->x, (float)v2->y, (float)v2->z),64JPH::Float3((float)v1->x, (float)v1->y, (float)v1->z),65JPH::Float3((float)v0->x, (float)v0->y, (float)v0->z),660,67triangle_index++);68}6970JPH::MeshShapeSettings shape_settings(jolt_faces);71shape_settings.mActiveEdgeCosThresholdAngle = JoltProjectSettings::active_edge_threshold_cos;72shape_settings.mPerTriangleUserData = JoltProjectSettings::enable_ray_cast_face_index;7374const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();75ERR_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()));7677return JoltShape3D::with_double_sided(shape_result.Get(), back_face_collision);78}7980AABB JoltConcavePolygonShape3D::_calculate_aabb() const {81AABB result;8283for (int i = 0; i < faces.size(); ++i) {84const Vector3 &vertex = faces[i];8586if (i == 0) {87result.position = vertex;88} else {89result.expand_to(vertex);90}91}9293return result;94}9596Variant JoltConcavePolygonShape3D::get_data() const {97Dictionary data;98data["faces"] = faces;99data["backface_collision"] = back_face_collision;100return data;101}102103void JoltConcavePolygonShape3D::set_data(const Variant &p_data) {104ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);105106const Dictionary data = p_data;107108const Variant maybe_faces = data.get("faces", Variant());109ERR_FAIL_COND(maybe_faces.get_type() != Variant::PACKED_VECTOR3_ARRAY);110111const Variant maybe_back_face_collision = data.get("backface_collision", Variant());112ERR_FAIL_COND(maybe_back_face_collision.get_type() != Variant::BOOL);113114faces = maybe_faces;115back_face_collision = maybe_back_face_collision;116117aabb = _calculate_aabb();118119destroy();120}121122String JoltConcavePolygonShape3D::to_string() const {123return vformat("{vertex_count=%d}", faces.size());124}125126127