godot_body_2d.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**************************************************************************/
  2. /* godot_body_2d.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. #ifndef GODOT_BODY_2D_H
  31. #define GODOT_BODY_2D_H
  32. #include "godot_area_2d.h"
  33. #include "godot_collision_object_2d.h"
  34. #include "core/templates/list.h"
  35. #include "core/templates/pair.h"
  36. #include "core/templates/vset.h"
  37. class GodotConstraint2D;
  38. class GodotPhysicsDirectBodyState2D;
  39. class GodotBody2D : public GodotCollisionObject2D {
  40. PhysicsServer2D::BodyMode mode = PhysicsServer2D::BODY_MODE_RIGID;
  41. Vector2 biased_linear_velocity;
  42. real_t biased_angular_velocity = 0.0;
  43. Vector2 linear_velocity;
  44. real_t angular_velocity = 0.0;
  45. Vector2 prev_linear_velocity;
  46. real_t prev_angular_velocity = 0.0;
  47. Vector2 constant_linear_velocity;
  48. real_t constant_angular_velocity = 0.0;
  49. PhysicsServer2D::BodyDampMode linear_damp_mode = PhysicsServer2D::BODY_DAMP_MODE_COMBINE;
  50. PhysicsServer2D::BodyDampMode angular_damp_mode = PhysicsServer2D::BODY_DAMP_MODE_COMBINE;
  51. real_t linear_damp = 0.0;
  52. real_t angular_damp = 0.0;
  53. real_t total_linear_damp = 0.0;
  54. real_t total_angular_damp = 0.0;
  55. real_t gravity_scale = 1.0;
  56. real_t bounce = 0.0;
  57. real_t friction = 1.0;
  58. real_t mass = 1.0;
  59. real_t _inv_mass = 1.0;
  60. real_t inertia = 0.0;
  61. real_t _inv_inertia = 0.0;
  62. Vector2 center_of_mass_local;
  63. Vector2 center_of_mass;
  64. bool calculate_inertia = true;
  65. bool calculate_center_of_mass = true;
  66. Vector2 gravity;
  67. real_t still_time = 0.0;
  68. Vector2 applied_force;
  69. real_t applied_torque = 0.0;
  70. Vector2 constant_force;
  71. real_t constant_torque = 0.0;
  72. SelfList<GodotBody2D> active_list;
  73. SelfList<GodotBody2D> mass_properties_update_list;
  74. SelfList<GodotBody2D> direct_state_query_list;
  75. VSet<RID> exceptions;
  76. PhysicsServer2D::CCDMode continuous_cd_mode = PhysicsServer2D::CCD_MODE_DISABLED;
  77. bool omit_force_integration = false;
  78. bool active = true;
  79. bool can_sleep = true;
  80. bool first_time_kinematic = false;
  81. void _mass_properties_changed();
  82. virtual void _shapes_changed() override;
  83. Transform2D new_transform;
  84. List<Pair<GodotConstraint2D *, int>> constraint_list;
  85. struct AreaCMP {
  86. GodotArea2D *area = nullptr;
  87. int refCount = 0;
  88. _FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }
  89. _FORCE_INLINE_ bool operator<(const AreaCMP &p_cmp) const { return area->get_priority() < p_cmp.area->get_priority(); }
  90. _FORCE_INLINE_ AreaCMP() {}
  91. _FORCE_INLINE_ AreaCMP(GodotArea2D *p_area) {
  92. area = p_area;
  93. refCount = 1;
  94. }
  95. };
  96. Vector<AreaCMP> areas;
  97. struct Contact {
  98. Vector2 local_pos;
  99. Vector2 local_normal;
  100. Vector2 local_velocity_at_pos;
  101. real_t depth = 0.0;
  102. int local_shape = 0;
  103. Vector2 collider_pos;
  104. int collider_shape = 0;
  105. ObjectID collider_instance_id;
  106. RID collider;
  107. Vector2 collider_velocity_at_pos;
  108. Vector2 impulse;
  109. };
  110. Vector<Contact> contacts; //no contacts by default
  111. int contact_count = 0;
  112. Callable body_state_callback;
  113. struct ForceIntegrationCallbackData {
  114. Callable callable;
  115. Variant udata;
  116. };
  117. ForceIntegrationCallbackData *fi_callback_data = nullptr;
  118. GodotPhysicsDirectBodyState2D *direct_state = nullptr;
  119. uint64_t island_step = 0;
  120. void _update_transform_dependent();
  121. friend class GodotPhysicsDirectBodyState2D; // i give up, too many functions to expose
  122. public:
  123. void set_state_sync_callback(const Callable &p_callable);
  124. void set_force_integration_callback(const Callable &p_callable, const Variant &p_udata = Variant());
  125. GodotPhysicsDirectBodyState2D *get_direct_state();
  126. _FORCE_INLINE_ void add_area(GodotArea2D *p_area) {
  127. int index = areas.find(AreaCMP(p_area));
  128. if (index > -1) {
  129. areas.write[index].refCount += 1;
  130. } else {
  131. areas.ordered_insert(AreaCMP(p_area));
  132. }
  133. }
  134. _FORCE_INLINE_ void remove_area(GodotArea2D *p_area) {
  135. int index = areas.find(AreaCMP(p_area));
  136. if (index > -1) {
  137. areas.write[index].refCount -= 1;
  138. if (areas[index].refCount < 1) {
  139. areas.remove_at(index);
  140. }
  141. }
  142. }
  143. _FORCE_INLINE_ void set_max_contacts_reported(int p_size) {
  144. contacts.resize(p_size);
  145. contact_count = 0;
  146. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && p_size) {
  147. set_active(true);
  148. }
  149. }
  150. _FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
  151. _FORCE_INLINE_ bool can_report_contacts() const { return !contacts.is_empty(); }
  152. _FORCE_INLINE_ void add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_normal, real_t p_depth, int p_local_shape, const Vector2 &p_local_velocity_at_pos, const Vector2 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector2 &p_collider_velocity_at_pos, const Vector2 &p_impulse);
  153. _FORCE_INLINE_ void add_exception(const RID &p_exception) { exceptions.insert(p_exception); }
  154. _FORCE_INLINE_ void remove_exception(const RID &p_exception) { exceptions.erase(p_exception); }
  155. _FORCE_INLINE_ bool has_exception(const RID &p_exception) const { return exceptions.has(p_exception); }
  156. _FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
  157. _FORCE_INLINE_ uint64_t get_island_step() const { return island_step; }
  158. _FORCE_INLINE_ void set_island_step(uint64_t p_step) { island_step = p_step; }
  159. _FORCE_INLINE_ void add_constraint(GodotConstraint2D *p_constraint, int p_pos) { constraint_list.push_back({ p_constraint, p_pos }); }
  160. _FORCE_INLINE_ void remove_constraint(GodotConstraint2D *p_constraint, int p_pos) { constraint_list.erase({ p_constraint, p_pos }); }
  161. const List<Pair<GodotConstraint2D *, int>> &get_constraint_list() const { return constraint_list; }
  162. _FORCE_INLINE_ void clear_constraint_list() { constraint_list.clear(); }
  163. _FORCE_INLINE_ void set_omit_force_integration(bool p_omit_force_integration) { omit_force_integration = p_omit_force_integration; }
  164. _FORCE_INLINE_ bool get_omit_force_integration() const { return omit_force_integration; }
  165. _FORCE_INLINE_ void set_linear_velocity(const Vector2 &p_velocity) { linear_velocity = p_velocity; }
  166. _FORCE_INLINE_ Vector2 get_linear_velocity() const { return linear_velocity; }
  167. _FORCE_INLINE_ void set_angular_velocity(real_t p_velocity) { angular_velocity = p_velocity; }
  168. _FORCE_INLINE_ real_t get_angular_velocity() const { return angular_velocity; }
  169. _FORCE_INLINE_ Vector2 get_prev_linear_velocity() const { return prev_linear_velocity; }
  170. _FORCE_INLINE_ real_t get_prev_angular_velocity() const { return prev_angular_velocity; }
  171. _FORCE_INLINE_ void set_biased_linear_velocity(const Vector2 &p_velocity) { biased_linear_velocity = p_velocity; }
  172. _FORCE_INLINE_ Vector2 get_biased_linear_velocity() const { return biased_linear_velocity; }
  173. _FORCE_INLINE_ void set_biased_angular_velocity(real_t p_velocity) { biased_angular_velocity = p_velocity; }
  174. _FORCE_INLINE_ real_t get_biased_angular_velocity() const { return biased_angular_velocity; }
  175. _FORCE_INLINE_ void apply_central_impulse(const Vector2 &p_impulse) {
  176. linear_velocity += p_impulse * _inv_mass;
  177. }
  178. _FORCE_INLINE_ void apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) {
  179. linear_velocity += p_impulse * _inv_mass;
  180. angular_velocity += _inv_inertia * (p_position - center_of_mass).cross(p_impulse);
  181. }
  182. _FORCE_INLINE_ void apply_torque_impulse(real_t p_torque) {
  183. angular_velocity += _inv_inertia * p_torque;
  184. }
  185. _FORCE_INLINE_ void apply_bias_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2(), real_t p_max_delta_av = -1.0) {
  186. biased_linear_velocity += p_impulse * _inv_mass;
  187. if (p_max_delta_av != 0.0) {
  188. real_t delta_av = _inv_inertia * (p_position - center_of_mass).cross(p_impulse);
  189. if (p_max_delta_av > 0 && delta_av > p_max_delta_av) {
  190. delta_av = p_max_delta_av;
  191. }
  192. biased_angular_velocity += delta_av;
  193. }
  194. }
  195. _FORCE_INLINE_ void apply_central_force(const Vector2 &p_force) {
  196. applied_force += p_force;
  197. }
  198. _FORCE_INLINE_ void apply_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) {
  199. applied_force += p_force;
  200. applied_torque += (p_position - center_of_mass).cross(p_force);
  201. }
  202. _FORCE_INLINE_ void apply_torque(real_t p_torque) {
  203. applied_torque += p_torque;
  204. }
  205. _FORCE_INLINE_ void add_constant_central_force(const Vector2 &p_force) {
  206. constant_force += p_force;
  207. }
  208. _FORCE_INLINE_ void add_constant_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) {
  209. constant_force += p_force;
  210. constant_torque += (p_position - center_of_mass).cross(p_force);
  211. }
  212. _FORCE_INLINE_ void add_constant_torque(real_t p_torque) {
  213. constant_torque += p_torque;
  214. }
  215. void set_constant_force(const Vector2 &p_force) { constant_force = p_force; }
  216. Vector2 get_constant_force() const { return constant_force; }
  217. void set_constant_torque(real_t p_torque) { constant_torque = p_torque; }
  218. real_t get_constant_torque() const { return constant_torque; }
  219. void set_active(bool p_active);
  220. _FORCE_INLINE_ bool is_active() const { return active; }
  221. _FORCE_INLINE_ void wakeup() {
  222. if ((!get_space()) || mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  223. return;
  224. }
  225. set_active(true);
  226. }
  227. void set_param(PhysicsServer2D::BodyParameter p_param, const Variant &p_value);
  228. Variant get_param(PhysicsServer2D::BodyParameter p_param) const;
  229. void set_mode(PhysicsServer2D::BodyMode p_mode);
  230. PhysicsServer2D::BodyMode get_mode() const;
  231. void set_state(PhysicsServer2D::BodyState p_state, const Variant &p_variant);
  232. Variant get_state(PhysicsServer2D::BodyState p_state) const;
  233. _FORCE_INLINE_ void set_continuous_collision_detection_mode(PhysicsServer2D::CCDMode p_mode) { continuous_cd_mode = p_mode; }
  234. _FORCE_INLINE_ PhysicsServer2D::CCDMode get_continuous_collision_detection_mode() const { return continuous_cd_mode; }
  235. void set_space(GodotSpace2D *p_space) override;
  236. void update_mass_properties();
  237. void reset_mass_properties();
  238. _FORCE_INLINE_ const Vector2 &get_center_of_mass() const { return center_of_mass; }
  239. _FORCE_INLINE_ const Vector2 &get_center_of_mass_local() const { return center_of_mass_local; }
  240. _FORCE_INLINE_ real_t get_inv_mass() const { return _inv_mass; }
  241. _FORCE_INLINE_ real_t get_inv_inertia() const { return _inv_inertia; }
  242. _FORCE_INLINE_ real_t get_friction() const { return friction; }
  243. _FORCE_INLINE_ real_t get_bounce() const { return bounce; }
  244. void integrate_forces(real_t p_step);
  245. void integrate_velocities(real_t p_step);
  246. _FORCE_INLINE_ Vector2 get_velocity_in_local_point(const Vector2 &rel_pos) const {
  247. return linear_velocity + Vector2(-angular_velocity * rel_pos.y, angular_velocity * rel_pos.x);
  248. }
  249. _FORCE_INLINE_ Vector2 get_motion() const {
  250. if (mode > PhysicsServer2D::BODY_MODE_KINEMATIC) {
  251. return new_transform.get_origin() - get_transform().get_origin();
  252. } else if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  253. return get_transform().get_origin() - new_transform.get_origin(); //kinematic simulates forward
  254. }
  255. return Vector2();
  256. }
  257. void call_queries();
  258. void wakeup_neighbours();
  259. bool sleep_test(real_t p_step);
  260. GodotBody2D();
  261. ~GodotBody2D();
  262. };
  263. //add contact inline
  264. void GodotBody2D::add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_normal, real_t p_depth, int p_local_shape, const Vector2 &p_local_velocity_at_pos, const Vector2 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector2 &p_collider_velocity_at_pos, const Vector2 &p_impulse) {
  265. int c_max = contacts.size();
  266. if (c_max == 0) {
  267. return;
  268. }
  269. Contact *c = contacts.ptrw();
  270. int idx = -1;
  271. if (contact_count < c_max) {
  272. idx = contact_count++;
  273. } else {
  274. real_t least_depth = 1e20;
  275. int least_deep = -1;
  276. for (int i = 0; i < c_max; i++) {
  277. if (i == 0 || c[i].depth < least_depth) {
  278. least_deep = i;
  279. least_depth = c[i].depth;
  280. }
  281. }
  282. if (least_deep >= 0 && least_depth < p_depth) {
  283. idx = least_deep;
  284. }
  285. if (idx == -1) {
  286. return; //none least deepe than this
  287. }
  288. }
  289. c[idx].local_pos = p_local_pos;
  290. c[idx].local_normal = p_local_normal;
  291. c[idx].local_velocity_at_pos = p_local_velocity_at_pos;
  292. c[idx].depth = p_depth;
  293. c[idx].local_shape = p_local_shape;
  294. c[idx].collider_pos = p_collider_pos;
  295. c[idx].collider_shape = p_collider_shape;
  296. c[idx].collider_instance_id = p_collider_instance_id;
  297. c[idx].collider = p_collider;
  298. c[idx].collider_velocity_at_pos = p_collider_velocity_at_pos;
  299. c[idx].impulse = p_impulse;
  300. }
  301. #endif // GODOT_BODY_2D_H