Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/joint_limitation_cone_3d.cpp
21345 views
1
/**************************************************************************/
2
/* joint_limitation_cone_3d.cpp */
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
#include "joint_limitation_cone_3d.h"
32
33
#ifndef DISABLE_DEPRECATED
34
bool JointLimitationCone3D::_set(const StringName &p_path, const Variant &p_value) {
35
// To keep compatibility between 4.6.beta2 and beta3.
36
if (p_path == SNAME("radius_range")) {
37
set_angle((float)p_value * Math::TAU);
38
} else {
39
return false;
40
}
41
return true;
42
}
43
#endif // DISABLE_DEPRECATED
44
45
void JointLimitationCone3D::set_angle(real_t p_angle) {
46
angle = p_angle;
47
emit_changed();
48
}
49
50
real_t JointLimitationCone3D::get_angle() const {
51
return angle;
52
}
53
54
void JointLimitationCone3D::_bind_methods() {
55
ClassDB::bind_method(D_METHOD("set_angle", "angle"), &JointLimitationCone3D::set_angle);
56
ClassDB::bind_method(D_METHOD("get_angle"), &JointLimitationCone3D::get_angle);
57
58
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angle", PROPERTY_HINT_RANGE, "0,360,0.01,radians_as_degrees"), "set_angle", "get_angle");
59
}
60
61
Vector3 JointLimitationCone3D::_solve(const Vector3 &p_direction) const {
62
// Assume the central (forward of the cone) axis is the +Y.
63
// This is based on the coordinate system set by JointLimitation3D::_make_space().
64
Vector3 center_axis = Vector3(0, 1, 0);
65
66
// Apply the limitation if the angle exceeds radius_range * PI.
67
real_t current_angle = p_direction.angle_to(center_axis);
68
real_t max_angle = angle * 0.5;
69
70
if (current_angle <= max_angle) {
71
// If within the limitation range, return the new direction as is.
72
return p_direction;
73
}
74
75
// If outside the limitation range, calculate the closest direction within the range.
76
// Define a plane using the central axis and the new direction vector.
77
Vector3 plane_normal;
78
79
// Special handling for when the new direction vector is completely opposite to the central axis.
80
if (Math::is_equal_approx((double)current_angle, Math::PI)) {
81
// Select an arbitrary perpendicular axis
82
plane_normal = center_axis.get_any_perpendicular();
83
} else {
84
plane_normal = center_axis.cross(p_direction).normalized();
85
}
86
87
// Calculate a vector rotated by the maximum angle from the central axis on the plane.
88
Quaternion rotation = Quaternion(plane_normal, max_angle);
89
Vector3 limited_dir = rotation.xform(center_axis);
90
91
// Return the vector within the limitation range that is closest to p_direction.
92
// This preserves the directionality of p_direction as much as possible.
93
Vector3 projection = p_direction - center_axis * p_direction.dot(center_axis);
94
if (projection.length_squared() > CMP_EPSILON) {
95
Vector3 side_dir = projection.normalized();
96
Quaternion side_rotation = Quaternion(center_axis.cross(side_dir).normalized(), max_angle);
97
limited_dir = side_rotation.xform(center_axis);
98
}
99
100
return limited_dir.normalized();
101
}
102
103
#ifdef TOOLS_ENABLED
104
void JointLimitationCone3D::draw_shape(Ref<SurfaceTool> &p_surface_tool, const Transform3D &p_transform, float p_bone_length, const Color &p_color) const {
105
static const int N = 16;
106
static const real_t DP = Math::TAU / (real_t)N;
107
108
real_t sphere_r = p_bone_length * (real_t)0.25;
109
if (sphere_r <= CMP_EPSILON) {
110
return;
111
}
112
real_t alpha = CLAMP((real_t)angle, (real_t)0.0, (real_t)Math::TAU) * 0.5;
113
real_t y_cap = sphere_r * Math::cos(alpha);
114
real_t r_cap = sphere_r * Math::sin(alpha);
115
116
LocalVector<Vector3> vts;
117
118
// Cone bottom.
119
if (r_cap > CMP_EPSILON) {
120
for (int i = 0; i < N; i++) {
121
real_t a0 = (real_t)i * DP;
122
real_t a1 = (real_t)((i + 1) % N) * DP;
123
Vector3 p0 = Vector3(r_cap * Math::cos(a0), y_cap, r_cap * Math::sin(a0));
124
Vector3 p1 = Vector3(r_cap * Math::cos(a1), y_cap, r_cap * Math::sin(a1));
125
vts.push_back(p0);
126
vts.push_back(p1);
127
}
128
}
129
130
// Rotate arcs around Y-axis.
131
real_t t_start;
132
real_t arc_len;
133
if (alpha <= (real_t)1e-6) {
134
t_start = (real_t)0.5 * Math::PI;
135
arc_len = Math::PI;
136
} else {
137
t_start = (real_t)0.5 * Math::PI + alpha;
138
arc_len = Math::PI - alpha;
139
}
140
real_t dt = arc_len / (real_t)N;
141
142
for (int k = 0; k < N; k++) {
143
Basis ry(Vector3(0, 1, 0), (real_t)k * DP);
144
145
Vector3 prev = ry.xform(Vector3(sphere_r * Math::cos(t_start), sphere_r * Math::sin(t_start), 0));
146
147
for (int s = 1; s <= N; s++) {
148
real_t t = t_start + dt * (real_t)s;
149
Vector3 cur = ry.xform(Vector3(sphere_r * Math::cos(t), sphere_r * Math::sin(t), 0));
150
151
vts.push_back(prev);
152
vts.push_back(cur);
153
154
prev = cur;
155
}
156
157
Vector3 mouth = ry.xform(Vector3(sphere_r * Math::cos(t_start), sphere_r * Math::sin(t_start), 0));
158
Vector3 center = Vector3();
159
160
vts.push_back(center);
161
vts.push_back(mouth);
162
}
163
164
// Stack rings.
165
for (int i = 1; i <= 3; i++) {
166
for (int sgn = -1; sgn <= 1; sgn += 2) {
167
real_t y = (real_t)sgn * sphere_r * ((real_t)i / (real_t)4.0);
168
if (y >= y_cap - CMP_EPSILON) {
169
continue;
170
}
171
real_t ring_r2 = sphere_r * sphere_r - y * y;
172
if (ring_r2 <= (real_t)0.0) {
173
continue;
174
}
175
real_t ring_r = Math::sqrt(ring_r2);
176
177
for (int j = 0; j < N; j++) {
178
real_t a0 = (real_t)j * DP;
179
real_t a1 = (real_t)((j + 1) % N) * DP;
180
Vector3 p0 = Vector3(ring_r * Math::cos(a0), y, ring_r * Math::sin(a0));
181
Vector3 p1 = Vector3(ring_r * Math::cos(a1), y, ring_r * Math::sin(a1));
182
183
vts.push_back(p0);
184
vts.push_back(p1);
185
}
186
}
187
}
188
189
for (int64_t i = 0; i < vts.size(); i++) {
190
p_surface_tool->set_color(p_color);
191
p_surface_tool->add_vertex(p_transform.xform(vts[i]));
192
}
193
}
194
#endif // TOOLS_ENABLED
195
196