body_2d_sw.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*************************************************************************/
  2. /* body_2d_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef BODY_2D_SW_H
  30. #define BODY_2D_SW_H
  31. #include "collision_object_2d_sw.h"
  32. #include "vset.h"
  33. #include "area_2d_sw.h"
  34. class Constraint2DSW;
  35. class Body2DSW : public CollisionObject2DSW {
  36. Physics2DServer::BodyMode mode;
  37. Vector2 biased_linear_velocity;
  38. real_t biased_angular_velocity;
  39. Vector2 linear_velocity;
  40. real_t angular_velocity;
  41. real_t linear_damp;
  42. real_t angular_damp;
  43. real_t gravity_scale;
  44. real_t mass;
  45. real_t bounce;
  46. real_t friction;
  47. real_t _inv_mass;
  48. real_t _inv_inertia;
  49. Vector2 gravity;
  50. real_t area_linear_damp;
  51. real_t area_angular_damp;
  52. real_t still_time;
  53. Vector2 applied_force;
  54. real_t applied_torque;
  55. Vector2 one_way_collision_direction;
  56. float one_way_collision_max_depth;
  57. SelfList<Body2DSW> active_list;
  58. SelfList<Body2DSW> inertia_update_list;
  59. SelfList<Body2DSW> direct_state_query_list;
  60. VSet<RID> exceptions;
  61. Physics2DServer::CCDMode continuous_cd_mode;
  62. bool omit_force_integration;
  63. bool active;
  64. bool can_sleep;
  65. bool first_time_kinematic;
  66. bool using_one_way_cache;
  67. void _update_inertia();
  68. virtual void _shapes_changed();
  69. Matrix32 new_transform;
  70. Map<Constraint2DSW*,int> constraint_map;
  71. struct AreaCMP {
  72. Area2DSW *area;
  73. _FORCE_INLINE_ bool operator==(const AreaCMP& p_cmp) const { return area->get_self() == p_cmp.area->get_self();}
  74. _FORCE_INLINE_ bool operator<(const AreaCMP& p_cmp) const { return area->get_priority() < p_cmp.area->get_priority();}
  75. _FORCE_INLINE_ AreaCMP() {}
  76. _FORCE_INLINE_ AreaCMP(Area2DSW *p_area) { area=p_area;}
  77. };
  78. Vector<AreaCMP> areas;
  79. struct Contact {
  80. Vector2 local_pos;
  81. Vector2 local_normal;
  82. float depth;
  83. int local_shape;
  84. Vector2 collider_pos;
  85. int collider_shape;
  86. ObjectID collider_instance_id;
  87. RID collider;
  88. Vector2 collider_velocity_at_pos;
  89. };
  90. Vector<Contact> contacts; //no contacts by default
  91. int contact_count;
  92. struct ForceIntegrationCallback {
  93. ObjectID id;
  94. StringName method;
  95. Variant callback_udata;
  96. };
  97. ForceIntegrationCallback *fi_callback;
  98. uint64_t island_step;
  99. Body2DSW *island_next;
  100. Body2DSW *island_list_next;
  101. _FORCE_INLINE_ void _compute_area_gravity(const Area2DSW *p_area);
  102. friend class Physics2DDirectBodyStateSW; // i give up, too many functions to expose
  103. public:
  104. void set_force_integration_callback(ObjectID p_id, const StringName& p_method, const Variant &p_udata=Variant());
  105. _FORCE_INLINE_ void add_area(Area2DSW *p_area) { areas.ordered_insert(AreaCMP(p_area)); }
  106. _FORCE_INLINE_ void remove_area(Area2DSW *p_area) { areas.erase(AreaCMP(p_area)); }
  107. _FORCE_INLINE_ void set_max_contacts_reported(int p_size) { contacts.resize(p_size); contact_count=0; if (mode==Physics2DServer::BODY_MODE_KINEMATIC && p_size) set_active(true);}
  108. _FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
  109. _FORCE_INLINE_ bool can_report_contacts() const { return !contacts.empty(); }
  110. _FORCE_INLINE_ void add_contact(const Vector2& p_local_pos,const Vector2& p_local_normal, float 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);
  111. _FORCE_INLINE_ void add_exception(const RID& p_exception) { exceptions.insert(p_exception);}
  112. _FORCE_INLINE_ void remove_exception(const RID& p_exception) { exceptions.erase(p_exception);}
  113. _FORCE_INLINE_ bool has_exception(const RID& p_exception) const { return exceptions.has(p_exception);}
  114. _FORCE_INLINE_ const VSet<RID>& get_exceptions() const { return exceptions;}
  115. _FORCE_INLINE_ uint64_t get_island_step() const { return island_step; }
  116. _FORCE_INLINE_ void set_island_step(uint64_t p_step) { island_step=p_step; }
  117. _FORCE_INLINE_ Body2DSW* get_island_next() const { return island_next; }
  118. _FORCE_INLINE_ void set_island_next(Body2DSW* p_next) { island_next=p_next; }
  119. _FORCE_INLINE_ Body2DSW* get_island_list_next() const { return island_list_next; }
  120. _FORCE_INLINE_ void set_island_list_next(Body2DSW* p_next) { island_list_next=p_next; }
  121. _FORCE_INLINE_ void add_constraint(Constraint2DSW* p_constraint, int p_pos) { constraint_map[p_constraint]=p_pos; }
  122. _FORCE_INLINE_ void remove_constraint(Constraint2DSW* p_constraint) { constraint_map.erase(p_constraint); }
  123. const Map<Constraint2DSW*,int>& get_constraint_map() const { return constraint_map; }
  124. _FORCE_INLINE_ void set_omit_force_integration(bool p_omit_force_integration) { omit_force_integration=p_omit_force_integration; }
  125. _FORCE_INLINE_ bool get_omit_force_integration() const { return omit_force_integration; }
  126. _FORCE_INLINE_ void set_linear_velocity(const Vector2& p_velocity) {linear_velocity=p_velocity; }
  127. _FORCE_INLINE_ Vector2 get_linear_velocity() const { return linear_velocity; }
  128. _FORCE_INLINE_ void set_angular_velocity(real_t p_velocity) { angular_velocity=p_velocity; }
  129. _FORCE_INLINE_ real_t get_angular_velocity() const { return angular_velocity; }
  130. _FORCE_INLINE_ void set_biased_linear_velocity(const Vector2& p_velocity) {biased_linear_velocity=p_velocity; }
  131. _FORCE_INLINE_ Vector2 get_biased_linear_velocity() const { return biased_linear_velocity; }
  132. _FORCE_INLINE_ void set_biased_angular_velocity(real_t p_velocity) { biased_angular_velocity=p_velocity; }
  133. _FORCE_INLINE_ real_t get_biased_angular_velocity() const { return biased_angular_velocity; }
  134. _FORCE_INLINE_ void apply_impulse(const Vector2& p_pos, const Vector2& p_j) {
  135. linear_velocity += p_j * _inv_mass;
  136. angular_velocity += _inv_inertia * p_pos.cross(p_j);
  137. }
  138. _FORCE_INLINE_ void apply_bias_impulse(const Vector2& p_pos, const Vector2& p_j) {
  139. biased_linear_velocity += p_j * _inv_mass;
  140. biased_angular_velocity += _inv_inertia * p_pos.cross(p_j);
  141. }
  142. void set_active(bool p_active);
  143. _FORCE_INLINE_ bool is_active() const { return active; }
  144. _FORCE_INLINE_ void wakeup() {
  145. if ((!get_space()) || mode==Physics2DServer::BODY_MODE_STATIC || mode==Physics2DServer::BODY_MODE_KINEMATIC)
  146. return;
  147. set_active(true);
  148. }
  149. void set_param(Physics2DServer::BodyParameter p_param, float);
  150. float get_param(Physics2DServer::BodyParameter p_param) const;
  151. void set_mode(Physics2DServer::BodyMode p_mode);
  152. Physics2DServer::BodyMode get_mode() const;
  153. void set_state(Physics2DServer::BodyState p_state, const Variant& p_variant);
  154. Variant get_state(Physics2DServer::BodyState p_state) const;
  155. void set_applied_force(const Vector2& p_force) { applied_force=p_force; }
  156. Vector2 get_applied_force() const { return applied_force; }
  157. void set_applied_torque(real_t p_torque) { applied_torque=p_torque; }
  158. real_t get_applied_torque() const { return applied_torque; }
  159. _FORCE_INLINE_ void set_continuous_collision_detection_mode(Physics2DServer::CCDMode p_mode) { continuous_cd_mode=p_mode; }
  160. _FORCE_INLINE_ Physics2DServer::CCDMode get_continuous_collision_detection_mode() const { return continuous_cd_mode; }
  161. void set_one_way_collision_direction(const Vector2& p_dir) {
  162. one_way_collision_direction=p_dir;
  163. using_one_way_cache=one_way_collision_direction!=Vector2();
  164. }
  165. Vector2 get_one_way_collision_direction() const { return one_way_collision_direction; }
  166. void set_one_way_collision_max_depth(float p_depth) { one_way_collision_max_depth=p_depth; }
  167. float get_one_way_collision_max_depth() const { return one_way_collision_max_depth; }
  168. _FORCE_INLINE_ bool is_using_one_way_collision() const { return using_one_way_cache; }
  169. void set_space(Space2DSW *p_space);
  170. void update_inertias();
  171. _FORCE_INLINE_ real_t get_inv_mass() const { return _inv_mass; }
  172. _FORCE_INLINE_ real_t get_inv_inertia() const { return _inv_inertia; }
  173. _FORCE_INLINE_ real_t get_friction() const { return friction; }
  174. _FORCE_INLINE_ Vector2 get_gravity() const { return gravity; }
  175. _FORCE_INLINE_ real_t get_bounce() const { return bounce; }
  176. _FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }
  177. _FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }
  178. void integrate_forces(real_t p_step);
  179. void integrate_velocities(real_t p_step);
  180. _FORCE_INLINE_ Vector2 get_motion() const {
  181. if (mode>Physics2DServer::BODY_MODE_KINEMATIC) {
  182. return new_transform.get_origin() - get_transform().get_origin();
  183. } else if (mode==Physics2DServer::BODY_MODE_KINEMATIC) {
  184. return get_transform().get_origin() -new_transform.get_origin(); //kinematic simulates forward
  185. }
  186. return Vector2();
  187. }
  188. void call_queries();
  189. void wakeup_neighbours();
  190. bool sleep_test(real_t p_step);
  191. Body2DSW();
  192. ~Body2DSW();
  193. };
  194. //add contact inline
  195. void Body2DSW::add_contact(const Vector2& p_local_pos,const Vector2& p_local_normal, float 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) {
  196. int c_max=contacts.size();
  197. if (c_max==0)
  198. return;
  199. Contact *c = &contacts[0];
  200. int idx=-1;
  201. if (contact_count<c_max) {
  202. idx=contact_count++;
  203. } else {
  204. float least_depth=1e20;
  205. int least_deep=-1;
  206. for(int i=0;i<c_max;i++) {
  207. if (i==0 || c[i].depth<least_depth) {
  208. least_deep=i;
  209. least_depth=c[i].depth;
  210. }
  211. }
  212. if (least_deep>=0 && least_depth<p_depth) {
  213. idx=least_deep;
  214. }
  215. if (idx==-1)
  216. return; //none least deepe than this
  217. }
  218. c[idx].local_pos=p_local_pos;
  219. c[idx].local_normal=p_local_normal;
  220. c[idx].depth=p_depth;
  221. c[idx].local_shape=p_local_shape;
  222. c[idx].collider_pos=p_collider_pos;
  223. c[idx].collider_shape=p_collider_shape;
  224. c[idx].collider_instance_id=p_collider_instance_id;
  225. c[idx].collider=p_collider;
  226. c[idx].collider_velocity_at_pos=p_collider_velocity_at_pos;
  227. }
  228. class Physics2DDirectBodyStateSW : public Physics2DDirectBodyState {
  229. OBJ_TYPE( Physics2DDirectBodyStateSW, Physics2DDirectBodyState );
  230. public:
  231. static Physics2DDirectBodyStateSW *singleton;
  232. Body2DSW *body;
  233. real_t step;
  234. virtual Vector2 get_total_gravity() const { return body->get_gravity(); } // get gravity vector working on this body space/area
  235. virtual float get_total_angular_damp() const { return body->get_angular_damp(); } // get density of this body space/area
  236. virtual float get_total_linear_damp() const { return body->get_linear_damp(); } // get density of this body space/area
  237. virtual float get_inverse_mass() const { return body->get_inv_mass(); } // get the mass
  238. virtual real_t get_inverse_inertia() const { return body->get_inv_inertia(); } // get density of this body space
  239. virtual void set_linear_velocity(const Vector2& p_velocity) { body->set_linear_velocity(p_velocity); }
  240. virtual Vector2 get_linear_velocity() const { return body->get_linear_velocity(); }
  241. virtual void set_angular_velocity(real_t p_velocity) { body->set_angular_velocity(p_velocity); }
  242. virtual real_t get_angular_velocity() const { return body->get_angular_velocity(); }
  243. virtual void set_transform(const Matrix32& p_transform) { body->set_state(Physics2DServer::BODY_STATE_TRANSFORM,p_transform); }
  244. virtual Matrix32 get_transform() const { return body->get_transform(); }
  245. virtual void set_sleep_state(bool p_enable) { body->set_active(!p_enable); }
  246. virtual bool is_sleeping() const { return !body->is_active(); }
  247. virtual int get_contact_count() const { return body->contact_count; }
  248. virtual Vector2 get_contact_local_pos(int p_contact_idx) const {
  249. ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,Vector2());
  250. return body->contacts[p_contact_idx].local_pos;
  251. }
  252. virtual Vector2 get_contact_local_normal(int p_contact_idx) const { ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,Vector2()); return body->contacts[p_contact_idx].local_normal; }
  253. virtual int get_contact_local_shape(int p_contact_idx) const { ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,-1); return body->contacts[p_contact_idx].local_shape; }
  254. virtual RID get_contact_collider(int p_contact_idx) const { ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,RID()); return body->contacts[p_contact_idx].collider; }
  255. virtual Vector2 get_contact_collider_pos(int p_contact_idx) const { ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,Vector2()); return body->contacts[p_contact_idx].collider_pos; }
  256. virtual ObjectID get_contact_collider_id(int p_contact_idx) const { ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,0); return body->contacts[p_contact_idx].collider_instance_id; }
  257. virtual int get_contact_collider_shape(int p_contact_idx) const { ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,0); return body->contacts[p_contact_idx].collider_shape; }
  258. virtual Variant get_contact_collider_shape_metadata(int p_contact_idx) const { ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,Variant()); return body->get_shape_metadata(body->contacts[p_contact_idx].collider_shape); }
  259. virtual Vector2 get_contact_collider_velocity_at_pos(int p_contact_idx) const { ERR_FAIL_INDEX_V(p_contact_idx,body->contact_count,Vector2()); return body->contacts[p_contact_idx].collider_velocity_at_pos; }
  260. virtual Physics2DDirectSpaceState* get_space_state();
  261. virtual real_t get_step() const { return step; }
  262. Physics2DDirectBodyStateSW() { singleton=this; body=NULL; }
  263. };
  264. #endif // BODY_2D_SW_H