Path: blob/master/modules/jolt_physics/shapes/jolt_box_shape_3d.cpp
10278 views
/**************************************************************************/1/* jolt_box_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_box_shape_3d.h"3132#include "../jolt_project_settings.h"33#include "../misc/jolt_type_conversions.h"3435#include "Jolt/Physics/Collision/Shape/BoxShape.h"3637JPH::ShapeRefC JoltBoxShape3D::_build() const {38const float min_half_extent = (float)half_extents[half_extents.min_axis_index()];39const float actual_margin = MIN(margin, min_half_extent * JoltProjectSettings::collision_margin_fraction);4041const JPH::BoxShapeSettings shape_settings(to_jolt(half_extents), actual_margin);42const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();4344ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics box 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 JoltBoxShape3D::get_data() const {50return half_extents;51}5253void JoltBoxShape3D::set_data(const Variant &p_data) {54ERR_FAIL_COND(p_data.get_type() != Variant::VECTOR3);5556const Vector3 new_half_extents = p_data;57if (unlikely(new_half_extents == half_extents)) {58return;59}6061half_extents = new_half_extents;6263destroy();64}6566void JoltBoxShape3D::set_margin(float p_margin) {67if (unlikely(margin == p_margin)) {68return;69}7071margin = p_margin;7273destroy();74}7576String JoltBoxShape3D::to_string() const {77return vformat("{half_extents=%v margin=%f}", half_extents, margin);78}7980AABB JoltBoxShape3D::get_aabb() const {81return AABB(-half_extents, half_extents * 2.0f);82}838485