godot_body_pair_2d.h 4.2 KB

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