jolt_soft_body_3d.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************/
  2. /* jolt_soft_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. #pragma once
  31. #include "jolt_object_3d.h"
  32. #include "servers/physics_server_3d.h"
  33. #include "Jolt/Jolt.h"
  34. #include "Jolt/Physics/SoftBody/SoftBodyCreationSettings.h"
  35. #include "Jolt/Physics/SoftBody/SoftBodySharedSettings.h"
  36. class JoltSpace3D;
  37. class JoltSoftBody3D final : public JoltObject3D {
  38. struct Shared {
  39. LocalVector<int> mesh_to_physics;
  40. JPH::Ref<JPH::SoftBodySharedSettings> settings = new JPH::SoftBodySharedSettings();
  41. int ref_count = 1;
  42. };
  43. inline static HashMap<RID, Shared> mesh_to_shared;
  44. HashSet<int> pinned_vertices;
  45. LocalVector<RID> exceptions;
  46. LocalVector<Vector3> normals;
  47. const Shared *shared = nullptr;
  48. RID mesh;
  49. JPH::SoftBodyCreationSettings *jolt_settings = new JPH::SoftBodyCreationSettings();
  50. float mass = 0.0f;
  51. float pressure = 0.0f;
  52. float linear_damping = 0.01f;
  53. float stiffness_coefficient = 0.5f;
  54. float shrinking_factor = 0.0f;
  55. int simulation_precision = 5;
  56. virtual JPH::BroadPhaseLayer _get_broad_phase_layer() const override;
  57. virtual JPH::ObjectLayer _get_object_layer() const override;
  58. virtual void _space_changing() override;
  59. virtual void _space_changed() override;
  60. virtual void _add_to_space() override;
  61. bool _ref_shared_data();
  62. void _deref_shared_data();
  63. void _update_mass();
  64. void _update_pressure();
  65. void _update_damping();
  66. void _update_simulation_precision();
  67. void _update_group_filter();
  68. void _try_rebuild();
  69. void _mesh_changed();
  70. void _simulation_precision_changed();
  71. void _mass_changed();
  72. void _pressure_changed();
  73. void _damping_changed();
  74. void _pins_changed();
  75. void _vertices_changed();
  76. void _exceptions_changed();
  77. public:
  78. JoltSoftBody3D();
  79. virtual ~JoltSoftBody3D() override;
  80. bool in_space() const;
  81. void add_collision_exception(const RID &p_excepted_body);
  82. void remove_collision_exception(const RID &p_excepted_body);
  83. bool has_collision_exception(const RID &p_excepted_body) const;
  84. const LocalVector<RID> &get_collision_exceptions() const { return exceptions; }
  85. virtual bool can_interact_with(const JoltBody3D &p_other) const override;
  86. virtual bool can_interact_with(const JoltSoftBody3D &p_other) const override;
  87. virtual bool can_interact_with(const JoltArea3D &p_other) const override;
  88. virtual bool reports_contacts() const override { return false; }
  89. virtual Vector3 get_velocity_at_position(const Vector3 &p_position) const override;
  90. void set_mesh(const RID &p_mesh);
  91. bool is_pickable() const { return pickable; }
  92. void set_pickable(bool p_enabled) { pickable = p_enabled; }
  93. bool is_sleeping() const;
  94. void set_is_sleeping(bool p_enabled);
  95. bool is_sleep_allowed() const;
  96. void set_is_sleep_allowed(bool p_enabled);
  97. void put_to_sleep() { set_is_sleeping(true); }
  98. void wake_up() { set_is_sleeping(false); }
  99. int get_simulation_precision() const { return simulation_precision; }
  100. void set_simulation_precision(int p_precision);
  101. float get_mass() const { return mass; }
  102. void set_mass(float p_mass);
  103. float get_stiffness_coefficient() const;
  104. void set_stiffness_coefficient(float p_coefficient);
  105. float get_shrinking_factor() const;
  106. void set_shrinking_factor(float p_shrinking_factor);
  107. float get_pressure() const { return pressure; }
  108. void set_pressure(float p_pressure);
  109. float get_linear_damping() const { return linear_damping; }
  110. void set_linear_damping(float p_damping);
  111. float get_drag() const;
  112. void set_drag(float p_drag);
  113. Variant get_state(PhysicsServer3D::BodyState p_state) const;
  114. void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_value);
  115. Transform3D get_transform() const;
  116. void set_transform(const Transform3D &p_transform);
  117. AABB get_bounds() const;
  118. void update_rendering_server(PhysicsServer3DRenderingServerHandler *p_rendering_server_handler);
  119. Vector3 get_vertex_position(int p_index);
  120. void set_vertex_position(int p_index, const Vector3 &p_position);
  121. void pin_vertex(int p_index);
  122. void unpin_vertex(int p_index);
  123. void unpin_all_vertices();
  124. bool is_vertex_pinned(int p_index) const;
  125. void apply_vertex_impulse(int p_index, const Vector3 &p_impulse);
  126. void apply_vertex_force(int p_index, const Vector3 &p_force);
  127. void apply_central_impulse(const Vector3 &p_impulse);
  128. void apply_central_force(const Vector3 &p_force);
  129. };