| 1 | /**************************************************************************/ |
| 2 | /* joint_3d_gizmo_plugin.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_3d_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_node.h" |
| 34 | #include "editor/settings/editor_settings.h" |
| 35 | #include "scene/3d/physics/joints/cone_twist_joint_3d.h" |
| 36 | #include "scene/3d/physics/joints/generic_6dof_joint_3d.h" |
| 37 | #include "scene/3d/physics/joints/hinge_joint_3d.h" |
| 38 | #include "scene/3d/physics/joints/pin_joint_3d.h" |
| 39 | #include "scene/3d/physics/joints/slider_joint_3d.h" |
| 40 | #include "scene/main/timer.h" |
| 41 | |
| 42 | #define BODY_A_RADIUS 0.25 |
| 43 | #define BODY_B_RADIUS 0.27 |
| 44 | |
| 45 | Basis JointGizmosDrawer::look_body(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) { |
| 46 | const Vector3 &p_eye(p_joint_transform.origin); |
| 47 | const Vector3 &p_target(p_body_transform.origin); |
| 48 | |
| 49 | Vector3 v_x, v_y, v_z; |
| 50 | |
| 51 | // Look the body with X |
| 52 | v_x = p_target - p_eye; |
| 53 | v_x.normalize(); |
| 54 | |
| 55 | v_z = v_x.cross(Vector3(0, 1, 0)); |
| 56 | v_z.normalize(); |
| 57 | |
| 58 | v_y = v_z.cross(v_x); |
| 59 | v_y.normalize(); |
| 60 | |
| 61 | Basis base; |
| 62 | base.set_columns(v_x, v_y, v_z); |
| 63 | |
| 64 | // Absorb current joint transform |
| 65 | base = p_joint_transform.basis.inverse() * base; |
| 66 | |
| 67 | return base; |
| 68 | } |
| 69 | |
| 70 | Basis JointGizmosDrawer::look_body_toward(Vector3::Axis p_axis, const Transform3D &joint_transform, const Transform3D &body_transform) { |
| 71 | switch (p_axis) { |
| 72 | case Vector3::AXIS_X: |
| 73 | return look_body_toward_x(joint_transform, body_transform); |
| 74 | case Vector3::AXIS_Y: |
| 75 | return look_body_toward_y(joint_transform, body_transform); |
| 76 | case Vector3::AXIS_Z: |
| 77 | return look_body_toward_z(joint_transform, body_transform); |
| 78 | default: |
| 79 | return Basis(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Basis JointGizmosDrawer::look_body_toward_x(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) { |
| 84 | const Vector3 &p_eye(p_joint_transform.origin); |
| 85 | const Vector3 &p_target(p_body_transform.origin); |
| 86 | |
| 87 | const Vector3 p_front(p_joint_transform.basis.get_column(0)); |
| 88 | |
| 89 | Vector3 v_x, v_y, v_z; |
| 90 | |
| 91 | // Look the body with X |
| 92 | v_x = p_target - p_eye; |
| 93 | v_x.normalize(); |
| 94 | |
| 95 | v_y = p_front.cross(v_x); |
| 96 | v_y.normalize(); |
| 97 | |
| 98 | v_z = v_y.cross(p_front); |
| 99 | v_z.normalize(); |
| 100 | |
| 101 | // Clamp X to FRONT axis |
| 102 | v_x = p_front; |
| 103 | v_x.normalize(); |
| 104 | |
| 105 | Basis base; |
| 106 | base.set_columns(v_x, v_y, v_z); |
| 107 | |
| 108 | // Absorb current joint transform |
| 109 | base = p_joint_transform.basis.inverse() * base; |
| 110 | |
| 111 | return base; |
| 112 | } |
| 113 | |
| 114 | Basis JointGizmosDrawer::look_body_toward_y(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) { |
| 115 | const Vector3 &p_eye(p_joint_transform.origin); |
| 116 | const Vector3 &p_target(p_body_transform.origin); |
| 117 | |
| 118 | const Vector3 p_up(p_joint_transform.basis.get_column(1)); |
| 119 | |
| 120 | Vector3 v_x, v_y, v_z; |
| 121 | |
| 122 | // Look the body with X |
| 123 | v_x = p_target - p_eye; |
| 124 | v_x.normalize(); |
| 125 | |
| 126 | v_z = v_x.cross(p_up); |
| 127 | v_z.normalize(); |
| 128 | |
| 129 | v_x = p_up.cross(v_z); |
| 130 | v_x.normalize(); |
| 131 | |
| 132 | // Clamp Y to UP axis |
| 133 | v_y = p_up; |
| 134 | v_y.normalize(); |
| 135 | |
| 136 | Basis base; |
| 137 | base.set_columns(v_x, v_y, v_z); |
| 138 | |
| 139 | // Absorb current joint transform |
| 140 | base = p_joint_transform.basis.inverse() * base; |
| 141 | |
| 142 | return base; |
| 143 | } |
| 144 | |
| 145 | Basis JointGizmosDrawer::look_body_toward_z(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) { |
| 146 | const Vector3 &p_eye(p_joint_transform.origin); |
| 147 | const Vector3 &p_target(p_body_transform.origin); |
| 148 | |
| 149 | const Vector3 p_lateral(p_joint_transform.basis.get_column(2)); |
| 150 | |
| 151 | Vector3 v_x, v_y, v_z; |
| 152 | |
| 153 | // Look the body with X |
| 154 | v_x = p_target - p_eye; |
| 155 | v_x.normalize(); |
| 156 | |
| 157 | v_z = p_lateral; |
| 158 | v_z.normalize(); |
| 159 | |
| 160 | v_y = v_z.cross(v_x); |
| 161 | v_y.normalize(); |
| 162 | |
| 163 | // Clamp X to Z axis |
| 164 | v_x = v_y.cross(v_z); |
| 165 | v_x.normalize(); |
| 166 | |
| 167 | Basis base; |
| 168 | base.set_columns(v_x, v_y, v_z); |
| 169 | |
| 170 | // Absorb current joint transform |
| 171 | base = p_joint_transform.basis.inverse() * base; |
| 172 | |
| 173 | return base; |
| 174 | } |
| 175 | |
| 176 | void JointGizmosDrawer::draw_circle(Vector3::Axis p_axis, real_t p_radius, const Transform3D &p_offset, const Basis &p_base, real_t p_limit_lower, real_t p_limit_upper, Vector<Vector3> &r_points, bool p_inverse) { |
| 177 | if (p_limit_lower == p_limit_upper) { |
| 178 | r_points.push_back(p_offset.translated_local(Vector3()).origin); |
| 179 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(0.5, 0, 0))).origin); |
| 180 | |
| 181 | } else { |
| 182 | if (p_limit_lower > p_limit_upper) { |
| 183 | p_limit_lower = -Math::PI; |
| 184 | p_limit_upper = Math::PI; |
| 185 | } |
| 186 | |
| 187 | const int points = 32; |
| 188 | |
| 189 | for (int i = 0; i < points; i++) { |
| 190 | real_t s = p_limit_lower + i * (p_limit_upper - p_limit_lower) / points; |
| 191 | real_t n = p_limit_lower + (i + 1) * (p_limit_upper - p_limit_lower) / points; |
| 192 | |
| 193 | Vector3 from; |
| 194 | Vector3 to; |
| 195 | switch (p_axis) { |
| 196 | case Vector3::AXIS_X: |
| 197 | if (p_inverse) { |
| 198 | from = p_base.xform(Vector3(0, Math::sin(s), Math::cos(s))) * p_radius; |
| 199 | to = p_base.xform(Vector3(0, Math::sin(n), Math::cos(n))) * p_radius; |
| 200 | } else { |
| 201 | from = p_base.xform(Vector3(0, -Math::sin(s), Math::cos(s))) * p_radius; |
| 202 | to = p_base.xform(Vector3(0, -Math::sin(n), Math::cos(n))) * p_radius; |
| 203 | } |
| 204 | break; |
| 205 | case Vector3::AXIS_Y: |
| 206 | if (p_inverse) { |
| 207 | from = p_base.xform(Vector3(Math::cos(s), 0, -Math::sin(s))) * p_radius; |
| 208 | to = p_base.xform(Vector3(Math::cos(n), 0, -Math::sin(n))) * p_radius; |
| 209 | } else { |
| 210 | from = p_base.xform(Vector3(Math::cos(s), 0, Math::sin(s))) * p_radius; |
| 211 | to = p_base.xform(Vector3(Math::cos(n), 0, Math::sin(n))) * p_radius; |
| 212 | } |
| 213 | break; |
| 214 | case Vector3::AXIS_Z: |
| 215 | from = p_base.xform(Vector3(Math::cos(s), Math::sin(s), 0)) * p_radius; |
| 216 | to = p_base.xform(Vector3(Math::cos(n), Math::sin(n), 0)) * p_radius; |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | if (i == points - 1) { |
| 221 | r_points.push_back(p_offset.translated_local(to).origin); |
| 222 | r_points.push_back(p_offset.translated_local(Vector3()).origin); |
| 223 | } |
| 224 | if (i == 0) { |
| 225 | r_points.push_back(p_offset.translated_local(from).origin); |
| 226 | r_points.push_back(p_offset.translated_local(Vector3()).origin); |
| 227 | } |
| 228 | |
| 229 | r_points.push_back(p_offset.translated_local(from).origin); |
| 230 | r_points.push_back(p_offset.translated_local(to).origin); |
| 231 | } |
| 232 | |
| 233 | r_points.push_back(p_offset.translated_local(Vector3(0, p_radius * 1.5, 0)).origin); |
| 234 | r_points.push_back(p_offset.translated_local(Vector3()).origin); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void JointGizmosDrawer::draw_cone(const Transform3D &p_offset, const Basis &p_base, real_t p_swing, real_t p_twist, Vector<Vector3> &r_points) { |
| 239 | float r = 1.0; |
| 240 | float w = r * Math::sin(p_swing); |
| 241 | float d = r * Math::cos(p_swing); |
| 242 | |
| 243 | //swing |
| 244 | for (int i = 0; i < 360; i += 10) { |
| 245 | float ra = Math::deg_to_rad((float)i); |
| 246 | float rb = Math::deg_to_rad((float)i + 10); |
| 247 | Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w; |
| 248 | Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w; |
| 249 | |
| 250 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, a.x, a.y))).origin); |
| 251 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, b.x, b.y))).origin); |
| 252 | |
| 253 | if (i % 90 == 0) { |
| 254 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, a.x, a.y))).origin); |
| 255 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3())).origin); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3())).origin); |
| 260 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(1, 0, 0))).origin); |
| 261 | |
| 262 | /// Twist |
| 263 | float ts = Math::rad_to_deg(p_twist); |
| 264 | ts = MIN(ts, 720); |
| 265 | |
| 266 | for (int i = 0; i < int(ts); i += 5) { |
| 267 | float ra = Math::deg_to_rad((float)i); |
| 268 | float rb = Math::deg_to_rad((float)i + 5); |
| 269 | float c = i / 720.0; |
| 270 | float cn = (i + 5) / 720.0; |
| 271 | Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w * c; |
| 272 | Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w * cn; |
| 273 | |
| 274 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(c, a.x, a.y))).origin); |
| 275 | r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(cn, b.x, b.y))).origin); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | //// |
| 280 | |
| 281 | Joint3DGizmoPlugin::Joint3DGizmoPlugin() { |
| 282 | create_material("joint_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint")); |
| 283 | create_material("joint_body_a_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint_body_a")); |
| 284 | create_material("joint_body_b_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint_body_b")); |
| 285 | |
| 286 | update_timer = memnew(Timer); |
| 287 | update_timer->set_name("JointGizmoUpdateTimer"); |
| 288 | update_timer->set_wait_time(1.0 / 120.0); |
| 289 | update_timer->connect("timeout", callable_mp(this, &Joint3DGizmoPlugin::incremental_update_gizmos)); |
| 290 | update_timer->set_autostart(true); |
| 291 | callable_mp((Node *)EditorNode::get_singleton(), &Node::add_child).call_deferred(update_timer, false, Node::INTERNAL_MODE_DISABLED); |
| 292 | } |
| 293 | |
| 294 | void Joint3DGizmoPlugin::incremental_update_gizmos() { |
| 295 | if (!current_gizmos.is_empty()) { |
| 296 | HashSet<EditorNode3DGizmo *>::Iterator E = current_gizmos.find(last_drawn); |
| 297 | if (E) { |
| 298 | ++E; |
| 299 | } |
| 300 | if (!E) { |
| 301 | E = current_gizmos.begin(); |
| 302 | } |
| 303 | redraw(*E); |
| 304 | last_drawn = *E; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | bool Joint3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 309 | return Object::cast_to<Joint3D>(p_spatial) != nullptr; |
| 310 | } |
| 311 | |
| 312 | String Joint3DGizmoPlugin::get_gizmo_name() const { |
| 313 | return "Joint3D"; |
| 314 | } |
| 315 | |
| 316 | int Joint3DGizmoPlugin::get_priority() const { |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | void Joint3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 321 | Joint3D *joint = Object::cast_to<Joint3D>(p_gizmo->get_node_3d()); |
| 322 | |
| 323 | p_gizmo->clear(); |
| 324 | |
| 325 | Node3D *node_body_a = nullptr; |
| 326 | if (!joint->get_node_a().is_empty()) { |
| 327 | node_body_a = Object::cast_to<Node3D>(joint->get_node(joint->get_node_a())); |
| 328 | } |
| 329 | |
| 330 | Node3D *node_body_b = nullptr; |
| 331 | if (!joint->get_node_b().is_empty()) { |
| 332 | node_body_b = Object::cast_to<Node3D>(joint->get_node(joint->get_node_b())); |
| 333 | } |
| 334 | |
| 335 | if (!node_body_a && !node_body_b) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | Ref<Material> common_material = get_material("joint_material", p_gizmo); |
| 340 | Ref<Material> body_a_material = get_material("joint_body_a_material", p_gizmo); |
| 341 | Ref<Material> body_b_material = get_material("joint_body_b_material", p_gizmo); |
| 342 | |
| 343 | Vector<Vector3> points; |
| 344 | Vector<Vector3> body_a_points; |
| 345 | Vector<Vector3> body_b_points; |
| 346 | |
| 347 | if (Object::cast_to<PinJoint3D>(joint)) { |
| 348 | CreatePinJointGizmo(Transform3D(), points); |
| 349 | p_gizmo->add_collision_segments(points); |
| 350 | p_gizmo->add_lines(points, common_material); |
| 351 | } |
| 352 | |
| 353 | HingeJoint3D *hinge = Object::cast_to<HingeJoint3D>(joint); |
| 354 | if (hinge) { |
| 355 | CreateHingeJointGizmo( |
| 356 | Transform3D(), |
| 357 | hinge->get_global_transform(), |
| 358 | node_body_a ? node_body_a->get_global_transform() : Transform3D(), |
| 359 | node_body_b ? node_body_b->get_global_transform() : Transform3D(), |
| 360 | hinge->get_param(HingeJoint3D::PARAM_LIMIT_LOWER), |
| 361 | hinge->get_param(HingeJoint3D::PARAM_LIMIT_UPPER), |
| 362 | hinge->get_flag(HingeJoint3D::FLAG_USE_LIMIT), |
| 363 | points, |
| 364 | node_body_a ? &body_a_points : nullptr, |
| 365 | node_body_b ? &body_b_points : nullptr); |
| 366 | |
| 367 | p_gizmo->add_collision_segments(points); |
| 368 | p_gizmo->add_collision_segments(body_a_points); |
| 369 | p_gizmo->add_collision_segments(body_b_points); |
| 370 | |
| 371 | p_gizmo->add_lines(points, common_material); |
| 372 | p_gizmo->add_lines(body_a_points, body_a_material); |
| 373 | p_gizmo->add_lines(body_b_points, body_b_material); |
| 374 | } |
| 375 | |
| 376 | SliderJoint3D *slider = Object::cast_to<SliderJoint3D>(joint); |
| 377 | if (slider) { |
| 378 | CreateSliderJointGizmo( |
| 379 | Transform3D(), |
| 380 | slider->get_global_transform(), |
| 381 | node_body_a ? node_body_a->get_global_transform() : Transform3D(), |
| 382 | node_body_b ? node_body_b->get_global_transform() : Transform3D(), |
| 383 | slider->get_param(SliderJoint3D::PARAM_ANGULAR_LIMIT_LOWER), |
| 384 | slider->get_param(SliderJoint3D::PARAM_ANGULAR_LIMIT_UPPER), |
| 385 | slider->get_param(SliderJoint3D::PARAM_LINEAR_LIMIT_LOWER), |
| 386 | slider->get_param(SliderJoint3D::PARAM_LINEAR_LIMIT_UPPER), |
| 387 | points, |
| 388 | node_body_a ? &body_a_points : nullptr, |
| 389 | node_body_b ? &body_b_points : nullptr); |
| 390 | |
| 391 | p_gizmo->add_collision_segments(points); |
| 392 | p_gizmo->add_collision_segments(body_a_points); |
| 393 | p_gizmo->add_collision_segments(body_b_points); |
| 394 | |
| 395 | p_gizmo->add_lines(points, common_material); |
| 396 | p_gizmo->add_lines(body_a_points, body_a_material); |
| 397 | p_gizmo->add_lines(body_b_points, body_b_material); |
| 398 | } |
| 399 | |
| 400 | ConeTwistJoint3D *cone = Object::cast_to<ConeTwistJoint3D>(joint); |
| 401 | if (cone) { |
| 402 | CreateConeTwistJointGizmo( |
| 403 | Transform3D(), |
| 404 | cone->get_global_transform(), |
| 405 | node_body_a ? node_body_a->get_global_transform() : Transform3D(), |
| 406 | node_body_b ? node_body_b->get_global_transform() : Transform3D(), |
| 407 | cone->get_param(ConeTwistJoint3D::PARAM_SWING_SPAN), |
| 408 | cone->get_param(ConeTwistJoint3D::PARAM_TWIST_SPAN), |
| 409 | node_body_a ? &body_a_points : nullptr, |
| 410 | node_body_b ? &body_b_points : nullptr); |
| 411 | |
| 412 | p_gizmo->add_collision_segments(body_a_points); |
| 413 | p_gizmo->add_collision_segments(body_b_points); |
| 414 | |
| 415 | p_gizmo->add_lines(body_a_points, body_a_material); |
| 416 | p_gizmo->add_lines(body_b_points, body_b_material); |
| 417 | } |
| 418 | |
| 419 | Generic6DOFJoint3D *gen = Object::cast_to<Generic6DOFJoint3D>(joint); |
| 420 | if (gen) { |
| 421 | CreateGeneric6DOFJointGizmo( |
| 422 | Transform3D(), |
| 423 | gen->get_global_transform(), |
| 424 | node_body_a ? node_body_a->get_global_transform() : Transform3D(), |
| 425 | node_body_b ? node_body_b->get_global_transform() : Transform3D(), |
| 426 | |
| 427 | gen->get_param_x(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT), |
| 428 | gen->get_param_x(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT), |
| 429 | gen->get_param_x(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT), |
| 430 | gen->get_param_x(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT), |
| 431 | gen->get_flag_x(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT), |
| 432 | gen->get_flag_x(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT), |
| 433 | |
| 434 | gen->get_param_y(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT), |
| 435 | gen->get_param_y(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT), |
| 436 | gen->get_param_y(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT), |
| 437 | gen->get_param_y(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT), |
| 438 | gen->get_flag_y(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT), |
| 439 | gen->get_flag_y(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT), |
| 440 | |
| 441 | gen->get_param_z(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT), |
| 442 | gen->get_param_z(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT), |
| 443 | gen->get_param_z(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT), |
| 444 | gen->get_param_z(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT), |
| 445 | gen->get_flag_z(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT), |
| 446 | gen->get_flag_z(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT), |
| 447 | |
| 448 | points, |
| 449 | node_body_a ? &body_a_points : nullptr, |
| 450 | node_body_a ? &body_b_points : nullptr); |
| 451 | |
| 452 | p_gizmo->add_collision_segments(points); |
| 453 | p_gizmo->add_collision_segments(body_a_points); |
| 454 | p_gizmo->add_collision_segments(body_b_points); |
| 455 | |
| 456 | p_gizmo->add_lines(points, common_material); |
| 457 | p_gizmo->add_lines(body_a_points, body_a_material); |
| 458 | p_gizmo->add_lines(body_b_points, body_b_material); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void Joint3DGizmoPlugin::CreatePinJointGizmo(const Transform3D &p_offset, Vector<Vector3> &r_cursor_points) { |
| 463 | float cs = 0.25; |
| 464 | |
| 465 | r_cursor_points.push_back(p_offset.translated_local(Vector3(+cs, 0, 0)).origin); |
| 466 | r_cursor_points.push_back(p_offset.translated_local(Vector3(-cs, 0, 0)).origin); |
| 467 | r_cursor_points.push_back(p_offset.translated_local(Vector3(0, +cs, 0)).origin); |
| 468 | r_cursor_points.push_back(p_offset.translated_local(Vector3(0, -cs, 0)).origin); |
| 469 | r_cursor_points.push_back(p_offset.translated_local(Vector3(0, 0, +cs)).origin); |
| 470 | r_cursor_points.push_back(p_offset.translated_local(Vector3(0, 0, -cs)).origin); |
| 471 | } |
| 472 | |
| 473 | void Joint3DGizmoPlugin::CreateHingeJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_limit_lower, real_t p_limit_upper, bool p_use_limit, Vector<Vector3> &r_common_points, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) { |
| 474 | r_common_points.push_back(p_offset.translated_local(Vector3(0, 0, 0.5)).origin); |
| 475 | r_common_points.push_back(p_offset.translated_local(Vector3(0, 0, -0.5)).origin); |
| 476 | |
| 477 | if (!p_use_limit) { |
| 478 | p_limit_upper = -1; |
| 479 | p_limit_lower = 0; |
| 480 | } |
| 481 | |
| 482 | if (r_body_a_points) { |
| 483 | JointGizmosDrawer::draw_circle(Vector3::AXIS_Z, |
| 484 | BODY_A_RADIUS, |
| 485 | p_offset, |
| 486 | JointGizmosDrawer::look_body_toward_z(p_trs_joint, p_trs_body_a), |
| 487 | p_limit_lower, |
| 488 | p_limit_upper, |
| 489 | *r_body_a_points); |
| 490 | } |
| 491 | |
| 492 | if (r_body_b_points) { |
| 493 | JointGizmosDrawer::draw_circle(Vector3::AXIS_Z, |
| 494 | BODY_B_RADIUS, |
| 495 | p_offset, |
| 496 | JointGizmosDrawer::look_body_toward_z(p_trs_joint, p_trs_body_b), |
| 497 | p_limit_lower, |
| 498 | p_limit_upper, |
| 499 | *r_body_b_points); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void Joint3DGizmoPlugin::CreateSliderJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_angular_limit_lower, real_t p_angular_limit_upper, real_t p_linear_limit_lower, real_t p_linear_limit_upper, Vector<Vector3> &r_points, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) { |
| 504 | p_linear_limit_lower = -p_linear_limit_lower; |
| 505 | p_linear_limit_upper = -p_linear_limit_upper; |
| 506 | |
| 507 | float cs = 0.25; |
| 508 | r_points.push_back(p_offset.translated_local(Vector3(0, 0, 0.5)).origin); |
| 509 | r_points.push_back(p_offset.translated_local(Vector3(0, 0, -0.5)).origin); |
| 510 | |
| 511 | if (p_linear_limit_lower >= p_linear_limit_upper) { |
| 512 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, 0, 0)).origin); |
| 513 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, 0, 0)).origin); |
| 514 | |
| 515 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, -cs)).origin); |
| 516 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, cs)).origin); |
| 517 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, cs)).origin); |
| 518 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, cs)).origin); |
| 519 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, cs)).origin); |
| 520 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, -cs)).origin); |
| 521 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, -cs)).origin); |
| 522 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, -cs)).origin); |
| 523 | |
| 524 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, -cs)).origin); |
| 525 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, cs)).origin); |
| 526 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, cs)).origin); |
| 527 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, cs)).origin); |
| 528 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, cs)).origin); |
| 529 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, -cs)).origin); |
| 530 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, -cs)).origin); |
| 531 | r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, -cs)).origin); |
| 532 | |
| 533 | } else { |
| 534 | r_points.push_back(p_offset.translated_local(Vector3(+cs * 2, 0, 0)).origin); |
| 535 | r_points.push_back(p_offset.translated_local(Vector3(-cs * 2, 0, 0)).origin); |
| 536 | } |
| 537 | |
| 538 | if (r_body_a_points) { |
| 539 | JointGizmosDrawer::draw_circle( |
| 540 | Vector3::AXIS_X, |
| 541 | BODY_A_RADIUS, |
| 542 | p_offset, |
| 543 | JointGizmosDrawer::look_body_toward(Vector3::AXIS_X, p_trs_joint, p_trs_body_a), |
| 544 | p_angular_limit_lower, |
| 545 | p_angular_limit_upper, |
| 546 | *r_body_a_points); |
| 547 | } |
| 548 | |
| 549 | if (r_body_b_points) { |
| 550 | JointGizmosDrawer::draw_circle( |
| 551 | Vector3::AXIS_X, |
| 552 | BODY_B_RADIUS, |
| 553 | p_offset, |
| 554 | JointGizmosDrawer::look_body_toward(Vector3::AXIS_X, p_trs_joint, p_trs_body_b), |
| 555 | p_angular_limit_lower, |
| 556 | p_angular_limit_upper, |
| 557 | *r_body_b_points, |
| 558 | true); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | void Joint3DGizmoPlugin::CreateConeTwistJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_swing, real_t p_twist, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) { |
| 563 | if (r_body_a_points) { |
| 564 | JointGizmosDrawer::draw_cone( |
| 565 | p_offset, |
| 566 | JointGizmosDrawer::look_body(p_trs_joint, p_trs_body_a), |
| 567 | p_swing, |
| 568 | p_twist, |
| 569 | *r_body_a_points); |
| 570 | } |
| 571 | |
| 572 | if (r_body_b_points) { |
| 573 | JointGizmosDrawer::draw_cone( |
| 574 | p_offset, |
| 575 | JointGizmosDrawer::look_body(p_trs_joint, p_trs_body_b), |
| 576 | p_swing, |
| 577 | p_twist, |
| 578 | *r_body_b_points); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | void Joint3DGizmoPlugin::CreateGeneric6DOFJointGizmo( |
| 583 | const Transform3D &p_offset, |
| 584 | const Transform3D &p_trs_joint, |
| 585 | const Transform3D &p_trs_body_a, |
| 586 | const Transform3D &p_trs_body_b, |
| 587 | real_t p_angular_limit_lower_x, |
| 588 | real_t p_angular_limit_upper_x, |
| 589 | real_t p_linear_limit_lower_x, |
| 590 | real_t p_linear_limit_upper_x, |
| 591 | bool p_enable_angular_limit_x, |
| 592 | bool p_enable_linear_limit_x, |
| 593 | real_t p_angular_limit_lower_y, |
| 594 | real_t p_angular_limit_upper_y, |
| 595 | real_t p_linear_limit_lower_y, |
| 596 | real_t p_linear_limit_upper_y, |
| 597 | bool p_enable_angular_limit_y, |
| 598 | bool p_enable_linear_limit_y, |
| 599 | real_t p_angular_limit_lower_z, |
| 600 | real_t p_angular_limit_upper_z, |
| 601 | real_t p_linear_limit_lower_z, |
| 602 | real_t p_linear_limit_upper_z, |
| 603 | bool p_enable_angular_limit_z, |
| 604 | bool p_enable_linear_limit_z, |
| 605 | Vector<Vector3> &r_points, |
| 606 | Vector<Vector3> *r_body_a_points, |
| 607 | Vector<Vector3> *r_body_b_points) { |
| 608 | float cs = 0.25; |
| 609 | |
| 610 | for (int ax = 0; ax < 3; ax++) { |
| 611 | float ll = 0; |
| 612 | float ul = 0; |
| 613 | float lll = 0; |
| 614 | float lul = 0; |
| 615 | |
| 616 | int a1 = 0; |
| 617 | int a2 = 0; |
| 618 | int a3 = 0; |
| 619 | bool enable_ang = false; |
| 620 | bool enable_lin = false; |
| 621 | |
| 622 | switch (ax) { |
| 623 | case 0: |
| 624 | ll = p_angular_limit_lower_x; |
| 625 | ul = p_angular_limit_upper_x; |
| 626 | lll = -p_linear_limit_lower_x; |
| 627 | lul = -p_linear_limit_upper_x; |
| 628 | enable_ang = p_enable_angular_limit_x; |
| 629 | enable_lin = p_enable_linear_limit_x; |
| 630 | a1 = 0; |
| 631 | a2 = 1; |
| 632 | a3 = 2; |
| 633 | break; |
| 634 | case 1: |
| 635 | ll = p_angular_limit_lower_y; |
| 636 | ul = p_angular_limit_upper_y; |
| 637 | lll = -p_linear_limit_lower_y; |
| 638 | lul = -p_linear_limit_upper_y; |
| 639 | enable_ang = p_enable_angular_limit_y; |
| 640 | enable_lin = p_enable_linear_limit_y; |
| 641 | a1 = 1; |
| 642 | a2 = 2; |
| 643 | a3 = 0; |
| 644 | break; |
| 645 | case 2: |
| 646 | ll = p_angular_limit_lower_z; |
| 647 | ul = p_angular_limit_upper_z; |
| 648 | lll = -p_linear_limit_lower_z; |
| 649 | lul = -p_linear_limit_upper_z; |
| 650 | enable_ang = p_enable_angular_limit_z; |
| 651 | enable_lin = p_enable_linear_limit_z; |
| 652 | a1 = 2; |
| 653 | a2 = 0; |
| 654 | a3 = 1; |
| 655 | break; |
| 656 | } |
| 657 | |
| 658 | #define ADD_VTX(x, y, z) \ |
| 659 | { \ |
| 660 | Vector3 v; \ |
| 661 | v[a1] = (x); \ |
| 662 | v[a2] = (y); \ |
| 663 | v[a3] = (z); \ |
| 664 | r_points.push_back(p_offset.translated_local(v).origin); \ |
| 665 | } |
| 666 | |
| 667 | if (enable_lin && lll >= lul) { |
| 668 | ADD_VTX(lul, 0, 0); |
| 669 | ADD_VTX(lll, 0, 0); |
| 670 | |
| 671 | ADD_VTX(lul, -cs, -cs); |
| 672 | ADD_VTX(lul, -cs, cs); |
| 673 | ADD_VTX(lul, -cs, cs); |
| 674 | ADD_VTX(lul, cs, cs); |
| 675 | ADD_VTX(lul, cs, cs); |
| 676 | ADD_VTX(lul, cs, -cs); |
| 677 | ADD_VTX(lul, cs, -cs); |
| 678 | ADD_VTX(lul, -cs, -cs); |
| 679 | |
| 680 | ADD_VTX(lll, -cs, -cs); |
| 681 | ADD_VTX(lll, -cs, cs); |
| 682 | ADD_VTX(lll, -cs, cs); |
| 683 | ADD_VTX(lll, cs, cs); |
| 684 | ADD_VTX(lll, cs, cs); |
| 685 | ADD_VTX(lll, cs, -cs); |
| 686 | ADD_VTX(lll, cs, -cs); |
| 687 | ADD_VTX(lll, -cs, -cs); |
| 688 | |
| 689 | } else { |
| 690 | ADD_VTX(+cs * 2, 0, 0); |
| 691 | ADD_VTX(-cs * 2, 0, 0); |
| 692 | } |
| 693 | |
| 694 | if (!enable_ang) { |
| 695 | ll = 0; |
| 696 | ul = -1; |
| 697 | } |
| 698 | |
| 699 | if (r_body_a_points) { |
| 700 | JointGizmosDrawer::draw_circle( |
| 701 | static_cast<Vector3::Axis>(ax), |
| 702 | BODY_A_RADIUS, |
| 703 | p_offset, |
| 704 | JointGizmosDrawer::look_body_toward(static_cast<Vector3::Axis>(ax), p_trs_joint, p_trs_body_a), |
| 705 | ll, |
| 706 | ul, |
| 707 | *r_body_a_points, |
| 708 | true); |
| 709 | } |
| 710 | |
| 711 | if (r_body_b_points) { |
| 712 | JointGizmosDrawer::draw_circle( |
| 713 | static_cast<Vector3::Axis>(ax), |
| 714 | BODY_B_RADIUS, |
| 715 | p_offset, |
| 716 | JointGizmosDrawer::look_body_toward(static_cast<Vector3::Axis>(ax), p_trs_joint, p_trs_body_b), |
| 717 | ll, |
| 718 | ul, |
| 719 | *r_body_b_points); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | #undef ADD_VTX |
| 724 | } |