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