body_sw.h 18 KB

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