godot_body_direct_state_3d.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*************************************************************************/
  2. /* godot_body_direct_state_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "godot_body_direct_state_3d.h"
  31. #include "godot_body_3d.h"
  32. #include "godot_space_3d.h"
  33. Vector3 GodotPhysicsDirectBodyState3D::get_total_gravity() const {
  34. return body->gravity;
  35. }
  36. real_t GodotPhysicsDirectBodyState3D::get_total_angular_damp() const {
  37. return body->total_angular_damp;
  38. }
  39. real_t GodotPhysicsDirectBodyState3D::get_total_linear_damp() const {
  40. return body->total_linear_damp;
  41. }
  42. Vector3 GodotPhysicsDirectBodyState3D::get_center_of_mass() const {
  43. return body->get_center_of_mass();
  44. }
  45. Basis GodotPhysicsDirectBodyState3D::get_principal_inertia_axes() const {
  46. return body->get_principal_inertia_axes();
  47. }
  48. real_t GodotPhysicsDirectBodyState3D::get_inverse_mass() const {
  49. return body->get_inv_mass();
  50. }
  51. Vector3 GodotPhysicsDirectBodyState3D::get_inverse_inertia() const {
  52. return body->get_inv_inertia();
  53. }
  54. Basis GodotPhysicsDirectBodyState3D::get_inverse_inertia_tensor() const {
  55. return body->get_inv_inertia_tensor();
  56. }
  57. void GodotPhysicsDirectBodyState3D::set_linear_velocity(const Vector3 &p_velocity) {
  58. body->wakeup();
  59. body->set_linear_velocity(p_velocity);
  60. }
  61. Vector3 GodotPhysicsDirectBodyState3D::get_linear_velocity() const {
  62. return body->get_linear_velocity();
  63. }
  64. void GodotPhysicsDirectBodyState3D::set_angular_velocity(const Vector3 &p_velocity) {
  65. body->wakeup();
  66. body->set_angular_velocity(p_velocity);
  67. }
  68. Vector3 GodotPhysicsDirectBodyState3D::get_angular_velocity() const {
  69. return body->get_angular_velocity();
  70. }
  71. void GodotPhysicsDirectBodyState3D::set_transform(const Transform3D &p_transform) {
  72. body->set_state(PhysicsServer3D::BODY_STATE_TRANSFORM, p_transform);
  73. }
  74. Transform3D GodotPhysicsDirectBodyState3D::get_transform() const {
  75. return body->get_transform();
  76. }
  77. Vector3 GodotPhysicsDirectBodyState3D::get_velocity_at_local_position(const Vector3 &p_position) const {
  78. return body->get_velocity_in_local_point(p_position);
  79. }
  80. void GodotPhysicsDirectBodyState3D::add_central_force(const Vector3 &p_force) {
  81. body->wakeup();
  82. body->add_central_force(p_force);
  83. }
  84. void GodotPhysicsDirectBodyState3D::add_force(const Vector3 &p_force, const Vector3 &p_position) {
  85. body->wakeup();
  86. body->add_force(p_force, p_position);
  87. }
  88. void GodotPhysicsDirectBodyState3D::add_torque(const Vector3 &p_torque) {
  89. body->wakeup();
  90. body->add_torque(p_torque);
  91. }
  92. void GodotPhysicsDirectBodyState3D::apply_central_impulse(const Vector3 &p_impulse) {
  93. body->wakeup();
  94. body->apply_central_impulse(p_impulse);
  95. }
  96. void GodotPhysicsDirectBodyState3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
  97. body->wakeup();
  98. body->apply_impulse(p_impulse, p_position);
  99. }
  100. void GodotPhysicsDirectBodyState3D::apply_torque_impulse(const Vector3 &p_impulse) {
  101. body->wakeup();
  102. body->apply_torque_impulse(p_impulse);
  103. }
  104. void GodotPhysicsDirectBodyState3D::set_sleep_state(bool p_sleep) {
  105. body->set_active(!p_sleep);
  106. }
  107. bool GodotPhysicsDirectBodyState3D::is_sleeping() const {
  108. return !body->is_active();
  109. }
  110. int GodotPhysicsDirectBodyState3D::get_contact_count() const {
  111. return body->contact_count;
  112. }
  113. Vector3 GodotPhysicsDirectBodyState3D::get_contact_local_position(int p_contact_idx) const {
  114. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
  115. return body->contacts[p_contact_idx].local_pos;
  116. }
  117. Vector3 GodotPhysicsDirectBodyState3D::get_contact_local_normal(int p_contact_idx) const {
  118. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
  119. return body->contacts[p_contact_idx].local_normal;
  120. }
  121. real_t GodotPhysicsDirectBodyState3D::get_contact_impulse(int p_contact_idx) const {
  122. return 0.0f; // Only implemented for bullet
  123. }
  124. int GodotPhysicsDirectBodyState3D::get_contact_local_shape(int p_contact_idx) const {
  125. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1);
  126. return body->contacts[p_contact_idx].local_shape;
  127. }
  128. RID GodotPhysicsDirectBodyState3D::get_contact_collider(int p_contact_idx) const {
  129. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID());
  130. return body->contacts[p_contact_idx].collider;
  131. }
  132. Vector3 GodotPhysicsDirectBodyState3D::get_contact_collider_position(int p_contact_idx) const {
  133. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
  134. return body->contacts[p_contact_idx].collider_pos;
  135. }
  136. ObjectID GodotPhysicsDirectBodyState3D::get_contact_collider_id(int p_contact_idx) const {
  137. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, ObjectID());
  138. return body->contacts[p_contact_idx].collider_instance_id;
  139. }
  140. int GodotPhysicsDirectBodyState3D::get_contact_collider_shape(int p_contact_idx) const {
  141. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
  142. return body->contacts[p_contact_idx].collider_shape;
  143. }
  144. Vector3 GodotPhysicsDirectBodyState3D::get_contact_collider_velocity_at_position(int p_contact_idx) const {
  145. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
  146. return body->contacts[p_contact_idx].collider_velocity_at_pos;
  147. }
  148. PhysicsDirectSpaceState3D *GodotPhysicsDirectBodyState3D::get_space_state() {
  149. return body->get_space()->get_direct_state();
  150. }
  151. real_t GodotPhysicsDirectBodyState3D::get_step() const {
  152. return body->get_space()->get_last_step();
  153. }