bevy_silk 0.10.0

Cloth physics implementation in bevy
Documentation
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::{
    stick::{StickGeneration, StickLen, StickMode},
    vertex_anchor::VertexAnchor,
};
use bevy::{
    ecs::prelude::Component,
    log,
    math::{Mat4, Vec3},
    platform::collections::HashMap,
    prelude::{Entity, GlobalTransform},
};

/// A stick is defined by the two ids of the connectecte points
pub type StickId = [usize; 2];

macro_rules! get_point {
    ($id:expr, $points:expr, $anchored_points:expr) => {
        match $points.get($id) {
            None => {
                log::warn!("Failed to retrieve a Cloth point at index {}", $id);
                continue;
            }
            Some(p) => (*p, $anchored_points.contains_key(&$id)),
        }
    };
}

/// Cloth component. Do not insert it directly, use [`ClothBuilder`] instead.
///
/// [`ClothBuilder`]: crate::prelude::ClothBuilder
#[derive(Debug, Clone, Component, Default)]
#[must_use]
pub struct Cloth {
    /// cloth points unaffected by physics and following an anchor
    /// The key is the point index and the value is a tuple with:
    /// - 0: The [`VertexAnchor`] anchor
    /// - 1: The initial local space vertex position
    pub anchored_points: HashMap<usize, (VertexAnchor, Vec3)>,
    /// Current Cloth points 3D positions in world space
    pub current_point_positions: Vec<Vec3>,
    /// Old Cloth points 3D positions in world space
    pub previous_point_positions: Vec<Vec3>,
    /// Cloth sticks lengths
    ///
    /// * key: array of the two connected points indexes
    /// * value: the target distance between the points
    ///
    /// Note: this field will be automatically populated from mesh data
    pub stick_lengths: HashMap<StickId, f32>,
    /// Cloth sticks behaviour modes
    ///
    /// * key: array of the two connected points indexes
    /// * value: the stick mode
    pub stick_modes: HashMap<StickId, StickMode>,
}

impl Cloth {
    /// Computes the new local vertex positions of the cloth mesh
    ///
    /// # Arguments
    ///
    /// * `transform` - the `GlobalTransform` associated to the cloth entity
    #[must_use]
    pub fn compute_vertex_positions(
        &self,
        transform: &GlobalTransform,
    ) -> impl ExactSizeIterator<Item = Vec3> + '_ {
        let matrix = transform.to_matrix().inverse();

