jolt_physics_direct_body_state_3d.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**************************************************************************/
  2. /* jolt_physics_direct_body_state_3d.cpp */
  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. #include "jolt_physics_direct_body_state_3d.h"
  31. #include "../spaces/jolt_physics_direct_space_state_3d.h"
  32. #include "../spaces/jolt_space_3d.h"
  33. #include "jolt_body_3d.h"
  34. JoltPhysicsDirectBodyState3D::JoltPhysicsDirectBodyState3D(JoltBody3D *p_body) :
  35. body(p_body) {
  36. }
  37. Vector3 JoltPhysicsDirectBodyState3D::get_total_gravity() const {
  38. return body->get_gravity();
  39. }
  40. real_t JoltPhysicsDirectBodyState3D::get_total_angular_damp() const {
  41. return (real_t)body->get_total_angular_damp();
  42. }
  43. real_t JoltPhysicsDirectBodyState3D::get_total_linear_damp() const {
  44. return (real_t)body->get_total_linear_damp();
  45. }
  46. Vector3 JoltPhysicsDirectBodyState3D::get_center_of_mass() const {
  47. return body->get_center_of_mass_relative();
  48. }
  49. Vector3 JoltPhysicsDirectBodyState3D::get_center_of_mass_local() const {
  50. return body->get_center_of_mass_local();
  51. }
  52. Basis JoltPhysicsDirectBodyState3D::get_principal_inertia_axes() const {
  53. return body->get_principal_inertia_axes();
  54. }
  55. real_t JoltPhysicsDirectBodyState3D::get_inverse_mass() const {
  56. return 1.0 / body->get_mass();
  57. }
  58. Vector3 JoltPhysicsDirectBodyState3D::get_inverse_inertia() const {
  59. return body->get_inverse_inertia();
  60. }
  61. Basis JoltPhysicsDirectBodyState3D::get_inverse_inertia_tensor() const {
  62. return body->get_inverse_inertia_tensor();
  63. }
  64. Vector3 JoltPhysicsDirectBodyState3D::get_linear_velocity() const {
  65. return body->get_linear_velocity();
  66. }
  67. void JoltPhysicsDirectBodyState3D::set_linear_velocity(const Vector3 &p_velocity) {
  68. return body->set_linear_velocity(p_velocity);
  69. }
  70. Vector3 JoltPhysicsDirectBodyState3D::get_angular_velocity() const {
  71. return body->get_angular_velocity();
  72. }
  73. void JoltPhysicsDirectBodyState3D::set_angular_velocity(const Vector3 &p_velocity) {
  74. return body->set_angular_velocity(p_velocity);
  75. }
  76. void JoltPhysicsDirectBodyState3D::set_transform(const Transform3D &p_transform) {
  77. return body->set_transform(p_transform);
  78. }
  79. Transform3D JoltPhysicsDirectBodyState3D::get_transform() const {
  80. return body->get_transform_scaled();
  81. }
  82. Vector3 JoltPhysicsDirectBodyState3D::get_velocity_at_local_position(const Vector3 &p_local_position) const {
  83. return body->get_velocity_at_position(body->get_position() + p_local_position);
  84. }
  85. void JoltPhysicsDirectBodyState3D::apply_central_impulse(const Vector3 &p_impulse) {
  86. return body->apply_central_impulse(p_impulse);
  87. }
  88. void JoltPhysicsDirectBodyState3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
  89. return body->apply_impulse(p_impulse, p_position);
  90. }
  91. void JoltPhysicsDirectBodyState3D::apply_torque_impulse(const Vector3 &p_impulse) {
  92. return body->apply_torque_impulse(p_impulse);
  93. }
  94. void JoltPhysicsDirectBodyState3D::apply_central_force(const Vector3 &p_force) {
  95. return body->apply_central_force(p_force);
  96. }
  97. void JoltPhysicsDirectBodyState3D::apply_force(const Vector3 &p_force, const Vector3 &p_position) {
  98. return body->apply_force(p_force, p_position);
  99. }
  100. void JoltPhysicsDirectBodyState3D::apply_torque(const Vector3 &p_torque) {
  101. return body->apply_torque(p_torque);
  102. }
  103. void JoltPhysicsDirectBodyState3D::add_constant_central_force(const Vector3 &p_force) {
  104. return body->add_constant_central_force(p_force);
  105. }
  106. void JoltPhysicsDirectBodyState3D::add_constant_force(const Vector3 &p_force, const Vector3 &p_position) {
  107. return body->add_constant_force(p_force, p_position);
  108. }
  109. void JoltPhysicsDirectBodyState3D::add_constant_torque(const Vector3 &p_torque) {
  110. return body->add_constant_torque(p_torque);
  111. }
  112. Vector3 JoltPhysicsDirectBodyState3D::get_constant_force() const {
  113. return body->get_constant_force();
  114. }
  115. void JoltPhysicsDirectBodyState3D::set_constant_force(const Vector3 &p_force) {
  116. return body->set_constant_force(p_force);
  117. }
  118. Vector3 JoltPhysicsDirectBodyState3D::get_constant_torque() const {
  119. return body->get_constant_torque();
  120. }
  121. void JoltPhysicsDirectBodyState3D::set_constant_torque(const Vector3 &p_torque) {
  122. return body->set_constant_torque(p_torque);
  123. }
  124. bool JoltPhysicsDirectBodyState3D::is_sleeping() const {
  125. return body->is_sleeping();
  126. }
  127. void JoltPhysicsDirectBodyState3D::set_sleep_state(bool p_enabled) {
  128. body->set_is_sleeping(p_enabled);
  129. }
  130. int JoltPhysicsDirectBodyState3D::get_contact_count() const {
  131. return body->get_contact_count();
  132. }
  133. Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_position(int p_contact_idx) const {
  134. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
  135. return body->get_contact(p_contact_idx).position;
  136. }
  137. Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_normal(int p_contact_idx) const {
  138. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
  139. return body->get_contact(p_contact_idx).normal;
  140. }
  141. Vector3 JoltPhysicsDirectBodyState3D::get_contact_impulse(int p_contact_idx) const {
  142. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
  143. return body->get_contact(p_contact_idx).impulse;
  144. }
  145. int JoltPhysicsDirectBodyState3D::get_contact_local_shape(int p_contact_idx) const {
  146. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), 0);
  147. return body->get_contact(p_contact_idx).shape_index;
  148. }
  149. Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_velocity_at_position(int p_contact_idx) const {
  150. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
  151. return body->get_contact(p_contact_idx).velocity;
  152. }
  153. RID JoltPhysicsDirectBodyState3D::get_contact_collider(int p_contact_idx) const {
  154. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), RID());
  155. return body->get_contact(p_contact_idx).collider_rid;
  156. }
  157. Vector3 JoltPhysicsDirectBodyState3D::get_contact_collider_position(int p_contact_idx) const {
  158. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
  159. return body->get_contact(p_contact_idx).collider_position;
  160. }
  161. ObjectID JoltPhysicsDirectBodyState3D::get_contact_collider_id(int p_contact_idx) const {
  162. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), ObjectID());
  163. return body->get_contact(p_contact_idx).collider_id;
  164. }
  165. Object *JoltPhysicsDirectBodyState3D::get_contact_collider_object(int p_contact_idx) const {
  166. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), nullptr);
  167. return ObjectDB::get_instance(body->get_contact(p_contact_idx).collider_id);
  168. }
  169. int JoltPhysicsDirectBodyState3D::get_contact_collider_shape(int p_contact_idx) const {
  170. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), 0);
  171. return body->get_contact(p_contact_idx).collider_shape_index;
  172. }
  173. Vector3 JoltPhysicsDirectBodyState3D::get_contact_collider_velocity_at_position(int p_contact_idx) const {
  174. ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
  175. return body->get_contact(p_contact_idx).collider_velocity;
  176. }
  177. real_t JoltPhysicsDirectBodyState3D::get_step() const {
  178. return (real_t)body->get_space()->get_last_step();
  179. }
  180. void JoltPhysicsDirectBodyState3D::integrate_forces() {
  181. const float step = (float)get_step();
  182. Vector3 linear_velocity = get_linear_velocity();
  183. Vector3 angular_velocity = get_angular_velocity();
  184. linear_velocity *= MAX(1.0f - (float)get_total_linear_damp() * step, 0.0f);
  185. angular_velocity *= MAX(1.0f - (float)get_total_angular_damp() * step, 0.0f);
  186. linear_velocity += get_total_gravity() * step;
  187. set_linear_velocity(linear_velocity);
  188. set_angular_velocity(angular_velocity);
  189. }
  190. PhysicsDirectSpaceState3D *JoltPhysicsDirectBodyState3D::get_space_state() {
  191. return body->get_space()->get_direct_state();
  192. }