| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "jolt_object_3d.h" |
| 34 |
|
| 35 |
#include "servers/physics_3d/physics_server_3d.h" |
| 36 |
|
| 37 |
#include "Jolt/Jolt.h" |
| 38 |
|
| 39 |
#include "Jolt/Physics/SoftBody/SoftBodyCreationSettings.h" |
| 40 |
#include "Jolt/Physics/SoftBody/SoftBodySharedSettings.h" |
| 41 |
|
| 42 |
class JoltSpace3D; |
| 43 |
|
| 44 |
class JoltSoftBody3D final : public JoltObject3D { |
| 45 |
HashSet<int> pinned_vertices; |
| 46 |
LocalVector<RID> exceptions; |
| 47 |
LocalVector<Vector3> normals; |
| 48 |
|
| 49 |
RID mesh; |
| 50 |
LocalVector<int> mesh_to_physics; |
| 51 |
|
| 52 |
JPH::SoftBodyCreationSettings *jolt_settings = new JPH::SoftBodyCreationSettings(); |
| 53 |
|
| 54 |
float mass = 0.0f; |
| 55 |
float pressure = 0.0f; |
| 56 |
float linear_damping = 0.01f; |
| 57 |
float stiffness_coefficient = 0.5f; |
| 58 |
float shrinking_factor = 0.0f; |
| 59 |
|
| 60 |
int simulation_precision = 5; |
| 61 |
|
| 62 |
virtual JPH::BroadPhaseLayer _get_broad_phase_layer() const override; |
| 63 |
virtual JPH::ObjectLayer _get_object_layer() const override; |
| 64 |
|
| 65 |
virtual void _space_changing() override; |
| 66 |
virtual void _space_changed() override; |
| 67 |
|
| 68 |
virtual void _add_to_space() override; |
| 69 |
|
| 70 |
JPH::SoftBodySharedSettings *_create_shared_settings(); |
| 71 |
|
| 72 |
void _update_mass(); |
| 73 |
void _update_pressure(); |
| 74 |
void _update_damping(); |
| 75 |
void _update_simulation_precision(); |
| 76 |
void _update_group_filter(); |
| 77 |
|
| 78 |
void _try_rebuild(); |
| 79 |
|
| 80 |
void _mesh_changed(); |
| 81 |
void _simulation_precision_changed(); |
| 82 |
void _mass_changed(); |
| 83 |
void _pressure_changed(); |
| 84 |
void _damping_changed(); |
| 85 |
void _pins_changed(); |
| 86 |
void _vertices_changed(); |
| 87 |
void _exceptions_changed(); |
| 88 |
void _motion_changed(); |
| 89 |
|
| 90 |
public: |
| 91 |
JoltSoftBody3D(); |
| 92 |
virtual ~JoltSoftBody3D() override; |
| 93 |
|
| 94 |
void add_collision_exception(const RID &p_excepted_body); |
| 95 |
void remove_collision_exception(const RID &p_excepted_body); |
| 96 |
bool has_collision_exception(const RID &p_excepted_body) const; |
| 97 |
|
| 98 |
const LocalVector<RID> &get_collision_exceptions() const { return exceptions; } |
| 99 |
|
| 100 |
virtual bool can_interact_with(const JoltBody3D &p_other) const override; |
| 101 |
virtual bool can_interact_with(const JoltSoftBody3D &p_other) const override; |
| 102 |
virtual bool can_interact_with(const JoltArea3D &p_other) const override; |
| 103 |
|
| 104 |
virtual bool reports_contacts() const override { return false; } |
| 105 |
|
| 106 |
virtual Vector3 get_velocity_at_position(const Vector3 &p_position) const override; |
| 107 |
|
| 108 |
void set_mesh(const RID &p_mesh); |
| 109 |
|
| 110 |
bool is_pickable() const { return pickable; } |
| 111 |
void set_pickable(bool p_enabled) { pickable = p_enabled; } |
| 112 |
|
| 113 |
bool is_sleeping() const; |
| 114 |
void set_is_sleeping(bool p_enabled); |
| 115 |
|
| 116 |
bool is_sleep_allowed() const; |
| 117 |
void set_is_sleep_allowed(bool p_enabled); |
| 118 |
|
| 119 |
void put_to_sleep() { set_is_sleeping(true); } |
| 120 |
void wake_up() { set_is_sleeping(false); } |
| 121 |
|
| 122 |
int get_simulation_precision() const { return simulation_precision; } |
| 123 |
void set_simulation_precision(int p_precision); |
| 124 |
|
| 125 |
float get_mass() const { return mass; } |
| 126 |
void set_mass(float p_mass); |
| 127 |
|
| 128 |
float get_stiffness_coefficient() const; |
| 129 |
void set_stiffness_coefficient(float p_coefficient); |
| 130 |
|
| 131 |
float get_shrinking_factor() const; |
| 132 |
void set_shrinking_factor(float p_shrinking_factor); |
| 133 |
|
| 134 |
float get_pressure() const { return pressure; } |
| 135 |
void set_pressure(float p_pressure); |
| 136 |
|
| 137 |
float get_linear_damping() const { return linear_damping; } |
| 138 |
void set_linear_damping(float p_damping); |
| 139 |
|
| 140 |
float get_drag() const; |
| 141 |
void set_drag(float p_drag); |
| 142 |
|
| 143 |
Variant get_state(PhysicsServer3D::BodyState p_state) const; |
| 144 |
void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_value); |
| 145 |
|
| 146 |
Transform3D get_transform() const; |
| 147 |
void set_transform(const Transform3D &p_transform); |
| 148 |
|
| 149 |
AABB get_bounds() const; |
| 150 |
|
| 151 |
void update_rendering_server(PhysicsServer3DRenderingServerHandler *p_rendering_server_handler); |
| 152 |
|
| 153 |
Vector3 get_vertex_position(int p_index); |
| 154 |
void set_vertex_position(int p_index, const Vector3 &p_position); |
| 155 |
|
| 156 |
void pin_vertex(int p_index); |
| 157 |
void unpin_vertex(int p_index); |
| 158 |
|
| 159 |
void unpin_all_vertices(); |
| 160 |
|
| 161 |
bool is_vertex_pinned(int p_index) const; |
| 162 |
|
| 163 |
void apply_vertex_impulse(int p_index, const Vector3 &p_impulse); |
| 164 |
void apply_vertex_force(int p_index, const Vector3 &p_force); |
| 165 |
void apply_central_impulse(const Vector3 &p_impulse); |
| 166 |
void apply_central_force(const Vector3 &p_force); |
| 167 |
}; |