        // World space positions..
        self.current_point_positions
            .iter()
            // ..computed to local space
            .map(move |p| matrix.transform_point3(*p))
    }

    /// Creates a new cloth from a mesh. Points positions will be directly
    /// extracted from the given vertex positions and the sticks will be
    /// extracted from the given `indices` (triangles) according to
    /// the associated [`StickGeneration`] and [`StickLen`].
    ///
    /// # Arguments
    ///
    /// * `vertex_positions` - the mesh vertex positions
    /// * `indices` - the mesh indices
    /// * `anchored_points` - the pinned vertex position indices
    /// * `stick_generation` - The stick generation mode
    /// * `stick_len` - The stick length option
    /// * `transform_matrix` - the transform matrix of the associated
    ///   `GlobalTransform`
    ///
    /// # Panics
    ///
    /// May panic if `anchored_points` contains an out of bounds vertex id.
    pub fn new(
        vertex_positions: &[Vec3],
        indices: &[u32],
        anchored_points: HashMap<usize, VertexAnchor>,
        stick_generation: StickGeneration,
        stick_len: StickLen,
        stick_mode: StickMode,
        transform_matrix: &Mat4,
    ) -> Self {
        let anchored_points = anchored_points
            .into_iter()
            .map(|(i, anchor)| {
                let pos = vertex_positions
                    .get(i)
                    .unwrap_or_else(|| panic!("Anchored vertex id {i} is out of bounds"));
                (i, (anchor, *pos))
            })
            .collect();
        let positions: Vec<Vec3> = vertex_positions
            .iter()
            .map(|p| transform_matrix.transform_point3(*p))
            .collect();
        let indices: Vec<usize> = indices.iter().map(|i| *i as usize).collect();
        if !indices.len().is_multiple_of(3) {
            log::error!("Mesh indices count is not a multiple of 3, some indices will be skipped",);
        }
        let mut stick_lengths = HashMap::with_capacity(indices.len() / 3);
        for truple in indices.chunks_exact(3) {
            let [a, b, c] = [truple[0], truple[1], truple[2]];
            let [p_a, p_b, p_c] = [positions[a], positions[b], positions[c]];
            if !stick_lengths.contains_key(&[b, a]) {
                stick_lengths.insert([a, b], stick_len.get_len(p_a, p_b));
            }
            if !stick_lengths.contains_key(&[c, b]) {
                stick_lengths.insert([b, c], stick_len.get_len(p_b, p_c));
            }
            if stick_generation == StickGeneration::Triangles
                && !stick_lengths.contains_key(&[a, c])
            {
                stick_lengths.insert([c, a], stick_len.get_len(p_c, p_a));
            }
        }
        let stick_modes = stick_lengths.keys().map(|id| (*id, stick_mode)).collect();
        Self {
            anchored_points,
            current_point_positions: positions.clone(),
            previous_point_positions: positions,
            stick_lengths,
            stick_modes,
        }
    }

    /// Changes the stick behaviour to `new_mode` for `sticks`
    pub fn edit_stick_modes(&mut self, sticks: &[StickId], new_mode: StickMode) {
        log::debug!("Editing {} sticks: {new_mode:#?}", sticks.len());
        for id in sticks {
            self.stick_modes.get_mut(id).map_or_else(
                || {
                    log::warn!("Attempted to edit missing stick `{id:?}` behaviour");
                },
                |mode| {
                    *mode = new_mode;
                },
            );
        }
    }

    /// Adds an extra point to the cloth (Not included in the base mesh) and
    /// returns its id and associated stick ids.
    pub fn add_point(
        &mut self,
        pos: Vec3,
        stick_mode: StickMode,
        anchor: Option<VertexAnchor>,
        transform_matrix: &Mat4,
        connects_to: impl Fn(usize, &Vec3) -> bool,
    ) -> (usize, Vec<StickId>) {
        let center = transform_matrix.transform_point3(pos);
        self.current_point_positions.push(center);
        self.previous_point_positions.push(center);
        let id = self.current_point_positions.len().saturating_sub(1);
        let sticks: Vec<_> = self
            .current_point_positions
            .iter()
            .enumerate()
            .filter(|(i, p)| connects_to(*i, p))
            .map(|(i, p)| {
                let stick_id = [id, i];
                self.stick_modes.insert(stick_id, stick_mode);
                self.stick_lengths.insert(stick_id, p.distance(center));
                stick_id
            })
            .collect();
        log::debug!(
            "Added custom point {pos:?} and {} sticks with to it: {stick_mode:#?}",
            sticks.len()
        );
        if let Some(anchor) = anchor {
            self.anchored_points.insert(id, (anchor, pos));
        }
        (id, sticks)
    }

    /// Solves cloth points collisions, moving them outside of colliders
    ///
    /// # Arguments
    ///
    /// * `solve_point` - function taking a cloth point and returning the new
    ///   solved point
    pub fn solve_collisions(&mut self, solve_point: impl Fn(&Vec3) -> Option<Vec3>) {
        for (point, new_point) in self
            .current_point_positions
            .iter_mut()
            .enumerate()
            .filter(|(i, _p)| !self.anchored_points.contains_key(i))
            .filter_map(|(_i, p)| solve_point(p).map(|np| (p, np)))
        {
            *point = new_point;
        }
    }

    /// Updates the cloth anchored points
    ///
    /// # Arguments
    ///
    /// * `transform` - The `GlobalTransform` associated to the cloth entity
    /// * `anchor_query` - A function allowing to retrieve the `GlobalTransform`
    ///   of a given entity
    pub fn update_anchored_points<'a>(
        &mut self,
        transform: &GlobalTransform,
        anchor_query: impl Fn(Entity) -> Option<&'a GlobalTransform>,
    ) {
        for (i, (anchor, inital_pos)) in &self.anchored_points {
            self.current_point_positions[*i] =
                anchor.get_position(*inital_pos, transform, &anchor_query);
        }
    }

    /// Updates the cloth points according to their own velocity and external
    /// friction and acceleration
    ///
    /// # Arguments
    ///
    /// * `friction` - Friction to apply to the points velocity
    /// * `acceleration` - Global acceleration force (gravity, wind, etc)
    pub fn update_points(&mut self, friction: f32, acceleration: Vec3) {
        let position_cache = self.current_point_positions.clone();
        for (i, point) in self.current_point_positions.iter_mut().enumerate() {
            if !self.anchored_points.contains_key(&i) {
                let velocity = self
                    .previous_point_positions
                    .get(i)
                    .map_or(Vec3::ZERO, |prev| *point - *prev);
                *point += velocity * friction + acceleration * friction;
            }
        }
        self.previous_point_positions = position_cache;
    }

    /// Applies the cloth sticks constraints
    ///
    /// # Arguments
    ///
    /// * `depth` - Number of sticks constraint iterations
    pub fn update_sticks(&mut self, depth: u8) {
        for _ in 0..depth {
            for ([id_a, id_b], target_len) in &self.stick_lengths {
                let (position_a, fixed_a) =
                    get_point!(*id_a, self.current_point_positions, self.anchored_points);
                let (position_b, fixed_b) =
                    get_point!(*id_b, self.current_point_positions, self.anchored_points);
                if fixed_a && fixed_b {
                    continue;
                }
                let target_len = match self.stick_modes[&[*id_a, *id_b]] {
                    StickMode::Fixed => *target_len,
                    StickMode::Spring {
                        min_percent,
                        max_percent,
                    } => {
                        let dist = position_a.distance(position_b) / *target_len;
                        if dist < min_percent {
                            *target_len * min_percent
                        } else if dist > max_percent {
                            *target_len * max_percent
                        } else {
                            continue;
                        }
                    }
                };
                let center = (position_b + position_a) / 2.0;
                let direction = match (position_b - position_a).try_normalize() {
                    None => {
                        log::warn!(
                            "Failed handle stick between points {} and {} which are too close to \
                             each other",
                            *id_a,
                            *id_b
                        );
                        continue;
                    }
                    Some(dir) => dir * target_len / 2.0,
                };
                if !fixed_a {
                    self.current_point_positions[*id_a] = if fixed_b {
                        position_b - direction * 2.0
                    } else {
                        center - direction
                    };
                }
                if !fixed_b {
                    self.current_point_positions[*id_b] = if fixed_a {
                        position_a + direction * 2.0
                    } else {
                        center + direction
                    };
                }
            }
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::mesh::rectangle_mesh;

    mod init_from_mesh {
        use super::*;
        use crate::components::cloth_rendering::ClothRendering;
        use bevy::transform::prelude::Transform;

        fn expected_stick_len(
            len: usize,
            generation: StickGeneration,
            (size_x, size_y): (usize, usize),
        ) {
            match generation {
                StickGeneration::Quads => {
                    assert_eq!(len, (size_x - 1) * size_y + (size_y - 1) * size_x);
                }
                StickGeneration::Triangles => {
                    assert_eq!(
                        len,
                        (size_x - 1) * size_y + (size_y - 1) * size_x + (size_x - 1) * (size_y - 1)
                    );
                }
            }
        }

        #[test]
        fn works_with_quads() {
            let mesh = rectangle_mesh((100, 100), (Vec3::X, -Vec3::Y), Vec3::Z);
            let matrix = Transform::default().to_matrix();
            let cloth_rendering = ClothRendering::init(&mesh, Default::default()).unwrap();
            let cloth = Cloth::new(
                &cloth_rendering.vertex_positions,
                &cloth_rendering.indices,
                Default::default(),
                StickGeneration::Quads,
                StickLen::Auto,
                StickMode::Fixed,
                &matrix,
            );
            assert_eq!(cloth.current_point_positions.len(), 100 * 100);
            assert_eq!(cloth.previous_point_positions.len(), 100 * 100);
            expected_stick_len(
                cloth.stick_lengths.len(),
                StickGeneration::Quads,
                (100, 100),
            );
        }

        #[test]
        fn works_with_quads_2() {
            let mesh = rectangle_mesh((66, 42), (Vec3::X, -Vec3::Y), Vec3::Z);
            let matrix = Transform::default().to_matrix();
            let cloth_rendering = ClothRendering::init(&mesh, Default::default()).unwrap();
            let cloth = Cloth::new(
                &cloth_rendering.vertex_positions,
                &cloth_rendering.indices,
                Default::default(),
                StickGeneration::Quads,
                StickLen::Auto,
                StickMode::Fixed,
                &matrix,
            );
            assert_eq!(cloth.current_point_positions.len(), 66 * 42);
            assert_eq!(cloth.previous_point_positions.len(), 66 * 42);
            expected_stick_len(cloth.stick_lengths.len(), StickGeneration::Quads, (66, 42));
        }

        #[test]
        fn works_with_triangles() {
            let mesh = rectangle_mesh((100, 100), (Vec3::X, -Vec3::Y), Vec3::Z);
            let matrix = Transform::default().to_matrix();
            let cloth_rendering = ClothRendering::init(&mesh, Default::default()).unwrap();
            let cloth = Cloth::new(
                &cloth_rendering.vertex_positions,
                &cloth_rendering.indices,
                Default::default(),
                StickGeneration::Triangles,
                StickLen::Auto,
                StickMode::Fixed,
                &matrix,
            );
            assert_eq!(cloth.current_point_positions.len(), 100 * 100);
            assert_eq!(cloth.previous_point_positions.len(), 100 * 100);
            expected_stick_len(
                cloth.stick_lengths.len(),
                StickGeneration::Triangles,
                (100, 100),
            );
        }

        #[test]
        fn works_with_triangles_2() {
            let mesh = rectangle_mesh((66, 42), (Vec3::X, -Vec3::Y), Vec3::Z);
            let matrix = Transform::default().to_matrix();
            let cloth_rendering = ClothRendering::init(&mesh, Default::default()).unwrap();
            let cloth = Cloth::new(
                &cloth_rendering.vertex_positions,
                &cloth_rendering.indices,
                Default::default(),
                StickGeneration::Triangles,
                StickLen::Auto,
                StickMode::Fixed,
                &matrix,
            );
            assert_eq!(cloth.current_point_positions.len(), 66 * 42);
            assert_eq!(cloth.previous_point_positions.len(), 66 * 42);
            expected_stick_len(
                cloth.stick_lengths.len(),
                StickGeneration::Triangles,
                (66, 42),
            );
        }
    }
}