godot_body_direct_state_2d.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**************************************************************************/
  2. /* godot_body_direct_state_2d.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 "godot_body_direct_state_2d.h"
  31. #include "godot_body_2d.h"
  32. #include "godot_physics_server_2d.h"
  33. #include "godot_space_2d.h"
  34. Vector2 GodotPhysicsDirectBodyState2D::get_total_gravity() const {
  35. return body->gravity;
  36. }
  37. real_t GodotPhysicsDirectBodyState2D::get_total_angular_damp() const {
  38. return body->total_angular_damp;
  39. }
  40. real_t GodotPhysicsDirectBodyState2D::get_total_linear_damp() const {
  41. return body->total_linear_damp;
  42. }
  43. Vector2 GodotPhysicsDirectBodyState2D::get_center_of_mass() const {
  44. return body->get_center_of_mass();
  45. }
  46. Vector2 GodotPhysicsDirectBodyState2D::get_center_of_mass_local() const {
  47. return body->get_center_of_mass_local();
  48. }
  49. real_t GodotPhysicsDirectBodyState2D::get_inverse_mass() const {
  50. return body->get_inv_mass();
  51. }
  52. real_t GodotPhysicsDirectBodyState2D::get_inverse_inertia() const {
  53. return body->get_inv_inertia();
  54. }
  55. void GodotPhysicsDirectBodyState2D::set_linear_velocity(const Vector2 &p_velocity) {
  56. body->wakeup();
  57. body->set_linear_velocity(p_velocity);
  58. }
  59. Vector2 GodotPhysicsDirectBodyState2D::get_linear_velocity() const {
  60. return body->get_linear_velocity();
  61. }
  62. void GodotPhysicsDirectBodyState2D::set_angular_velocity(real_t p_velocity) {
  63. body->wakeup();
  64. body->set_angular_velocity(p_velocity);
  65. }
  66. real_t GodotPhysicsDirectBodyState2D::get_angular_velocity() const {
  67. return body->get_angular_velocity();
  68. }
  69. void GodotPhysicsDirectBodyState2D::set_transform(const Transform2D &p_transform) {
  70. body->set_state(PhysicsServer2D::BODY_STATE_TRANSFORM, p_transform);
  71. }
  72. Transform2D GodotPhysicsDirectBodyState2D::get_transform() const {
  73. return body->get_transform();
  74. }
  75. Vector2 GodotPhysicsDirectBodyState2D::get_velocity_at_local_position(const Vector2 &p_position) const {
  76. return body->get_velocity_in_local_point(p_position);
  77. }
  78. void GodotPhysicsDirectBodyState2D::apply_central_impulse(const Vector2 &p_impulse) {
  79. body->wakeup();
  80. body->apply_central_impulse(p_impulse);
  81. }
  82. void GodotPhysicsDirectBodyState2D::apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position) {
  83. body->wakeup();
  84. body->apply_impulse(p_impulse, p_position);
  85. }
  86. void GodotPhysicsDirectBodyState2D::apply_torque_impulse(real_t p_torque) {
  87. body->wakeup();
  88. body->apply_torque_impulse(p_torque);
  89. }
  90. void GodotPhysicsDirectBodyState2D::apply_central_force(const Vector2 &p_force) {
  91. body->wakeup();
  92. body->apply_central_force(p_force);
  93. }
  94. void GodotPhysicsDirectBodyState2D::apply_force(const Vector2 &p_force, const Vector2 &p_position) {
  95. body->wakeup();
  96. body->apply_force(p_force, p_position);
  97. }
  98. void GodotPhysicsDirectBodyState2D::apply_torque(real_t p_torque) {
  99. body->wakeup();
  100. body->apply_torque(p_torque);
  101. }
  102. void GodotPhysicsDirectBodyState2D::add_constant_central_force(const Vector2 &p_force) {
  103. body->wakeup();
  104. body->add_constant_central_force(p_force);
  105. }
  106. void GodotPhysicsDirectBodyState2D::add_constant_force(const Vector2 &p_force, const Vector2 &p_position) {
  107. body->wakeup();
  108. body->add_constant_force(p_force, p_position);
  109. }
  110. void GodotPhysicsDirectBodyState2D::add_constant_torque(real_t p_torque) {
  111. body->wakeup();
  112. body->add_constant_torque(p_torque);
  113. }
  114. void GodotPhysicsDirectBodyState2D::set_constant_force(const Vector2 &p_force) {
  115. if (!p_force.is_zero_approx()) {
  116. body->wakeup();
  117. }
  118. body->set_constant_force(p_force);
  119. }
  120. Vector2 GodotPhysicsDirectBodyState2D::get_constant_force() const {
  121. return body->get_constant_force();
  122. }
  123. void GodotPhysicsDirectBodyState2D::set_constant_torque(real_t p_torque) {
  124. if (!Math::is_zero_approx(p_torque)) {
  125. body->wakeup();
  126. }
  127. body->set_constant_torque(p_torque);
  128. }
  129. real_t GodotPhysicsDirectBodyState2D::get_constant_torque() const {
  130. return body->get_constant_torque();
  131. }
  132. void GodotPhysicsDirectBodyState2D::set_sleep_state(bool p_enable) {
  133. body->set_active(!p_enable);
  134. }
  135. bool GodotPhysicsDirectBodyState2D::is_sleeping() const {
  136. return !body->is_active();
  137. }
  138. int GodotPhysicsDirectBodyState2D::get_contact_count() const {
  139. return body->contact_count;
  140. }
  141. Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_position(int p_contact_idx) const {
  142. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  143. return body->contacts[p_contact_idx].local_pos;
  144. }
  145. Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_normal(int p_contact_idx) const {
  146. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  147. return body->contacts[p_contact_idx].local_normal;
  148. }
  149. int GodotPhysicsDirectBodyState2D::get_contact_local_shape(int p_contact_idx) const {
  150. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1);
  151. return body->contacts[p_contact_idx].local_shape;
  152. }
  153. Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_velocity_at_position(int p_contact_idx) const {
  154. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  155. return body->contacts[p_contact_idx].local_velocity_at_pos;
  156. }
  157. RID GodotPhysicsDirectBodyState2D::get_contact_collider(int p_contact_idx) const {
  158. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID());
  159. return body->contacts[p_contact_idx].collider;
  160. }
  161. Vector2 GodotPhysicsDirectBodyState2D::get_contact_collider_position(int p_contact_idx) const {
  162. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  163. return body->contacts[p_contact_idx].collider_pos;
  164. }
  165. ObjectID GodotPhysicsDirectBodyState2D::get_contact_collider_id(int p_contact_idx) const {
  166. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, ObjectID());
  167. return body->contacts[p_contact_idx].collider_instance_id;
  168. }
  169. int GodotPhysicsDirectBodyState2D::get_contact_collider_shape(int p_contact_idx) const {
  170. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
  171. return body->contacts[p_contact_idx].collider_shape;
  172. }
  173. Vector2 GodotPhysicsDirectBodyState2D::get_contact_collider_velocity_at_position(int p_contact_idx) const {
  174. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  175. return body->contacts[p_contact_idx].collider_velocity_at_pos;
  176. }
  177. Vector2 GodotPhysicsDirectBodyState2D::get_contact_impulse(int p_contact_idx) const {
  178. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  179. return body->contacts[p_contact_idx].impulse;
  180. }
  181. PhysicsDirectSpaceState2D *GodotPhysicsDirectBodyState2D::get_space_state() {
  182. return body->get_space()->get_direct_state();
  183. }
  184. real_t GodotPhysicsDirectBodyState2D::get_step() const {
  185. return body->get_space()->get_last_step();
  186. }