godot_body_direct_state_2d.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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) 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_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. real_t GodotPhysicsDirectBodyState2D::get_inverse_mass() const {
  47. return body->get_inv_mass();
  48. }
  49. real_t GodotPhysicsDirectBodyState2D::get_inverse_inertia() const {
  50. return body->get_inv_inertia();
  51. }
  52. void GodotPhysicsDirectBodyState2D::set_linear_velocity(const Vector2 &p_velocity) {
  53. body->wakeup();
  54. body->set_linear_velocity(p_velocity);
  55. }
  56. Vector2 GodotPhysicsDirectBodyState2D::get_linear_velocity() const {
  57. return body->get_linear_velocity();
  58. }
  59. void GodotPhysicsDirectBodyState2D::set_angular_velocity(real_t p_velocity) {
  60. body->wakeup();
  61. body->set_angular_velocity(p_velocity);
  62. }
  63. real_t GodotPhysicsDirectBodyState2D::get_angular_velocity() const {
  64. return body->get_angular_velocity();
  65. }
  66. void GodotPhysicsDirectBodyState2D::set_transform(const Transform2D &p_transform) {
  67. body->set_state(PhysicsServer2D::BODY_STATE_TRANSFORM, p_transform);
  68. }
  69. Transform2D GodotPhysicsDirectBodyState2D::get_transform() const {
  70. return body->get_transform();
  71. }
  72. Vector2 GodotPhysicsDirectBodyState2D::get_velocity_at_local_position(const Vector2 &p_position) const {
  73. return body->get_velocity_in_local_point(p_position);
  74. }
  75. void GodotPhysicsDirectBodyState2D::add_central_force(const Vector2 &p_force) {
  76. body->wakeup();
  77. body->add_central_force(p_force);
  78. }
  79. void GodotPhysicsDirectBodyState2D::add_force(const Vector2 &p_force, const Vector2 &p_position) {
  80. body->wakeup();
  81. body->add_force(p_force, p_position);
  82. }
  83. void GodotPhysicsDirectBodyState2D::add_torque(real_t p_torque) {
  84. body->wakeup();
  85. body->add_torque(p_torque);
  86. }
  87. void GodotPhysicsDirectBodyState2D::apply_central_impulse(const Vector2 &p_impulse) {
  88. body->wakeup();
  89. body->apply_central_impulse(p_impulse);
  90. }
  91. void GodotPhysicsDirectBodyState2D::apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position) {
  92. body->wakeup();
  93. body->apply_impulse(p_impulse, p_position);
  94. }
  95. void GodotPhysicsDirectBodyState2D::apply_torque_impulse(real_t p_torque) {
  96. body->wakeup();
  97. body->apply_torque_impulse(p_torque);
  98. }
  99. void GodotPhysicsDirectBodyState2D::set_sleep_state(bool p_enable) {
  100. body->set_active(!p_enable);
  101. }
  102. bool GodotPhysicsDirectBodyState2D::is_sleeping() const {
  103. return !body->is_active();
  104. }
  105. int GodotPhysicsDirectBodyState2D::get_contact_count() const {
  106. return body->contact_count;
  107. }
  108. Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_position(int p_contact_idx) const {
  109. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  110. return body->contacts[p_contact_idx].local_pos;
  111. }
  112. Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_normal(int p_contact_idx) const {
  113. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  114. return body->contacts[p_contact_idx].local_normal;
  115. }
  116. int GodotPhysicsDirectBodyState2D::get_contact_local_shape(int p_contact_idx) const {
  117. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1);
  118. return body->contacts[p_contact_idx].local_shape;
  119. }
  120. RID GodotPhysicsDirectBodyState2D::get_contact_collider(int p_contact_idx) const {
  121. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID());
  122. return body->contacts[p_contact_idx].collider;
  123. }
  124. Vector2 GodotPhysicsDirectBodyState2D::get_contact_collider_position(int p_contact_idx) const {
  125. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  126. return body->contacts[p_contact_idx].collider_pos;
  127. }
  128. ObjectID GodotPhysicsDirectBodyState2D::get_contact_collider_id(int p_contact_idx) const {
  129. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, ObjectID());
  130. return body->contacts[p_contact_idx].collider_instance_id;
  131. }
  132. int GodotPhysicsDirectBodyState2D::get_contact_collider_shape(int p_contact_idx) const {
  133. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
  134. return body->contacts[p_contact_idx].collider_shape;
  135. }
  136. Vector2 GodotPhysicsDirectBodyState2D::get_contact_collider_velocity_at_position(int p_contact_idx) const {
  137. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  138. return body->contacts[p_contact_idx].collider_velocity_at_pos;
  139. }
  140. PhysicsDirectSpaceState2D *GodotPhysicsDirectBodyState2D::get_space_state() {
  141. return body->get_space()->get_direct_state();
  142. }
  143. real_t GodotPhysicsDirectBodyState2D::get_step() const {
  144. return body->get_space()->get_last_step();
  145. }