godot_joint_3d.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**************************************************************************/
  2. /* godot_joint_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. #ifndef GODOT_JOINT_3D_H
  31. #define GODOT_JOINT_3D_H
  32. #include "godot_body_3d.h"
  33. #include "godot_constraint_3d.h"
  34. class GodotJoint3D : public GodotConstraint3D {
  35. protected:
  36. bool dynamic_A = false;
  37. bool dynamic_B = false;
  38. void plane_space(const Vector3 &n, Vector3 &p, Vector3 &q) {
  39. if (Math::abs(n.z) > Math_SQRT12) {
  40. // choose p in y-z plane
  41. real_t a = n[1] * n[1] + n[2] * n[2];
  42. real_t k = 1.0 / Math::sqrt(a);
  43. p = Vector3(0, -n[2] * k, n[1] * k);
  44. // set q = n x p
  45. q = Vector3(a * k, -n[0] * p[2], n[0] * p[1]);
  46. } else {
  47. // choose p in x-y plane
  48. real_t a = n.x * n.x + n.y * n.y;
  49. real_t k = 1.0 / Math::sqrt(a);
  50. p = Vector3(-n.y * k, n.x * k, 0);
  51. // set q = n x p
  52. q = Vector3(-n.z * p.y, n.z * p.x, a * k);
  53. }
  54. }
  55. _FORCE_INLINE_ real_t atan2fast(real_t y, real_t x) {
  56. real_t coeff_1 = Math_PI / 4.0f;
  57. real_t coeff_2 = 3.0f * coeff_1;
  58. real_t abs_y = Math::abs(y);
  59. real_t angle;
  60. if (x >= 0.0f) {
  61. real_t r = (x - abs_y) / (x + abs_y);
  62. angle = coeff_1 - coeff_1 * r;
  63. } else {
  64. real_t r = (x + abs_y) / (abs_y - x);
  65. angle = coeff_2 - coeff_1 * r;
  66. }
  67. return (y < 0.0f) ? -angle : angle;
  68. }
  69. public:
  70. virtual bool setup(real_t p_step) override { return false; }
  71. virtual bool pre_solve(real_t p_step) override { return true; }
  72. virtual void solve(real_t p_step) override {}
  73. void copy_settings_from(GodotJoint3D *p_joint) {
  74. set_self(p_joint->get_self());
  75. set_priority(p_joint->get_priority());
  76. disable_collisions_between_bodies(p_joint->is_disabled_collisions_between_bodies());
  77. }
  78. virtual PhysicsServer3D::JointType get_type() const { return PhysicsServer3D::JOINT_TYPE_MAX; }
  79. _FORCE_INLINE_ GodotJoint3D(GodotBody3D **p_body_ptr = nullptr, int p_body_count = 0) :
  80. GodotConstraint3D(p_body_ptr, p_body_count) {
  81. }
  82. virtual ~GodotJoint3D() {
  83. for (int i = 0; i < get_body_count(); i++) {
  84. GodotBody3D *body = get_body_ptr()[i];
  85. if (body) {
  86. body->remove_constraint(this);
  87. }
  88. }
  89. }
  90. };
  91. #endif // GODOT_JOINT_3D_H