| 1 | /**************************************************************************/ |
| 2 | /* godot_body_pair_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_BODY_PAIR_3D_H |
| 32 | #define GODOT_BODY_PAIR_3D_H |
| 33 | |
| 34 | #include "godot_body_3d.h" |
| 35 | #include "godot_constraint_3d.h" |
| 36 | #include "godot_soft_body_3d.h" |
| 37 | |
| 38 | #include "core/templates/local_vector.h" |
| 39 | |
| 40 | class GodotBodyContact3D : public GodotConstraint3D { |
| 41 | protected: |
| 42 | struct Contact { |
| 43 | Vector3 position; |
| 44 | Vector3 normal; |
| 45 | int index_A = 0, index_B = 0; |
| 46 | Vector3 local_A, local_B; |
| 47 | Vector3 acc_impulse; // accumulated impulse - only one of the object's impulse is needed as impulse_a == -impulse_b |
| 48 | real_t acc_normal_impulse = 0.0; // accumulated normal impulse (Pn) |
| 49 | Vector3 acc_tangent_impulse; // accumulated tangent impulse (Pt) |
| 50 | real_t acc_bias_impulse = 0.0; // accumulated normal impulse for position bias (Pnb) |
| 51 | real_t acc_bias_impulse_center_of_mass = 0.0; // accumulated normal impulse for position bias applied to com |
| 52 | real_t mass_normal = 0.0; |
| 53 | real_t bias = 0.0; |
| 54 | real_t bounce = 0.0; |
| 55 | |
| 56 | real_t depth = 0.0; |
| 57 | bool active = false; |
| 58 | bool used = false; |
| 59 | Vector3 rA, rB; // Offset in world orientation with respect to center of mass |
| 60 | }; |
| 61 | |
| 62 | Vector3 sep_axis; |
| 63 | bool collided = false; |
| 64 | bool check_ccd = false; |
| 65 | |
| 66 | GodotSpace3D *space = nullptr; |
| 67 | |
| 68 | GodotBodyContact3D(GodotBody3D **p_body_ptr = nullptr, int p_body_count = 0) : |
| 69 | GodotConstraint3D(p_body_ptr, p_body_count) { |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | class GodotBodyPair3D : public GodotBodyContact3D { |
| 74 | enum { |
| 75 | MAX_CONTACTS = 4 |
| 76 | }; |
| 77 | |
| 78 | union { |
| 79 | struct { |
| 80 | GodotBody3D *A; |
| 81 | GodotBody3D *B; |
| 82 | }; |
| 83 | |
| 84 | GodotBody3D *_arr[2] = { nullptr, nullptr }; |
| 85 | }; |
| 86 | |
| 87 | int shape_A = 0; |
| 88 | int shape_B = 0; |
| 89 | |
| 90 | bool collide_A = false; |
| 91 | bool collide_B = false; |
| 92 | |
| 93 | bool report_contacts_only = false; |
| 94 | |
| 95 | Vector3 offset_B; //use local A coordinates to avoid numerical issues on collision detection |
| 96 | |
| 97 | Contact contacts[MAX_CONTACTS]; |
| 98 | int contact_count = 0; |
| 99 | |
| 100 | static void _contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata); |
| 101 | |
| 102 | void contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal); |
| 103 | |
| 104 | void validate_contacts(); |
| 105 | bool _test_ccd(real_t p_step, GodotBody3D *p_A, int p_shape_A, const Transform3D &p_xform_A, GodotBody3D *p_B, int p_shape_B, const Transform3D &p_xform_B); |
| 106 | |
| 107 | public: |
| 108 | virtual bool setup(real_t p_step) override; |
| 109 | virtual bool pre_solve(real_t p_step) override; |
| 110 | virtual void solve(real_t p_step) override; |
| 111 | |
| 112 | GodotBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotBody3D *p_B, int p_shape_B); |
| 113 | ~GodotBodyPair3D(); |
| 114 | }; |
| 115 | |
| 116 | class GodotBodySoftBodyPair3D : public GodotBodyContact3D { |
| 117 | GodotBody3D *body = nullptr; |
| 118 | GodotSoftBody3D *soft_body = nullptr; |
| 119 | |
| 120 | int body_shape = 0; |
| 121 | |
| 122 | bool body_collides = false; |
| 123 | bool soft_body_collides = false; |
| 124 | |
| 125 | bool report_contacts_only = false; |
| 126 | |
| 127 | LocalVector<Contact> contacts; |
| 128 | |
| 129 | static void _contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata); |
| 130 | |
| 131 | void contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal); |
| 132 | |
| 133 | void validate_contacts(); |
| 134 | |
| 135 | public: |
| 136 | virtual bool setup(real_t p_step) override; |
| 137 | virtual bool pre_solve(real_t p_step) override; |
| 138 | virtual void solve(real_t p_step) override; |
| 139 | |
| 140 | virtual GodotSoftBody3D *get_soft_body_ptr(int p_index) const override { return soft_body; } |
| 141 | virtual int get_soft_body_count() const override { return 1; } |
| 142 | |
| 143 | GodotBodySoftBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotSoftBody3D *p_B); |
| 144 | ~GodotBodySoftBodyPair3D(); |
| 145 | }; |
| 146 | |
| 147 | #endif // GODOT_BODY_PAIR_3D_H |
| 148 | |