godot_body_pair_3d.h 5.6 KB

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