Path: blob/master/modules/jolt_physics/shapes/jolt_cylinder_shape_3d.cpp
10278 views
/**************************************************************************/1/* jolt_cylinder_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_cylinder_shape_3d.h"3132#include "../jolt_project_settings.h"33#include "../misc/jolt_type_conversions.h"3435#include "Jolt/Physics/Collision/Shape/CylinderShape.h"3637JPH::ShapeRefC JoltCylinderShape3D::_build() const {38const float half_height = height / 2.0f;39const float min_half_extent = MIN(half_height, radius);40const float actual_margin = MIN(margin, min_half_extent * JoltProjectSettings::collision_margin_fraction);4142const JPH::CylinderShapeSettings shape_settings(half_height, radius, actual_margin);43const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();44ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics cylinder shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string()));4546return shape_result.Get();47}4849Variant JoltCylinderShape3D::get_data() const {50Dictionary data;51data["height"] = height;52data["radius"] = radius;53return data;54}5556void JoltCylinderShape3D::set_data(const Variant &p_data) {57ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);5859const Dictionary data = p_data;6061const Variant maybe_height = data.get("height", Variant());62ERR_FAIL_COND(maybe_height.get_type() != Variant::FLOAT);6364const Variant maybe_radius = data.get("radius", Variant());65ERR_FAIL_COND(maybe_radius.get_type() != Variant::FLOAT);6667const float new_height = maybe_height;68const float new_radius = maybe_radius;6970if (unlikely(new_height == height && new_radius == radius)) {71return;72}7374height = new_height;75radius = new_radius;7677destroy();78}7980void JoltCylinderShape3D::set_margin(float p_margin) {81if (unlikely(margin == p_margin)) {82return;83}8485margin = p_margin;8687destroy();88}8990AABB JoltCylinderShape3D::get_aabb() const {91const Vector3 half_extents(radius, height / 2.0f, radius);92return AABB(-half_extents, half_extents * 2.0f);93}9495String JoltCylinderShape3D::to_string() const {96return vformat("{height=%f radius=%f margin=%f}", height, radius, margin);97}9899100