Path: blob/master/modules/jolt_physics/shapes/jolt_convex_polygon_shape_3d.cpp
10278 views
/**************************************************************************/1/* jolt_convex_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_convex_polygon_shape_3d.h"3132#include "../jolt_project_settings.h"33#include "../misc/jolt_type_conversions.h"3435#include "Jolt/Physics/Collision/Shape/ConvexHullShape.h"3637JPH::ShapeRefC JoltConvexPolygonShape3D::_build() const {38const int vertex_count = (int)vertices.size();3940if (unlikely(vertex_count == 0)) {41return nullptr;42}4344ERR_FAIL_COND_V_MSG(vertex_count < 3, nullptr, vformat("Failed to build Jolt Physics convex polygon shape with %s. It must have a vertex count of at least 3. This shape belongs to %s.", to_string(), _owners_to_string()));4546JPH::Array<JPH::Vec3> jolt_vertices;47jolt_vertices.reserve((size_t)vertex_count);4849const Vector3 *vertices_begin = &vertices[0];50const Vector3 *vertices_end = vertices_begin + vertex_count;5152for (const Vector3 *vertex = vertices_begin; vertex != vertices_end; ++vertex) {53jolt_vertices.emplace_back((float)vertex->x, (float)vertex->y, (float)vertex->z);54}5556const float min_half_extent = _calculate_aabb().get_shortest_axis_size() * 0.5f;57const float actual_margin = MIN(margin, min_half_extent * JoltProjectSettings::collision_margin_fraction);5859const JPH::ConvexHullShapeSettings shape_settings(jolt_vertices, actual_margin);60const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();61ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics convex 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()));6263return shape_result.Get();64}6566AABB JoltConvexPolygonShape3D::_calculate_aabb() const {67AABB result;6869for (int i = 0; i < vertices.size(); ++i) {70if (i == 0) {71result.position = vertices[i];72} else {73result.expand_to(vertices[i]);74}75}7677return result;78}7980Variant JoltConvexPolygonShape3D::get_data() const {81return vertices;82}8384void JoltConvexPolygonShape3D::set_data(const Variant &p_data) {85ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR3_ARRAY);8687vertices = p_data;8889aabb = _calculate_aabb();9091destroy();92}9394void JoltConvexPolygonShape3D::set_margin(float p_margin) {95if (unlikely(margin == p_margin)) {96return;97}9899margin = p_margin;100101destroy();102}103104String JoltConvexPolygonShape3D::to_string() const {105return vformat("{vertex_count=%d margin=%f}", vertices.size(), margin);106}107108109