godot_body_pair_3d.h 5.7 KB

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