I've created a cloth simulation using glium to render the cloth and I'm running into an issue where my single-threaded solution is faster than the multi-threaded threaded version. Here is the single threaded method:
let mut forces = vec![vec![(0.0, 0.0); self.points[0].len()]; self.points.len()];
for (i, row) in self.points.iter().enumerate() {
for (j, point) in row.iter().enumerate() {
let mut total_force_x = 0.0;
let mut total_force_y = 0.0;
//spring and damper
for spring in &self.springs {
let point1 = &self.points[spring.p1.0][spring.p1.1];
let point2 = &self.points[spring.p2.0][spring.p2.1];
let dx = point2.x - point1.x;
let dy = point2.y - point1.y;
let distance = (dx * dx + dy * dy).sqrt();
let magnitude = spring.spring_coeff * (distance - spring.rest_length);
let spring_force_x = (magnitude * dx) / distance;
let spring_force_y = (magnitude * dy) / distance;
let damping_force_x = -point.vx * spring.damp_coeff;
let damping_force_y = -point.vy * spring.damp_coeff;
if point1.x == point.x && point1.y == point.y {
total_force_x += spring_force_x + damping_force_x;
total_force_y += spring_force_y + damping_force_y;
} else if point2.x == point.x && point2.y == point.y {
total_force_x -= spring_force_x - damping_force_x;
total_force_y -= spring_force_y - damping_force_y;
}
}```