body_sw.h 17 KB

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