Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/jolt_physics/objects/jolt_soft_body_3d.h
10278 views
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
31
#pragma once
32
33
#include "jolt_object_3d.h"
34
35
#include "servers/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
struct Shared {
46
LocalVector<int> mesh_to_physics;
47
JPH::Ref<JPH::SoftBodySharedSettings> settings = new JPH::SoftBodySharedSettings();
48
int ref_count = 1;
49
};
50
51
inline static HashMap<RID, Shared> mesh_to_shared;
52
53
HashSet<int> pinned_vertices;
54
LocalVector<RID> exceptions;
55
LocalVector<Vector3> normals;
56
57
const Shared *shared = nullptr;
58
59
RID mesh;
60
61
JPH::SoftBodyCreationSettings *jolt_settings = new JPH::SoftBodyCreationSettings();
62
63
float mass = 0.0f;
64
float pressure = 0.0f;
65
float linear_damping = 0.01f;
66
float stiffness_coefficient = 0.5f;
67
float shrinking_factor = 0.0f;
68
69
int simulation_precision = 5;
70
71
virtual JPH::BroadPhaseLayer _get_broad_phase_layer() const override;
72
virtual JPH::ObjectLayer _get_object_layer() const override;
73
74
virtual void _space_changing() override;
75
virtual void _space_changed() override;
76
77
virtual void _add_to_space() override;
78
79
bool _ref_shared_data();
80
void _deref_shared_data();
81
82
void _update_mass();
83
void _update_pressure();
84
void _update_damping();
85
void _update_simulation_precision();
86
void _update_group_filter();
87
88
void _try_rebuild();
89
90
void _mesh_changed();
91
void _simulation_precision_changed();
92
void _mass_changed();
93
void _pressure_changed();
94
void _damping_changed();
95
void _pins_changed();
96
void _vertices_changed();
97
void _exceptions_changed();
98
void _motion_changed();
99
100
public:
101
JoltSoftBody3D();
102
virtual ~JoltSoftBody3D() override;
103
104
bool in_space() const;
105
106
void add_collision_exception(const RID &p_excepted_body);
107
void remove_collision_exception(const RID &p_excepted_body);
108
bool has_collision_exception(const RID &p_excepted_body) const;
109
110
const LocalVector<RID> &get_collision_exceptions() const { return exceptions; }
111
112
virtual bool can_interact_with(const JoltBody3D &p_other) const override;
113
virtual bool can_interact_with(const JoltSoftBody3D &p_other) const override;
114
virtual bool can_interact_with(const JoltArea3D &p_other) const override;
115
116
virtual bool reports_contacts() const override { return false; }
117
118
virtual Vector3 get_velocity_at_position(const Vector3 &p_position) const override;
119
120
void set_mesh(const RID &p_mesh);
121
122
bool is_pickable() const { return pickable; }
123
void set_pickable(bool p_enabled) { pickable = p_enabled; }
124
125
bool is_sleeping() const;
126
void set_is_sleeping(bool p_enabled);
127
128
bool is_sleep_allowed() const;
129
void set_is_sleep_allowed(bool p_enabled);
130
131
void put_to_sleep() { set_is_sleeping(true); }
132
void wake_up() { set_is_sleeping(false); }
133
134
int get_simulation_precision() const { return simulation_precision; }
135
void set_simulation_precision(int p_precision);
136
137
float get_mass() const { return mass; }
138
void set_mass(float p_mass);
139
140
float get_stiffness_coefficient() const;
141
void set_stiffness_coefficient(float p_coefficient);
142
143
float get_shrinking_factor() const;
144
void set_shrinking_factor(float p_shrinking_factor);
145
146
float get_pressure() const { return pressure; }
147
void set_pressure(float p_pressure);
148
149
float get_linear_damping() const { return linear_damping; }
150
void set_linear_damping(float p_damping);
151
152
float get_drag() const;
153
void set_drag(float p_drag);
154
155
Variant get_state(PhysicsServer3D::BodyState p_state) const;
156
void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_value);
157
158
Transform3D get_transform() const;
159
void set_transform(const Transform3D &p_transform);
160
161
AABB get_bounds() const;
162
163
void update_rendering_server(PhysicsServer3DRenderingServerHandler *p_rendering_server_handler);
164
165
Vector3 get_vertex_position(int p_index);
166
void set_vertex_position(int p_index, const Vector3 &p_position);
167
168
void pin_vertex(int p_index);
169
void unpin_vertex(int p_index);
170
171
void unpin_all_vertices();
172
173
bool is_vertex_pinned(int p_index) const;
174
175
void apply_vertex_impulse(int p_index, const Vector3 &p_impulse);
176
void apply_vertex_force(int p_index, const Vector3 &p_force);
177
void apply_central_impulse(const Vector3 &p_impulse);
178
void apply_central_force(const Vector3 &p_force);
179
};
180
181