| 1 | /**************************************************************************/ |
| 2 | /* godot_body_pair_2d.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_2D_H |
| 32 | #define GODOT_BODY_PAIR_2D_H |
| 33 | |
| 34 | #include "godot_body_2d.h" |
| 35 | #include "godot_constraint_2d.h" |
| 36 | |
| 37 | class GodotBodyPair2D : public GodotConstraint2D { |
| 38 | enum { |
| 39 | MAX_CONTACTS = 2 |
| 40 | }; |
| 41 | union { |
| 42 | struct { |
| 43 | GodotBody2D *A; |
| 44 | GodotBody2D *B; |
| 45 | }; |
| 46 | |
| 47 | GodotBody2D *_arr[2] = { nullptr, nullptr }; |
| 48 | }; |
| 49 | |
| 50 | int shape_A = 0; |
| 51 | int shape_B = 0; |
| 52 | |
| 53 | bool collide_A = false; |
| 54 | bool collide_B = false; |
| 55 | |
| 56 | GodotSpace2D *space = nullptr; |
| 57 | |
| 58 | struct Contact { |
| 59 | Vector2 position; |
| 60 | Vector2 normal; |
| 61 | Vector2 local_A, local_B; |
| 62 | Vector2 acc_impulse; // accumulated impulse |
| 63 | real_t acc_normal_impulse = 0.0; // accumulated normal impulse (Pn) |
| 64 | real_t acc_tangent_impulse = 0.0; // accumulated tangent impulse (Pt) |
| 65 | real_t acc_bias_impulse = 0.0; // accumulated normal impulse for position bias (Pnb) |
| 66 | real_t acc_bias_impulse_center_of_mass = 0.0; // accumulated normal impulse for position bias applied to com |
| 67 | real_t mass_normal, mass_tangent = 0.0; |
| 68 | real_t bias = 0.0; |
| 69 | |
| 70 | real_t depth = 0.0; |
| 71 | bool active = false; |
| 72 | bool used = false; |
| 73 | Vector2 rA, rB; |
| 74 | real_t bounce = 0.0; |
| 75 | }; |
| 76 | |
| 77 | Vector2 offset_B; //use local A coordinates to avoid numerical issues on collision detection |
| 78 | |
| 79 | Vector2 sep_axis; |
| 80 | Contact contacts[MAX_CONTACTS]; |
| 81 | int contact_count = 0; |
| 82 | bool collided = false; |
| 83 | bool check_ccd = false; |
| 84 | bool oneway_disabled = false; |
| 85 | bool report_contacts_only = false; |
| 86 | |
| 87 | bool _test_ccd(real_t p_step, GodotBody2D *p_A, int p_shape_A, const Transform2D &p_xform_A, GodotBody2D *p_B, int p_shape_B, const Transform2D &p_xform_B); |
| 88 | void _validate_contacts(); |
| 89 | static void _add_contact(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_self); |
| 90 | _FORCE_INLINE_ void _contact_added_callback(const Vector2 &p_point_A, const Vector2 &p_point_B); |
| 91 | |
| 92 | public: |
| 93 | virtual bool setup(real_t p_step) override; |
| 94 | virtual bool pre_solve(real_t p_step) override; |
| 95 | virtual void solve(real_t p_step) override; |
| 96 | |
| 97 | GodotBodyPair2D(GodotBody2D *p_A, int p_shape_A, GodotBody2D *p_B, int p_shape_B); |
| 98 | ~GodotBodyPair2D(); |
| 99 | }; |
| 100 | |
| 101 | #endif // GODOT_BODY_PAIR_2D_H |
| 102 | |