godot_body_3d.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /**************************************************************************/
  2. /* godot_body_3d.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 GODOT_BODY_3D_H
  31. #define GODOT_BODY_3D_H
  32. #include "godot_area_3d.h"
  33. #include "godot_collision_object_3d.h"
  34. #include "core/templates/vset.h"
  35. class GodotConstraint3D;
  36. class GodotPhysicsDirectBodyState3D;
  37. class GodotBody3D : public GodotCollisionObject3D {
  38. PhysicsServer3D::BodyMode mode = PhysicsServer3D::BODY_MODE_RIGID;
  39. Vector3 linear_velocity;
  40. Vector3 angular_velocity;
  41. Vector3 prev_linear_velocity;
  42. Vector3 prev_angular_velocity;
  43. Vector3 constant_linear_velocity;
  44. Vector3 constant_angular_velocity;
  45. Vector3 biased_linear_velocity;
  46. Vector3 biased_angular_velocity;
  47. real_t mass = 1.0;
  48. real_t bounce = 0.0;
  49. real_t friction = 1.0;
  50. Vector3 inertia;
  51. PhysicsServer3D::BodyDampMode linear_damp_mode = PhysicsServer3D::BODY_DAMP_MODE_COMBINE;
  52. PhysicsServer3D::BodyDampMode angular_damp_mode = PhysicsServer3D::BODY_DAMP_MODE_COMBINE;
  53. real_t linear_damp = 0.0;
  54. real_t angular_damp = 0.0;
  55. real_t total_linear_damp = 0.0;
  56. real_t total_angular_damp = 0.0;
  57. real_t gravity_scale = 1.0;
  58. uint16_t locked_axis = 0;
  59. real_t _inv_mass = 1.0;
  60. Vector3 _inv_inertia; // Relative to the principal axes of inertia
  61. // Relative to the local frame of reference
  62. Basis principal_inertia_axes_local;
  63. Vector3 center_of_mass_local;
  64. // In world orientation with local origin
  65. Basis _inv_inertia_tensor;
  66. Basis principal_inertia_axes;
  67. Vector3 center_of_mass;
  68. bool calculate_inertia = true;
  69. bool calculate_center_of_mass = true;
  70. Vector3 gravity;
  71. real_t still_time = 0.0;
  72. Vector3 applied_force;
  73. Vector3 applied_torque;
  74. Vector3 constant_force;
  75. Vector3 constant_torque;
  76. SelfList<GodotBody3D> active_list;
  77. SelfList<GodotBody3D> mass_properties_update_list;
  78. SelfList<GodotBody3D> direct_state_query_list;
  79. VSet<RID> exceptions;
  80. bool omit_force_integration = false;
  81. bool active = true;
  82. bool continuous_cd = false;
  83. bool can_sleep = true;
  84. bool first_time_kinematic = false;
  85. void _mass_properties_changed();
  86. virtual void _shapes_changed() override;
  87. Transform3D new_transform;
  88. HashMap<GodotConstraint3D *, int> constraint_map;
  89. Vector<AreaCMP> areas;
  90. struct Contact {
  91. Vector3 local_pos;
  92. Vector3 local_normal;
  93. Vector3 local_velocity_at_pos;
  94. real_t depth = 0.0;
  95. int local_shape = 0;
  96. Vector3 collider_pos;
  97. int collider_shape = 0;
  98. ObjectID collider_instance_id;
  99. RID collider;
  100. Vector3 collider_velocity_at_pos;
  101. Vector3 impulse;
  102. };
  103. Vector<Contact> contacts; //no contacts by default
  104. int contact_count = 0;
  105. Callable body_state_callback;
  106. struct ForceIntegrationCallbackData {
  107. Callable callable;
  108. Variant udata;
  109. };
  110. ForceIntegrationCallbackData *fi_callback_data = nullptr;
  111. GodotPhysicsDirectBodyState3D *direct_state = nullptr;
  112. uint64_t island_step = 0;
  113. void _update_transform_dependent();
  114. friend class GodotPhysicsDirectBodyState3D; // i give up, too many functions to expose
  115. public:
  116. void set_state_sync_callback(const Callable &p_callable);
  117. void set_force_integration_callback(const Callable &p_callable, const Variant &p_udata = Variant());
  118. GodotPhysicsDirectBodyState3D *get_direct_state();
  119. _FORCE_INLINE_ void add_area(GodotArea3D *p_area) {
  120. int index = areas.find(AreaCMP(p_area));
  121. if (index > -1) {
  122. areas.write[index].refCount += 1;
  123. } else {
  124. areas.ordered_insert(AreaCMP(p_area));
  125. }
  126. }
  127. _FORCE_INLINE_ void remove_area(GodotArea3D *p_area) {
  128. int index = areas.find(AreaCMP(p_area));
  129. if (index > -1) {
  130. areas.write[index].refCount -= 1;
  131. if (areas[index].refCount < 1) {
  132. areas.remove_at(index);
  133. }
  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 == PhysicsServer3D::BODY_MODE_KINEMATIC && p_size) {
  140. set_active(true);
  141. }
  142. }
  143. _FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
  144. _FORCE_INLINE_ bool can_report_contacts() const { return !contacts.is_empty(); }
  145. _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_local_velocity_at_pos, 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, const Vector3 &p_impulse);
  146. _FORCE_INLINE_ void add_exception(const RID &p_exception) { exceptions.insert(p_exception); }
  147. _FORCE_INLINE_ void remove_exception(const RID &p_exception) { exceptions.erase(p_exception); }
  148. _FORCE_INLINE_ bool has_exception(const RID &p_exception) const { return exceptions.has(p_exception); }
  149. _FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
  150. _FORCE_INLINE_ uint64_t get_island_step() const { return island_step; }
  151. _FORCE_INLINE_ void set_island_step(uint64_t p_step) { island_step = p_step; }
  152. _FORCE_INLINE_ void add_constraint(GodotConstraint3D *p_constraint, int p_pos) { constraint_map[p_constraint] = p_pos; }
  153. _FORCE_INLINE_ void remove_constraint(GodotConstraint3D *p_constraint) { constraint_map.erase(p_constraint); }
  154. const HashMap<GodotConstraint3D *, int> &get_constraint_map() const { return constraint_map; }
  155. _FORCE_INLINE_ void clear_constraint_map() { constraint_map.clear(); }
  156. _FORCE_INLINE_ void set_omit_force_integration(bool p_omit_force_integration) { omit_force_integration = p_omit_force_integration; }
  157. _FORCE_INLINE_ bool get_omit_force_integration() const { return omit_force_integration; }
  158. _FORCE_INLINE_ Basis get_principal_inertia_axes() const { return principal_inertia_axes; }
  159. _FORCE_INLINE_ Vector3 get_center_of_mass() const { return center_of_mass; }
  160. _FORCE_INLINE_ Vector3 get_center_of_mass_local() const { return center_of_mass_local; }
  161. _FORCE_INLINE_ Vector3 xform_local_to_principal(const Vector3 &p_pos) const { return principal_inertia_axes_local.xform(p_pos - center_of_mass_local); }
  162. _FORCE_INLINE_ void set_linear_velocity(const Vector3 &p_velocity) { linear_velocity = p_velocity; }
  163. _FORCE_INLINE_ Vector3 get_linear_velocity() const { return linear_velocity; }
  164. _FORCE_INLINE_ void set_angular_velocity(const Vector3 &p_velocity) { angular_velocity = p_velocity; }
  165. _FORCE_INLINE_ Vector3 get_angular_velocity() const { return angular_velocity; }
  166. _FORCE_INLINE_ Vector3 get_prev_linear_velocity() const { return prev_linear_velocity; }
  167. _FORCE_INLINE_ Vector3 get_prev_angular_velocity() const { return prev_angular_velocity; }
  168. _FORCE_INLINE_ const Vector3 &get_biased_linear_velocity() const { return biased_linear_velocity; }
  169. _FORCE_INLINE_ const Vector3 &get_biased_angular_velocity() const { return biased_angular_velocity; }
  170. _FORCE_INLINE_ void apply_central_impulse(const Vector3 &p_impulse) {
  171. linear_velocity += p_impulse * _inv_mass;
  172. }
  173. _FORCE_INLINE_ void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3()) {
  174. linear_velocity += p_impulse * _inv_mass;
  175. angular_velocity += _inv_inertia_tensor.xform((p_position - center_of_mass).cross(p_impulse));
  176. }
  177. _FORCE_INLINE_ void apply_torque_impulse(const Vector3 &p_impulse) {
  178. angular_velocity += _inv_inertia_tensor.xform(p_impulse);
  179. }
  180. _FORCE_INLINE_ void apply_bias_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3(), real_t p_max_delta_av = -1.0) {
  181. biased_linear_velocity += p_impulse * _inv_mass;
  182. if (p_max_delta_av != 0.0) {
  183. Vector3 delta_av = _inv_inertia_tensor.xform((p_position - center_of_mass).cross(p_impulse));
  184. if (p_max_delta_av > 0 && delta_av.length() > p_max_delta_av) {
  185. delta_av = delta_av.normalized() * p_max_delta_av;
  186. }
  187. biased_angular_velocity += delta_av;
  188. }
  189. }
  190. _FORCE_INLINE_ void apply_bias_torque_impulse(const Vector3 &p_impulse) {
  191. biased_angular_velocity += _inv_inertia_tensor.xform(p_impulse);
  192. }
  193. _FORCE_INLINE_ void apply_central_force(const Vector3 &p_force) {
  194. applied_force += p_force;
  195. }
  196. _FORCE_INLINE_ void apply_force(const Vector3 &p_force, const Vector3 &p_position = Vector3()) {
  197. applied_force += p_force;
  198. applied_torque += (p_position - center_of_mass).cross(p_force);
  199. }
  200. _FORCE_INLINE_ void apply_torque(const Vector3 &p_torque) {
  201. applied_torque += p_torque;
  202. }
  203. _FORCE_INLINE_ void add_constant_central_force(const Vector3 &p_force) {
  204. constant_force += p_force;
  205. }
  206. _FORCE_INLINE_ void add_constant_force(const Vector3 &p_force, const Vector3 &p_position = Vector3()) {
  207. constant_force += p_force;
  208. constant_torque += (p_position - center_of_mass).cross(p_force);
  209. }
  210. _FORCE_INLINE_ void add_constant_torque(const Vector3 &p_torque) {
  211. constant_torque += p_torque;
  212. }
  213. void set_constant_force(const Vector3 &p_force) { constant_force = p_force; }
  214. Vector3 get_constant_force() const { return constant_force; }
  215. void set_constant_torque(const Vector3 &p_torque) { constant_torque = p_torque; }
  216. Vector3 get_constant_torque() const { return constant_torque; }
  217. void set_active(bool p_active);
  218. _FORCE_INLINE_ bool is_active() const { return active; }
  219. _FORCE_INLINE_ void wakeup() {
  220. if ((!get_space()) || mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  221. return;
  222. }
  223. set_active(true);
  224. }
  225. void set_param(PhysicsServer3D::BodyParameter p_param, const Variant &p_value);
  226. Variant get_param(PhysicsServer3D::BodyParameter p_param) const;
  227. void set_mode(PhysicsServer3D::BodyMode p_mode);
  228. PhysicsServer3D::BodyMode get_mode() const;
  229. void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant);
  230. Variant get_state(PhysicsServer3D::BodyState p_state) const;
  231. _FORCE_INLINE_ void set_continuous_collision_detection(bool p_enable) { continuous_cd = p_enable; }
  232. _FORCE_INLINE_ bool is_continuous_collision_detection_enabled() const { return continuous_cd; }
  233. void set_space(GodotSpace3D *p_space) override;
  234. void update_mass_properties();
  235. void reset_mass_properties();
  236. _FORCE_INLINE_ real_t get_inv_mass() const { return _inv_mass; }
  237. _FORCE_INLINE_ const Vector3 &get_inv_inertia() const { return _inv_inertia; }
  238. _FORCE_INLINE_ const Basis &get_inv_inertia_tensor() const { return _inv_inertia_tensor; }
  239. _FORCE_INLINE_ real_t get_friction() const { return friction; }
  240. _FORCE_INLINE_ real_t get_bounce() const { return bounce; }
  241. void set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool lock);
  242. bool is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const;
  243. void integrate_forces(real_t p_step);
  244. void integrate_velocities(real_t p_step);
  245. _FORCE_INLINE_ Vector3 get_velocity_in_local_point(const Vector3 &rel_pos) const {
  246. return linear_velocity + angular_velocity.cross(rel_pos - center_of_mass);
  247. }
  248. _FORCE_INLINE_ real_t compute_impulse_denominator(const Vector3 &p_pos, const Vector3 &p_normal) const {
  249. Vector3 r0 = p_pos - get_transform().origin - center_of_mass;
  250. Vector3 c0 = (r0).cross(p_normal);
  251. Vector3 vec = (_inv_inertia_tensor.xform_inv(c0)).cross(r0);
  252. return _inv_mass + p_normal.dot(vec);
  253. }
  254. _FORCE_INLINE_ real_t compute_angular_impulse_denominator(const Vector3 &p_axis) const {
  255. return p_axis.dot(_inv_inertia_tensor.xform_inv(p_axis));
  256. }
  257. //void simulate_motion(const Transform3D& p_xform,real_t p_step);
  258. void call_queries();
  259. void wakeup_neighbours();
  260. bool sleep_test(real_t p_step);
  261. GodotBody3D();
  262. ~GodotBody3D();
  263. };
  264. //add contact inline
  265. void GodotBody3D::add_contact(const Vector3 &p_local_pos, const Vector3 &p_local_normal, real_t p_depth, int p_local_shape, const Vector3 &p_local_velocity_at_pos, 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, const Vector3 &p_impulse) {
  266. int c_max = contacts.size();
  267. if (c_max == 0) {
  268. return;
  269. }
  270. Contact *c = contacts.ptrw();
  271. int idx = -1;
  272. if (contact_count < c_max) {
  273. idx = contact_count++;
  274. } else {
  275. real_t least_depth = 1e20;
  276. int least_deep = -1;
  277. for (int i = 0; i < c_max; i++) {
  278. if (i == 0 || c[i].depth < least_depth) {
  279. least_deep = i;
  280. least_depth = c[i].depth;
  281. }
  282. }
  283. if (least_deep >= 0 && least_depth < p_depth) {
  284. idx = least_deep;
  285. }
  286. if (idx == -1) {
  287. return; //none least deepe than this
  288. }
  289. }
  290. c[idx].local_pos = p_local_pos;
  291. c[idx].local_normal = p_local_normal;
  292. c[idx].local_velocity_at_pos = p_local_velocity_at_pos;
  293. c[idx].depth = p_depth;
  294. c[idx].local_shape = p_local_shape;
  295. c[idx].collider_pos = p_collider_pos;
  296. c[idx].collider_shape = p_collider_shape;
  297. c[idx].collider_instance_id = p_collider_instance_id;
  298. c[idx].collider = p_collider;
  299. c[idx].collider_velocity_at_pos = p_collider_velocity_at_pos;
  300. c[idx].impulse = p_impulse;
  301. }
  302. #endif // GODOT_BODY_3D_H