body_sw.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*************************************************************************/
  2. /* body_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #ifndef BODY_SW_H
  31. #define BODY_SW_H
  32. #include "area_sw.h"
  33. #include "collision_object_sw.h"
  34. #include "vset.h"
  35. class ConstraintSW;
  36. class BodySW : public CollisionObjectSW {
  37. PhysicsServer::BodyMode mode;
  38. Vector3 linear_velocity;
  39. Vector3 angular_velocity;
  40. Vector3 biased_linear_velocity;
  41. Vector3 biased_angular_velocity;
  42. real_t mass;
  43. real_t bounce;
  44. real_t friction;
  45. real_t linear_damp;
  46. real_t angular_damp;
  47. real_t gravity_scale;
  48. PhysicsServer::BodyAxisLock axis_lock;
  49. real_t _inv_mass;
  50. Vector3 _inv_inertia; // Relative to the principal axes of inertia
  51. // Relative to the local frame of reference
  52. Basis principal_inertia_axes_local;
  53. Vector3 center_of_mass_local;
  54. // In world orientation with local origin
  55. Basis _inv_inertia_tensor;
  56. Basis principal_inertia_axes;
  57. Vector3 center_of_mass;
  58. Vector3 gravity;
  59. real_t still_time;
  60. Vector3 applied_force;
  61. Vector3 applied_torque;
  62. real_t area_angular_damp;
  63. real_t area_linear_damp;
  64. SelfList<BodySW> active_list;
  65. SelfList<BodySW> inertia_update_list;
  66. SelfList<BodySW> direct_state_query_list;
  67. VSet<RID> exceptions;
  68. bool omit_force_integration;
  69. bool active;
  70. bool first_integration;
  71. bool continuous_cd;
  72. bool can_sleep;
  73. bool first_time_kinematic;
  74. void _update_inertia();
  75. virtual void _shapes_changed();
  76. Transform new_transform;
  77. Map<ConstraintSW *, int> constraint_map;
  78. struct AreaCMP {
  79. AreaSW *area;
  80. int refCount;
  81. _FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }
  82. _FORCE_INLINE_ bool operator<(const AreaCMP &p_cmp) const { return area->get_priority() < p_cmp.area->get_priority(); }
  83. _FORCE_INLINE_ AreaCMP() {}
  84. _FORCE_INLINE_ AreaCMP(AreaSW *p_area) {
  85. area = p_area;
  86. refCount = 1;
  87. }
  88. };
  89. Vector<AreaCMP> areas;
  90. struct Contact {
  91. Vector3 local_pos;
  92. Vector3 local_normal;
  93. real_t depth;
  94. int local_shape;
  95. Vector3 collider_pos;
  96. int collider_shape;
  97. ObjectID collider_instance_id;
  98. RID collider;
  99. Vector3 collider_velocity_at_pos;
  100. };
  101. Vector<Contact> contacts; //no contacts by default
  102. int contact_count;
  103. struct ForceIntegrationCallback {
  104. ObjectID id;
  105. StringName method;
  106. Variant udata;
  107. };
  108. ForceIntegrationCallback *fi_callback;
  109. uint64_t island_step;
  110. BodySW *island_next;
  111. BodySW *island_list_next;
  112. _FORCE_INLINE_ void _compute_area_gravity_and_dampenings(const AreaSW *p_area);
  113. _FORCE_INLINE_ void _update_transform_dependant();
  114. friend class PhysicsDirectBodyStateSW; // i give up, too many functions to expose
  115. public:
  116. void set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
  117. _FORCE_INLINE_ void add_area(AreaSW *p_area) {
  118. int index = areas.find(AreaCMP(p_area));
  119. if (index > -1) {
  120. areas[index].refCount += 1;
  121. } else {
  122. areas.ordered_insert(AreaCMP(p_area));
  123. }
  124. }
  125. _FORCE_INLINE_ void remove_area(AreaSW *p_area) {
  126. int index = areas.find(AreaCMP(p_area));
  127. if (index > -1) {
  128. areas[index].refCount -= 1;
  129. if (areas[index].refCount < 1)
  130. areas.remove(index);
  131. }
  132. }
  133. _FORCE_INLINE_ void set_max_contacts_reported(int p_size) {
  134. contacts.resize(p_size);
  135. contact_count = 0;
  136. if (mode == PhysicsServer::BODY_MODE_KINEMATIC && p_size) set_active(true);
  137. }
  138. _FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
  139. _FORCE_INLINE_ bool can_report_contacts() const { return !contacts.empty(); }
  140. _FORCE_INLINE_ void add_contact(const Vector3 &p_local_pos, const Vector3 &p_local_normal, real_t p_depth, int p_local_shape, const Vector3 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector3 &p_collider_velocity_at_pos);
  141. _FORCE_INLINE_ void add_exception(const RID &p_exception) { exceptions.insert(p_exception); }
  142. _FORCE_INLINE_ void remove_exception(const RID &p_exception) { exceptions.erase(p_exception); }
  143. _FORCE_INLINE_ bool has_exception(const RID &p_exception) const { return exceptions.has(p_exception); }
  144. _FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
  145. _FORCE_INLINE_ uint64_t get_island_step() const { return island_step; }
  146. _FORCE_INLINE_ void set_island_step(uint64_t p_step) { island_step = p_step; }
  147. _FORCE_INLINE_ BodySW *get_island_next() const { return island_next; }
  148. _FORCE_INLINE_ void set_island_next(BodySW *p_next) { island_next = p_next; }
  149. _FORCE_INLINE_ BodySW *get_island_list_next() const { return island_list_next; }
  150. _FORCE_INLINE_ void set_island_list_next(BodySW *p_next) { island_list_next = p_next; }
  151. _FORCE_INLINE_ void add_constraint(ConstraintSW *p_constraint, int p_pos) { constraint_map[p_constraint] = p_pos; }
  152. _FORCE_INLINE_ void remove_constraint(ConstraintSW *p_constraint) { constraint_map.erase(p_constraint); }
  153. const Map<ConstraintSW *, int> &get_constraint_map() const { return constraint_map; }
  154. _FORCE_INLINE_ void clear_constraint_map() { constraint_map.clear(); }
  155. _FORCE_INLINE_ void set_omit_force_integration(bool p_omit_force_integration) { omit_force_integration = p_omit_force_integration; }
  156. _FORCE_INLINE_ bool get_omit_force_integration() const { return omit_force_integration; }
  157. _FORCE_INLINE_ Basis get_principal_inertia_axes() const { return principal_inertia_axes; }
  158. _FORCE_INLINE_ Vector3 get_center_of_mass() const { return center_of_mass; }
  159. _FORCE_INLINE_ Vector3 xform_local_to_principal(const Vector3 &p_pos) const { return principal_inertia_axes_local.xform(p_pos - center_of_mass_local); }
  160. _FORCE_INLINE_ void set_linear_velocity(const Vector3 &p_velocity) { linear_velocity = p_velocity; }
  161. _FORCE_INLINE_ Vector3 get_linear_velocity() const { return linear_velocity; }
  162. _FORCE_INLINE_ void set_angular_velocity(const Vector3 &p_velocity) { angular_velocity = p_velocity; }
  163. _FORCE_INLINE_ Vector3 get_angular_velocity() const { return angular_velocity; }
  164. _FORCE_INLINE_ const Vector3 &get_biased_linear_velocity() const { return biased_linear_velocity; }
  165. _FORCE_INLINE_ const Vector3 &get_biased_angular_velocity() const { return biased_angular_velocity; }
  166. _FORCE_INLINE_ void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) {
  167. linear_velocity += p_j * _inv_mass;
  168. angular_velocity += _inv_inertia_tensor.xform((p_pos - center_of_mass).cross(p_j));
  169. }
  170. _FORCE_INLINE_ void apply_torque_impulse(const Vector3 &p_j) {
  171. angular_velocity += _inv_inertia_tensor.xform(p_j);
  172. }
  173. _FORCE_INLINE_ void apply_bias_impulse(const Vector3 &p_pos, const Vector3 &p_j, real_t p_max_delta_av = -1.0) {
  174. biased_linear_velocity += p_j * _inv_mass;
  175. if (p_max_delta_av != 0.0) {
  176. Vector3 delta_av = _inv_inertia_tensor.xform((p_pos - center_of_mass).cross(p_j));
  177. if (p_max_delta_av > 0 && delta_av.length() > p_max_delta_av) {
  178. delta_av = delta_av.normalized() * p_max_delta_av;
  179. }
  180. biased_angular_velocity += delta_av;
  181. }
  182. }
  183. _FORCE_INLINE_ void apply_bias_torque_impulse(const Vector3 &p_j) {
  184. biased_angular_velocity += _inv_inertia_tensor.xform(p_j);
  185. }
  186. _FORCE_INLINE_ void add_force(const Vector3 &p_force, const Vector3 &p_pos) {
  187. applied_force += p_force;
  188. applied_torque += p_pos.cross(p_force);
  189. }
  190. void set_active(bool p_active);
  191. _FORCE_INLINE_ bool is_active() const { return active; }
  192. _FORCE_INLINE_ void wakeup() {
  193. if ((!get_space()) || mode == PhysicsServer::BODY_MODE_STATIC || mode == PhysicsServer::BODY_MODE_KINEMATIC)
  194. return;
  195. set_active(true);
  196. }
  197. void set_param(PhysicsServer::BodyParameter p_param, real_t);
  198. real_t get_param(PhysicsServer::BodyParameter p_param) const;
  199. void set_mode(PhysicsServer::BodyMode p_mode);
  200. PhysicsServer::BodyMode get_mode() const;
  201. void set_state(PhysicsServer::BodyState p_state, const Variant &p_variant);
  202. Variant get_state(PhysicsServer::BodyState p_state) const;
  203. void set_applied_force(const Vector3 &p_force) { applied_force = p_force; }
  204. Vector3 get_applied_force() const { return applied_force; }
  205. void set_applied_torque(const Vector3 &p_torque) { applied_torque = p_torque; }
  206. Vector3 get_applied_torque() const { return applied_torque; }
  207. _FORCE_INLINE_ void set_continuous_collision_detection(bool p_enable) { continuous_cd = p_enable; }
  208. _FORCE_INLINE_ bool is_continuous_collision_detection_enabled() const { return continuous_cd; }
  209. void set_space(SpaceSW *p_space);
  210. void update_inertias();
  211. _FORCE_INLINE_ real_t get_inv_mass() const { return _inv_mass; }
  212. _FORCE_INLINE_ Vector3 get_inv_inertia() const { return _inv_inertia; }
  213. _FORCE_INLINE_ Basis get_inv_inertia_tensor() const { return _inv_inertia_tensor; }
  214. _FORCE_INLINE_ real_t get_friction() const { return friction; }
  215. _FORCE_INLINE_ Vector3 get_gravity() const { return gravity; }
  216. _FORCE_INLINE_ real_t get_bounce() const { return bounce; }
  217. _FORCE_INLINE_ void set_axis_lock(PhysicsServer::BodyAxisLock p_lock) { axis_lock = p_lock; }
  218. _FORCE_INLINE_ PhysicsServer::BodyAxisLock get_axis_lock() const { return axis_lock; }
  219. void integrate_forces(real_t p_step);
  220. void integrate_velocities(real_t p_step);
  221. _FORCE_INLINE_ Vector3 get_velocity_in_local_point(const Vector3 &rel_pos) const {
  222. return linear_velocity + angular_velocity.cross(rel_pos - center_of_mass);
  223. }
  224. _FORCE_INLINE_ real_t compute_impulse_denominator(const Vector3 &p_pos, const Vector3 &p_normal) const {
  225. Vector3 r0 = p_pos - get_transform().origin - center_of_mass;
  226. Vector3 c0 = (r0).cross(p_normal);
  227. Vector3 vec = (_inv_inertia_tensor.xform_inv(c0)).cross(r0);
  228. return _inv_mass + p_normal.dot(vec);
  229. }
  230. _FORCE_INLINE_ real_t compute_angular_impulse_denominator(const Vector3 &p_axis) const {
  231. return p_axis.dot(_inv_inertia_tensor.xform_inv(p_axis));
  232. }
  233. //void simulate_motion(const Transform& p_xform,real_t p_step);
  234. void call_queries();
  235. void wakeup_neighbours();
  236. bool sleep_test(real_t p_step);
  237. BodySW();
  238. ~BodySW();
  239. };
  240. //add contact inline
  241. void BodySW::add_contact(const Vector3 &p_local_pos, const Vector3 &p_local_normal, real_t p_depth, int p_local_shape, const Vector3 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector3 &p_collider_velocity_at_pos) {
  242. int c_max = contacts.size();
  243. if (c_max == 0)
  244. return;
  245. Contact *c = &contacts[0];
  246. int idx = -1;
  247. if (contact_count < c_max) {
  248. idx = contact_count++;
  249. } else {
  250. real_t least_depth = 1e20;
  251. int least_deep = -1;
  252. for (int i = 0; i < c_max; i++) {
  253. if (i == 0 || c[i].depth < least_depth) {
  254. least_deep = i;
  255. least_depth = c[i].depth;
  256. }
  257. }
  258. if (least_deep >= 0 && least_depth < p_depth) {
  259. idx = least_deep;
  260. }
  261. if (idx == -1)
  262. return; //none least deepe than this
  263. }
  264. c[idx].local_pos = p_local_pos;
  265. c[idx].local_normal = p_local_normal;
  266. c[idx].depth = p_depth;
  267. c[idx].local_shape = p_local_shape;
  268. c[idx].collider_pos = p_collider_pos;
  269. c[idx].collider_shape = p_collider_shape;
  270. c[idx].collider_instance_id = p_collider_instance_id;
  271. c[idx].collider = p_collider;
  272. c[idx].collider_velocity_at_pos = p_collider_velocity_at_pos;
  273. }
  274. class PhysicsDirectBodyStateSW : public PhysicsDirectBodyState {
  275. GDCLASS(PhysicsDirectBodyStateSW, PhysicsDirectBodyState);
  276. public:
  277. static PhysicsDirectBodyStateSW *singleton;
  278. BodySW *body;
  279. real_t step;
  280. virtual Vector3 get_total_gravity() const { return body->gravity; } // get gravity vector working on this body space/area
  281. virtual real_t get_total_angular_damp() const { return body->area_angular_damp; } // get density of this body space/area
  282. virtual real_t get_total_linear_damp() const { return body->area_linear_damp; } // get density of this body space/area
  283. virtual Vector3 get_center_of_mass() const { return body->get_center_of_mass(); }
  284. virtual Basis get_principal_inertia_axes() const { return body->get_principal_inertia_axes(); }
  285. virtual real_t get_inverse_mass() const { return body->get_inv_mass(); } // get the mass
  286. virtual Vector3 get_inverse_inertia() const { return body->get_inv_inertia(); } // get density of this body space
  287. virtual Basis get_inverse_inertia_tensor() const { return body->get_inv_inertia_tensor(); } // get density of this body space
  288. virtual void set_linear_velocity(const Vector3 &p_velocity) { body->set_linear_velocity(p_velocity); }
  289. virtual Vector3 get_linear_velocity() const { return body->get_linear_velocity(); }
  290. virtual void set_angular_velocity(const Vector3 &p_velocity) { body->set_angular_velocity(p_velocity); }
  291. virtual Vector3 get_angular_velocity() const { return body->get_angular_velocity(); }
  292. virtual void set_transform(const Transform &p_transform) { body->set_state(PhysicsServer::BODY_STATE_TRANSFORM, p_transform); }
  293. virtual Transform get_transform() const { return body->get_transform(); }
  294. virtual void add_force(const Vector3 &p_force, const Vector3 &p_pos) { body->add_force(p_force, p_pos); }
  295. virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) { body->apply_impulse(p_pos, p_j); }
  296. virtual void apply_torque_impulse(const Vector3 &p_j) { body->apply_torque_impulse(p_j); }
  297. virtual void set_sleep_state(bool p_enable) { body->set_active(!p_enable); }
  298. virtual bool is_sleeping() const { return !body->is_active(); }
  299. virtual int get_contact_count() const { return body->contact_count; }
  300. virtual Vector3 get_contact_local_position(int p_contact_idx) const {
  301. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
  302. return body->contacts[p_contact_idx].local_pos;
  303. }
  304. virtual Vector3 get_contact_local_normal(int p_contact_idx) const {
  305. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
  306. return body->contacts[p_contact_idx].local_normal;
  307. }
  308. virtual int get_contact_local_shape(int p_contact_idx) const {
  309. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1);
  310. return body->contacts[p_contact_idx].local_shape;
  311. }
  312. virtual RID get_contact_collider(int p_contact_idx) const {
  313. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID());
  314. return body->contacts[p_contact_idx].collider;
  315. }
  316. virtual Vector3 get_contact_collider_position(int p_contact_idx) const {
  317. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
  318. return body->contacts[p_contact_idx].collider_pos;
  319. }
  320. virtual ObjectID get_contact_collider_id(int p_contact_idx) const {
  321. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
  322. return body->contacts[p_contact_idx].collider_instance_id;
  323. }
  324. virtual int get_contact_collider_shape(int p_contact_idx) const {
  325. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
  326. return body->contacts[p_contact_idx].collider_shape;
  327. }
  328. virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const {
  329. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
  330. return body->contacts[p_contact_idx].collider_velocity_at_pos;
  331. }
  332. virtual PhysicsDirectSpaceState *get_space_state();
  333. virtual real_t get_step() const { return step; }
  334. PhysicsDirectBodyStateSW() {
  335. singleton = this;
  336. body = NULL;
  337. }
  338. };
  339. #endif // BODY__SW_H