Debian Codegraph @

modules/jolt_physics/shapes/jolt_shape_3d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* jolt_shape_3d.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "servers/physics_3d/physics_server_3d.h"
34
35 #include "Jolt/Jolt.h"
36
37 #include "Jolt/Physics/Collision/Shape/Shape.h"
38
39 class JoltShapedObject3D;
40
41 class JoltShape3D {
42 protected:
43 HashMap<JoltShapedObject3D *, int> ref_counts_by_owner;
44 Mutex jolt_ref_mutex;
45 RID rid;
46 JPH::ShapeRefC jolt_ref;
47
48 virtual JPH::ShapeRefC _build() const = 0;
49
50 String _owners_to_string() const;
51
52 public:
53 typedef PhysicsServer3D::ShapeType ShapeType;
54
55 virtual ~JoltShape3D() = 0;
56
57 RID get_rid() const { return rid; }
58 void set_rid(const RID &p_rid) { rid = p_rid; }
59
60 void add_owner(JoltShapedObject3D *p_owner);
61 void remove_owner(JoltShapedObject3D *p_owner);
62 void remove_self();
63
64 virtual ShapeType get_type() const = 0;
65 virtual bool is_convex() const = 0;
66
67 virtual Variant get_data() const = 0;
68 virtual void set_data(const Variant &p_data) = 0;
69
70 virtual float get_margin() const = 0;
71 virtual void set_margin(float p_margin) = 0;
72
73 virtual AABB get_aabb() const = 0;
74
75 float get_solver_bias() const;
76 void set_solver_bias(float p_bias);
77
78 JPH::ShapeRefC try_build();
79
80 void destroy();
81
82 const JPH::Shape *get_jolt_ref() const { return jolt_ref; }
83
84 static JPH::ShapeRefC with_scale(const JPH::Shape *p_shape, const Vector3 &p_scale);
85 static JPH::ShapeRefC with_basis_origin(const JPH::Shape *p_shape, const Basis &p_basis, const Vector3 &p_origin);
86 static JPH::ShapeRefC with_center_of_mass_offset(const JPH::Shape *p_shape, const Vector3 &p_offset);
87 static JPH::ShapeRefC with_center_of_mass(const JPH::Shape *p_shape, const Vector3 &p_center_of_mass);
88 static JPH::ShapeRefC with_user_data(const JPH::Shape *p_shape, uint64_t p_user_data);
89 static JPH::ShapeRefC with_double_sided(const JPH::Shape *p_shape, bool p_back_face_collision);
90 static JPH::ShapeRefC without_custom_shapes(const JPH::Shape *p_shape);
91
92 static Vector3 make_scale_valid(const JPH::Shape *p_shape, const Vector3 &p_scale);
93 static bool is_scale_valid(const Vector3 &p_scale, const Vector3 &p_valid_scale, real_t p_tolerance = 0.01f);
94 };
95
96 #ifdef DEBUG_ENABLED
97
98 #define JOLT_ENSURE_SCALE_NOT_ZERO(m_transform, m_msg) \
99 if (unlikely((m_transform).basis.determinant() == 0.0f)) { \
100 WARN_PRINT(vformat("%s " \
101 "The basis of the transform was singular, which is not supported by Jolt Physics. " \
102 "This is likely caused by one or more axes having a scale of zero. " \
103 "The basis (and thus its scale) will be treated as identity.", \
104 m_msg)); \
105 \
106 (m_transform).basis = Basis(); \
107 } else \
108 ((void)0)
109
110 #define ERR_PRINT_INVALID_SCALE_MSG(m_scale, m_valid_scale, m_msg) \
111 if (unlikely(!JoltShape3D::is_scale_valid(m_scale, valid_scale))) { \
112 ERR_PRINT(vformat("%s " \
113 "A scale of %v is not supported by Jolt Physics for this shape/body. " \
114 "The scale will instead be treated as %v.", \
115 m_msg, m_scale, valid_scale)); \
116 } else \
117 ((void)0)
118
119 #else
120
121 #define JOLT_ENSURE_SCALE_NOT_ZERO(m_transform, m_msg)
122
123 #define ERR_PRINT_INVALID_SCALE_MSG(m_scale, m_valid_scale, m_msg)
124
125 #endif
126
127 #define JOLT_ENSURE_SCALE_VALID(m_shape, m_scale, m_msg) \
128 if (true) { \
129 const Vector3 valid_scale = JoltShape3D::make_scale_valid(m_shape, m_scale); \
130 ERR_PRINT_INVALID_SCALE_MSG(m_scale, valid_scale, m_msg); \
131 (m_scale) = valid_scale; \
132 } else \
133 ((void)0)