body_2d_sw.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**************************************************************************/
  2. /* body_2d_sw.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 BODY_2D_SW_H
  31. #define BODY_2D_SW_H
  32. #include "area_2d_sw.h"
  33. #include "collision_object_2d_sw.h"
  34. #include "core/vset.h"
  35. class Constraint2DSW;
  36. class Physics2DDirectBodyStateSW;
  37. class Body2DSW : public CollisionObject2DSW {
  38. Physics2DServer::BodyMode mode;
  39. Vector2 biased_linear_velocity;
  40. real_t biased_angular_velocity;
  41. Vector2 linear_velocity;
  42. real_t angular_velocity;
  43. real_t linear_damp;
  44. real_t angular_damp;
  45. real_t gravity_scale;
  46. real_t mass;
  47. real_t inertia;
  48. real_t bounce;
  49. real_t friction;
  50. real_t _inv_mass;
  51. real_t _inv_inertia;
  52. bool user_inertia;
  53. Vector2 gravity;
  54. real_t area_linear_damp;
  55. real_t area_angular_damp;
  56. real_t still_time;
  57. Vector2 applied_force;
  58. real_t applied_torque;
  59. SelfList<Body2DSW> active_list;
  60. SelfList<Body2DSW> inertia_update_list;
  61. SelfList<Body2DSW> direct_state_query_list;
  62. VSet<RID> exceptions;
  63. Physics2DServer::CCDMode continuous_cd_mode;
  64. bool omit_force_integration;
  65. bool active;
  66. bool can_sleep;
  67. bool first_time_kinematic;
  68. bool first_integration;
  69. void _update_inertia();
  70. virtual void _shapes_changed();
  71. Transform2D new_transform;
  72. Map<Constraint2DSW *, int> constraint_map;
  73. struct AreaCMP {
  74. Area2DSW *area;
  75. int refCount;
  76. _FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }
  77. _FORCE_INLINE_ bool operator<(const AreaCMP &p_cmp) const { return area->get_priority() < p_cmp.area->get_priority(); }
  78. _FORCE_INLINE_ AreaCMP() {}
  79. _FORCE_INLINE_ AreaCMP(Area2DSW *p_area) {
  80. area = p_area;
  81. refCount = 1;
  82. }
  83. };
  84. Vector<AreaCMP> areas;
  85. struct Contact {
  86. Vector2 local_pos;
  87. Vector2 local_normal;
  88. real_t depth;
  89. int local_shape;
  90. Vector2 collider_pos;
  91. int collider_shape;
  92. ObjectID collider_instance_id;
  93. RID collider;
  94. Vector2 collider_velocity_at_pos;
  95. };
  96. Vector<Contact> contacts; //no contacts by default
  97. int contact_count;
  98. struct ForceIntegrationCallback {
  99. ObjectID id;
  100. StringName method;
  101. Variant callback_udata;
  102. };
  103. ForceIntegrationCallback *fi_callback;
  104. uint64_t island_step;
  105. Body2DSW *island_next;
  106. Body2DSW *island_list_next;
  107. _FORCE_INLINE_ void _compute_area_gravity_and_dampenings(const Area2DSW *p_area);
  108. Physics2DDirectBodyStateSW *direct_access = nullptr;
  109. friend class Physics2DDirectBodyStateSW; // i give up, too many functions to expose
  110. public:
  111. void set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
  112. _FORCE_INLINE_ void add_area(Area2DSW *p_area) {
  113. int index = areas.find(AreaCMP(p_area));
  114. if (index > -1) {
  115. areas.write[index].refCount += 1;
  116. } else {
  117. areas.ordered_insert(AreaCMP(p_area));
  118. }
  119. }
  120. _FORCE_INLINE_ void remove_area(Area2DSW *p_area) {
  121. int index = areas.find(AreaCMP(p_area));
  122. if (index > -1) {
  123. areas.write[index].refCount -= 1;
  124. if (areas[index].refCount < 1) {
  125. areas.remove(index);
  126. }
  127. }
  128. }
  129. _FORCE_INLINE_ void set_max_contacts_reported(int p_size) {
  130. contacts.resize(p_size);
  131. contact_count = 0;
  132. if (mode == Physics2DServer::BODY_MODE_KINEMATIC && p_size) {
  133. set_active(true);
  134. }
  135. }
  136. _FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
  137. _FORCE_INLINE_ bool can_report_contacts() const { return !contacts.empty(); }
  138. _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_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector2 &p_collider_velocity_at_pos);
  139. _FORCE_INLINE_ void add_exception(const RID &p_exception) { exceptions.insert(p_exception); }
  140. _FORCE_INLINE_ void remove_exception(const RID &p_exception) { exceptions.erase(p_exception); }
  141. _FORCE_INLINE_ bool has_exception(const RID &p_exception) const { return exceptions.has(p_exception); }
  142. _FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
  143. _FORCE_INLINE_ uint64_t get_island_step() const { return island_step; }
  144. _FORCE_INLINE_ void set_island_step(uint64_t p_step) { island_step = p_step; }
  145. _FORCE_INLINE_ Body2DSW *get_island_next() const { return island_next; }
  146. _FORCE_INLINE_ void set_island_next(Body2DSW *p_next) { island_next = p_next; }
  147. _FORCE_INLINE_ Body2DSW *get_island_list_next() const { return island_list_next; }
  148. _FORCE_INLINE_ void set_island_list_next(Body2DSW *p_next) { island_list_next = p_next; }
  149. _FORCE_INLINE_ void add_constraint(Constraint2DSW *p_constraint, int p_pos) { constraint_map[p_constraint] = p_pos; }
  150. _FORCE_INLINE_ void remove_constraint(Constraint2DSW *p_constraint) { constraint_map.erase(p_constraint); }
  151. const Map<Constraint2DSW *, int> &get_constraint_map() const { return constraint_map; }
  152. _FORCE_INLINE_ void clear_constraint_map() { constraint_map.clear(); }
  153. _FORCE_INLINE_ void set_omit_force_integration(bool p_omit_force_integration) { omit_force_integration = p_omit_force_integration; }
  154. _FORCE_INLINE_ bool get_omit_force_integration() const { return omit_force_integration; }
  155. _FORCE_INLINE_ void set_linear_velocity(const Vector2 &p_velocity) { linear_velocity = p_velocity; }
  156. _FORCE_INLINE_ Vector2 get_linear_velocity() const { return linear_velocity; }
  157. _FORCE_INLINE_ void set_angular_velocity(real_t p_velocity) { angular_velocity = p_velocity; }
  158. _FORCE_INLINE_ real_t get_angular_velocity() const { return angular_velocity; }
  159. _FORCE_INLINE_ void set_biased_linear_velocity(const Vector2 &p_velocity) { biased_linear_velocity = p_velocity; }
  160. _FORCE_INLINE_ Vector2 get_biased_linear_velocity() const { return biased_linear_velocity; }
  161. _FORCE_INLINE_ void set_biased_angular_velocity(real_t p_velocity) { biased_angular_velocity = p_velocity; }
  162. _FORCE_INLINE_ real_t get_biased_angular_velocity() const { return biased_angular_velocity; }
  163. _FORCE_INLINE_ void apply_central_impulse(const Vector2 &p_impulse) {
  164. linear_velocity += p_impulse * _inv_mass;
  165. }
  166. _FORCE_INLINE_ void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse) {
  167. linear_velocity += p_impulse * _inv_mass;
  168. angular_velocity += _inv_inertia * p_offset.cross(p_impulse);
  169. }
  170. _FORCE_INLINE_ void apply_torque_impulse(real_t p_torque) {
  171. angular_velocity += _inv_inertia * p_torque;
  172. }
  173. _FORCE_INLINE_ void apply_bias_impulse(const Vector2 &p_pos, const Vector2 &p_j) {
  174. biased_linear_velocity += p_j * _inv_mass;
  175. biased_angular_velocity += _inv_inertia * p_pos.cross(p_j);
  176. }
  177. void set_active(bool p_active);
  178. _FORCE_INLINE_ bool is_active() const { return active; }
  179. _FORCE_INLINE_ void wakeup() {
  180. if ((!get_space()) || mode == Physics2DServer::BODY_MODE_STATIC || mode == Physics2DServer::BODY_MODE_KINEMATIC) {
  181. return;
  182. }
  183. set_active(true);
  184. }
  185. void set_param(Physics2DServer::BodyParameter p_param, real_t);
  186. real_t get_param(Physics2DServer::BodyParameter p_param) const;
  187. void set_mode(Physics2DServer::BodyMode p_mode);
  188. Physics2DServer::BodyMode get_mode() const;
  189. void set_state(Physics2DServer::BodyState p_state, const Variant &p_variant);
  190. Variant get_state(Physics2DServer::BodyState p_state) const;
  191. void set_applied_force(const Vector2 &p_force) { applied_force = p_force; }
  192. Vector2 get_applied_force() const { return applied_force; }
  193. void set_applied_torque(real_t p_torque) { applied_torque = p_torque; }
  194. real_t get_applied_torque() const { return applied_torque; }
  195. _FORCE_INLINE_ void add_central_force(const Vector2 &p_force) {
  196. applied_force += p_force;
  197. }
  198. _FORCE_INLINE_ void add_force(const Vector2 &p_offset, const Vector2 &p_force) {
  199. applied_force += p_force;
  200. applied_torque += p_offset.cross(p_force);
  201. }
  202. _FORCE_INLINE_ void add_torque(real_t p_torque) {
  203. applied_torque += p_torque;
  204. }
  205. _FORCE_INLINE_ void set_continuous_collision_detection_mode(Physics2DServer::CCDMode p_mode) { continuous_cd_mode = p_mode; }
  206. _FORCE_INLINE_ Physics2DServer::CCDMode get_continuous_collision_detection_mode() const { return continuous_cd_mode; }
  207. void set_space(Space2DSW *p_space);
  208. void update_inertias();
  209. _FORCE_INLINE_ real_t get_inv_mass() const { return _inv_mass; }
  210. _FORCE_INLINE_ real_t get_inv_inertia() const { return _inv_inertia; }
  211. _FORCE_INLINE_ real_t get_friction() const { return friction; }
  212. _FORCE_INLINE_ Vector2 get_gravity() const { return gravity; }
  213. _FORCE_INLINE_ real_t get_bounce() const { return bounce; }
  214. _FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }
  215. _FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }
  216. void integrate_forces(real_t p_step);
  217. void integrate_velocities(real_t p_step);
  218. _FORCE_INLINE_ Vector2 get_velocity_in_local_point(const Vector2 &rel_pos) const {
  219. return linear_velocity + Vector2(-angular_velocity * rel_pos.y, angular_velocity * rel_pos.x);
  220. }
  221. _FORCE_INLINE_ Vector2 get_motion() const {
  222. if (mode > Physics2DServer::BODY_MODE_KINEMATIC) {
  223. return new_transform.get_origin() - get_transform().get_origin();
  224. } else if (mode == Physics2DServer::BODY_MODE_KINEMATIC) {
  225. return get_transform().get_origin() - new_transform.get_origin(); //kinematic simulates forward
  226. }
  227. return Vector2();
  228. }
  229. void call_queries();
  230. void wakeup_neighbours();
  231. bool sleep_test(real_t p_step);
  232. Physics2DDirectBodyStateSW *get_direct_state() const { return direct_access; }
  233. Body2DSW();
  234. ~Body2DSW();
  235. };
  236. //add contact inline
  237. void Body2DSW::add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_normal, real_t p_depth, int p_local_shape, 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) {
  238. int c_max = contacts.size();
  239. if (c_max == 0) {
  240. return;
  241. }
  242. Contact *c = contacts.ptrw();
  243. int idx = -1;
  244. if (contact_count < c_max) {
  245. idx = contact_count++;
  246. } else {
  247. real_t least_depth = 1e20;
  248. int least_deep = -1;
  249. for (int i = 0; i < c_max; i++) {
  250. if (i == 0 || c[i].depth < least_depth) {
  251. least_deep = i;
  252. least_depth = c[i].depth;
  253. }
  254. }
  255. if (least_deep >= 0 && least_depth < p_depth) {
  256. idx = least_deep;
  257. }
  258. if (idx == -1) {
  259. return; //none least deepe than this
  260. }
  261. }
  262. c[idx].local_pos = p_local_pos;
  263. c[idx].local_normal = p_local_normal;
  264. c[idx].depth = p_depth;
  265. c[idx].local_shape = p_local_shape;
  266. c[idx].collider_pos = p_collider_pos;
  267. c[idx].collider_shape = p_collider_shape;
  268. c[idx].collider_instance_id = p_collider_instance_id;
  269. c[idx].collider = p_collider;
  270. c[idx].collider_velocity_at_pos = p_collider_velocity_at_pos;
  271. }
  272. class Physics2DDirectBodyStateSW : public Physics2DDirectBodyState {
  273. GDCLASS(Physics2DDirectBodyStateSW, Physics2DDirectBodyState);
  274. public:
  275. Body2DSW *body = nullptr;
  276. virtual Vector2 get_total_gravity() const { return body->gravity; } // get gravity vector working on this body space/area
  277. virtual real_t get_total_angular_damp() const { return body->area_angular_damp; } // get density of this body space/area
  278. virtual real_t get_total_linear_damp() const { return body->area_linear_damp; } // get density of this body space/area
  279. virtual real_t get_inverse_mass() const { return body->get_inv_mass(); } // get the mass
  280. virtual real_t get_inverse_inertia() const { return body->get_inv_inertia(); } // get density of this body space
  281. virtual void set_linear_velocity(const Vector2 &p_velocity) {
  282. body->wakeup();
  283. body->set_linear_velocity(p_velocity);
  284. }
  285. virtual Vector2 get_linear_velocity() const { return body->get_linear_velocity(); }
  286. virtual void set_angular_velocity(real_t p_velocity) {
  287. body->wakeup();
  288. body->set_angular_velocity(p_velocity);
  289. }
  290. virtual real_t get_angular_velocity() const { return body->get_angular_velocity(); }
  291. virtual void set_transform(const Transform2D &p_transform) { body->set_state(Physics2DServer::BODY_STATE_TRANSFORM, p_transform); }
  292. virtual Transform2D get_transform() const { return body->get_transform(); }
  293. virtual Vector2 get_velocity_at_local_position(const Vector2 &p_position) const { return body->get_velocity_in_local_point(p_position); }
  294. virtual void add_central_force(const Vector2 &p_force) {
  295. body->wakeup();
  296. body->add_central_force(p_force);
  297. }
  298. virtual void add_force(const Vector2 &p_offset, const Vector2 &p_force) {
  299. body->wakeup();
  300. body->add_force(p_offset, p_force);
  301. }
  302. virtual void add_torque(real_t p_torque) {
  303. body->wakeup();
  304. body->add_torque(p_torque);
  305. }
  306. virtual void apply_central_impulse(const Vector2 &p_impulse) {
  307. body->wakeup();
  308. body->apply_central_impulse(p_impulse);
  309. }
  310. virtual void apply_impulse(const Vector2 &p_offset, const Vector2 &p_force) {
  311. body->wakeup();
  312. body->apply_impulse(p_offset, p_force);
  313. }
  314. virtual void apply_torque_impulse(real_t p_torque) {
  315. body->wakeup();
  316. body->apply_torque_impulse(p_torque);
  317. }
  318. virtual void set_sleep_state(bool p_enable) { body->set_active(!p_enable); }
  319. virtual bool is_sleeping() const { return !body->is_active(); }
  320. virtual int get_contact_count() const { return body->contact_count; }
  321. virtual Vector2 get_contact_local_position(int p_contact_idx) const {
  322. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  323. return body->contacts[p_contact_idx].local_pos;
  324. }
  325. virtual Vector2 get_contact_local_normal(int p_contact_idx) const {
  326. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  327. return body->contacts[p_contact_idx].local_normal;
  328. }
  329. virtual int get_contact_local_shape(int p_contact_idx) const {
  330. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1);
  331. return body->contacts[p_contact_idx].local_shape;
  332. }
  333. virtual RID get_contact_collider(int p_contact_idx) const {
  334. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID());
  335. return body->contacts[p_contact_idx].collider;
  336. }
  337. virtual Vector2 get_contact_collider_position(int p_contact_idx) const {
  338. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  339. return body->contacts[p_contact_idx].collider_pos;
  340. }
  341. virtual ObjectID get_contact_collider_id(int p_contact_idx) const {
  342. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
  343. return body->contacts[p_contact_idx].collider_instance_id;
  344. }
  345. virtual int get_contact_collider_shape(int p_contact_idx) const {
  346. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
  347. return body->contacts[p_contact_idx].collider_shape;
  348. }
  349. virtual Variant get_contact_collider_shape_metadata(int p_contact_idx) const;
  350. virtual Vector2 get_contact_collider_velocity_at_position(int p_contact_idx) const {
  351. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
  352. return body->contacts[p_contact_idx].collider_velocity_at_pos;
  353. }
  354. virtual Physics2DDirectSpaceState *get_space_state();
  355. virtual real_t get_step() const;
  356. Physics2DDirectBodyStateSW() {}
  357. };
  358. #endif // BODY_2D_SW_H