Path: blob/master/modules/jolt_physics/shapes/jolt_shape_3d.h
10278 views
/**************************************************************************/1/* jolt_shape_3d.h */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#pragma once3132#include "servers/physics_server_3d.h"3334#include "Jolt/Jolt.h"3536#include "Jolt/Physics/Collision/Shape/Shape.h"3738class JoltShapedObject3D;3940class JoltShape3D {41protected:42HashMap<JoltShapedObject3D *, int> ref_counts_by_owner;43Mutex jolt_ref_mutex;44RID rid;45JPH::ShapeRefC jolt_ref;4647virtual JPH::ShapeRefC _build() const = 0;4849String _owners_to_string() const;5051public:52typedef PhysicsServer3D::ShapeType ShapeType;5354virtual ~JoltShape3D() = 0;5556RID get_rid() const { return rid; }57void set_rid(const RID &p_rid) { rid = p_rid; }5859void add_owner(JoltShapedObject3D *p_owner);60void remove_owner(JoltShapedObject3D *p_owner);61void remove_self();6263virtual ShapeType get_type() const = 0;64virtual bool is_convex() const = 0;6566virtual Variant get_data() const = 0;67virtual void set_data(const Variant &p_data) = 0;6869virtual float get_margin() const = 0;70virtual void set_margin(float p_margin) = 0;7172virtual AABB get_aabb() const = 0;7374float get_solver_bias() const;75void set_solver_bias(float p_bias);7677JPH::ShapeRefC try_build();7879void destroy();8081const JPH::Shape *get_jolt_ref() const { return jolt_ref; }8283static JPH::ShapeRefC with_scale(const JPH::Shape *p_shape, const Vector3 &p_scale);84static JPH::ShapeRefC with_basis_origin(const JPH::Shape *p_shape, const Basis &p_basis, const Vector3 &p_origin);85static JPH::ShapeRefC with_center_of_mass_offset(const JPH::Shape *p_shape, const Vector3 &p_offset);86static JPH::ShapeRefC with_center_of_mass(const JPH::Shape *p_shape, const Vector3 &p_center_of_mass);87static JPH::ShapeRefC with_user_data(const JPH::Shape *p_shape, uint64_t p_user_data);88static JPH::ShapeRefC with_double_sided(const JPH::Shape *p_shape, bool p_back_face_collision);89static JPH::ShapeRefC without_custom_shapes(const JPH::Shape *p_shape);9091static Vector3 make_scale_valid(const JPH::Shape *p_shape, const Vector3 &p_scale);92static bool is_scale_valid(const Vector3 &p_scale, const Vector3 &p_valid_scale, real_t p_tolerance = 0.01f);93};9495#ifdef DEBUG_ENABLED9697#define JOLT_ENSURE_SCALE_NOT_ZERO(m_transform, m_msg) \98if (unlikely((m_transform).basis.determinant() == 0.0f)) { \99WARN_PRINT(vformat("%s " \100"The basis of the transform was singular, which is not supported by Jolt Physics. " \101"This is likely caused by one or more axes having a scale of zero. " \102"The basis (and thus its scale) will be treated as identity.", \103m_msg)); \104\105(m_transform).basis = Basis(); \106} else \107((void)0)108109#define ERR_PRINT_INVALID_SCALE_MSG(m_scale, m_valid_scale, m_msg) \110if (unlikely(!JoltShape3D::is_scale_valid(m_scale, valid_scale))) { \111ERR_PRINT(vformat("%s " \112"A scale of %v is not supported by Jolt Physics for this shape/body. " \113"The scale will instead be treated as %v.", \114m_msg, m_scale, valid_scale)); \115} else \116((void)0)117118#else119120#define JOLT_ENSURE_SCALE_NOT_ZERO(m_transform, m_msg)121122#define ERR_PRINT_INVALID_SCALE_MSG(m_scale, m_valid_scale, m_msg)123124#endif125126#define JOLT_ENSURE_SCALE_VALID(m_shape, m_scale, m_msg) \127if (true) { \128const Vector3 valid_scale = JoltShape3D::make_scale_valid(m_shape, m_scale); \129ERR_PRINT_INVALID_SCALE_MSG(m_scale, valid_scale, m_msg); \130(m_scale) = valid_scale; \131} else \132((void)0)133134135