| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "godot_body_3d.h" |
| 34 |
#include "godot_constraint_3d.h" |
| 35 |
|
| 36 |
class GodotJoint3D : public GodotConstraint3D { |
| 37 |
protected: |
| 38 |
bool dynamic_A = false; |
| 39 |
bool dynamic_B = false; |
| 40 |
|
| 41 |
void plane_space(const Vector3 &n, Vector3 &p, Vector3 &q) { |
| 42 |
if (Math::abs(n.z) > Math::SQRT12) { |
| 43 |
|
| 44 |
real_t a = n[1] * n[1] + n[2] * n[2]; |
| 45 |
real_t k = 1.0 / Math::sqrt(a); |
| 46 |
p = Vector3(0, -n[2] * k, n[1] * k); |
| 47 |
|
| 48 |
q = Vector3(a * k, -n[0] * p[2], n[0] * p[1]); |
| 49 |
} else { |
| 50 |
|
| 51 |
real_t a = n.x * n.x + n.y * n.y; |
| 52 |
real_t k = 1.0 / Math::sqrt(a); |
| 53 |
p = Vector3(-n.y * k, n.x * k, 0); |
| 54 |
|
| 55 |
q = Vector3(-n.z * p.y, n.z * p.x, a * k); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
_FORCE_INLINE_ real_t atan2fast(real_t y, real_t x) { |
| 60 |
real_t coeff_1 = Math::PI / 4.0f; |
| 61 |
real_t coeff_2 = 3.0f * coeff_1; |
| 62 |
real_t abs_y = Math::abs(y); |
| 63 |
real_t angle; |
| 64 |
if (x >= 0.0f) { |
| 65 |
real_t r = (x - abs_y) / (x + abs_y); |
| 66 |
angle = coeff_1 - coeff_1 * r; |
| 67 |
} else { |
| 68 |
real_t r = (x + abs_y) / (abs_y - x); |
| 69 |
angle = coeff_2 - coeff_1 * r; |
| 70 |
} |
| 71 |
return (y < 0.0f) ? -angle : angle; |
| 72 |
} |
| 73 |
|
| 74 |
public: |
| 75 |
virtual bool setup(real_t p_step) override { return false; } |
| 76 |
virtual bool pre_solve(real_t p_step) override { return true; } |
| 77 |
virtual void solve(real_t p_step) override {} |
| 78 |
|
| 79 |
void copy_settings_from(GodotJoint3D *p_joint) { |
| 80 |
set_self(p_joint->get_self()); |
| 81 |
set_priority(p_joint->get_priority()); |
| 82 |
disable_collisions_between_bodies(p_joint->is_disabled_collisions_between_bodies()); |
| 83 |
} |
| 84 |
|
| 85 |
virtual PhysicsServer3D::JointType get_type() const { return PhysicsServer3D::JOINT_TYPE_MAX; } |
| 86 |
_FORCE_INLINE_ GodotJoint3D(GodotBody3D **p_body_ptr = nullptr, int p_body_count = 0) : |
| 87 |
GodotConstraint3D(p_body_ptr, p_body_count) { |
| 88 |
} |
| 89 |
|
| 90 |
virtual ~GodotJoint3D() { |
| 91 |
for (int i = 0; i < get_body_count(); i++) { |
| 92 |
GodotBody3D *body = get_body_ptr()[i]; |
| 93 |
if (body) { |
| 94 |
body->remove_constraint(this); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
}; |