Path: blob/master/modules/jolt_physics/shapes/jolt_capsule_shape_3d.cpp
10278 views
/**************************************************************************/1/* jolt_capsule_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_capsule_shape_3d.h"3132#include "../misc/jolt_type_conversions.h"3334#include "Jolt/Physics/Collision/Shape/CapsuleShape.h"3536JPH::ShapeRefC JoltCapsuleShape3D::_build() const {37ERR_FAIL_COND_V_MSG(radius <= 0.0f, nullptr, vformat("Failed to build Jolt Physics capsule shape with %s. Its radius must be greater than 0. This shape belongs to %s.", to_string(), _owners_to_string()));38ERR_FAIL_COND_V_MSG(height <= 0.0f, nullptr, vformat("Failed to build Jolt Physics capsule shape with %s. Its height must be greater than 0. This shape belongs to %s.", to_string(), _owners_to_string()));39ERR_FAIL_COND_V_MSG(height < radius * 2.0f, nullptr, vformat("Failed to build Jolt Physics capsule shape with %s. Its height must be at least double that of its radius. This shape belongs to %s.", to_string(), _owners_to_string()));4041const float half_height = height / 2.0f;42const float cylinder_height = half_height - radius;4344const JPH::CapsuleShapeSettings shape_settings(cylinder_height, radius);45const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();46ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics capsule shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string()));4748return shape_result.Get();49}5051Variant JoltCapsuleShape3D::get_data() const {52Dictionary data;53data["height"] = height;54data["radius"] = radius;55return data;56}5758void JoltCapsuleShape3D::set_data(const Variant &p_data) {59ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);6061const Dictionary data = p_data;6263const Variant maybe_height = data.get("height", Variant());64ERR_FAIL_COND(maybe_height.get_type() != Variant::FLOAT);6566const Variant maybe_radius = data.get("radius", Variant());67ERR_FAIL_COND(maybe_radius.get_type() != Variant::FLOAT);6869const float new_height = maybe_height;70const float new_radius = maybe_radius;7172if (unlikely(new_height == height && new_radius == radius)) {73return;74}7576height = new_height;77radius = new_radius;7879destroy();80}8182AABB JoltCapsuleShape3D::get_aabb() const {83const Vector3 half_extents(radius, height / 2.0f, radius);84return AABB(-half_extents, half_extents * 2.0f);85}8687String JoltCapsuleShape3D::to_string() const {88return vformat("{height=%f radius=%f}", height, radius);89}909192