use crate::{JointMotorSpec, NodeCollider, SceneBodySpec, SceneJoint, SceneJointSpec, SceneSpec};
pub fn drop_bowl_scene() -> SceneSpec {
let mut bodies = Vec::new();
for i in 0..5 {
bodies.push(
SceneBodySpec::fixed(
NodeCollider::Ball { radius: 60.0 },
(-280.0 + i as f32 * 140.0, 340.0),
)
.restitution(0.4),
);
}
for i in 0..8 {
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Ball { radius: 22.0 },
(-210.0 + i as f32 * 60.0, -260.0 - (i % 3) as f32 * 40.0),
)
.restitution(0.5),
);
}
SceneSpec {
bodies,
gravity: (0.0, 520.0),
default_tangible: false,
perpetual: false,
joints: Vec::new(),
}
}
pub fn pyramid_scene() -> SceneSpec {
let mut bodies = vec![
SceneBodySpec::fixed(NodeCollider::Square { half: 300.0 }, (0.0, 360.0)).restitution(0.0),
];
for k in 0..9 {
let count = 9 - k;
for i in 0..count {
let x = (i as f32 - (count as f32 - 1.0) / 2.0) * 50.0;
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Square { half: 24.0 },
(x, 36.0 - k as f32 * 48.0),
)
.restitution(0.0),
);
}
}
SceneSpec {
bodies,
gravity: (0.0, 600.0),
default_tangible: false,
perpetual: false,
joints: Vec::new(),
}
}
pub fn domino_scene() -> SceneSpec {
let mut bodies = vec![
SceneBodySpec::fixed(NodeCollider::Square { half: 300.0 }, (0.0, 360.0)).restitution(0.0),
];
let tall = NodeCollider::Hull {
points: vec![(-6.0, -44.0), (6.0, -44.0), (6.0, 44.0), (-6.0, 44.0)],
fallback: 20.0,
};
for i in 0..14 {
let vel = if i == 0 { (90.0, 0.0) } else { (0.0, 0.0) };
bodies.push(
SceneBodySpec::dynamic(tall.clone(), (-260.0 + i as f32 * 40.0, 12.0))
.velocity(vel)
.restitution(0.0),
);
}
SceneSpec {
bodies,
gravity: (0.0, 520.0),
default_tangible: false,
perpetual: false,
joints: Vec::new(),
}
}
pub fn galton_scene() -> SceneSpec {
let mut bodies = Vec::new();
for row in 0..9 {
let n = 4 + row;
for i in 0..n {
let x = (i as f32 - (n as f32 - 1.0) / 2.0) * 60.0;
bodies.push(
SceneBodySpec::fixed(
NodeCollider::Ball { radius: 8.0 },
(x, -200.0 + row as f32 * 45.0),
)
.restitution(0.5),
);
}
}
bodies.push(
SceneBodySpec::fixed(NodeCollider::Square { half: 320.0 }, (0.0, 520.0)).restitution(0.2),
);
for i in 0..30 {
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Ball { radius: 6.0 },
((i % 5) as f32 * 4.0 - 8.0, -320.0 - i as f32 * 12.0),
)
.velocity((0.0, 30.0))
.restitution(0.5),
);
}
SceneSpec {
bodies,
gravity: (0.0, 450.0),
default_tangible: false,
perpetual: false,
joints: Vec::new(),
}
}
pub fn funnel_scene() -> SceneSpec {
let mut bodies = Vec::new();
for i in 0..8 {
let inset = i as f32 * 30.0;
let y = -200.0 + i as f32 * 28.0;
bodies.push(
SceneBodySpec::fixed(NodeCollider::Ball { radius: 30.0 }, (-360.0 + inset, y))
.restitution(0.1),
);
bodies.push(
SceneBodySpec::fixed(NodeCollider::Ball { radius: 30.0 }, (360.0 - inset, y))
.restitution(0.1),
);
}
bodies.push(
SceneBodySpec::fixed(NodeCollider::Square { half: 200.0 }, (0.0, 320.0)).restitution(0.1),
);
for i in 0..80 {
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Ball { radius: 8.0 },
(
((i % 7) as f32 - 3.0) * 16.0,
-340.0 - (i / 7) as f32 * 18.0,
),
)
.velocity((0.0, 20.0))
.restitution(0.2),
);
}
SceneSpec {
bodies,
gravity: (0.0, 500.0),
default_tangible: false,
perpetual: false,
joints: Vec::new(),
}
}
pub fn drift_scene() -> SceneSpec {
let seeds = [
(-360.0, -200.0, 38.0, -22.0),
(340.0, 170.0, -30.0, 26.0),
(-300.0, 200.0, 28.0, -30.0),
(320.0, -210.0, -34.0, 20.0),
(-40.0, -40.0, 30.0, 34.0),
(-200.0, 90.0, 36.0, -18.0),
(220.0, 30.0, -28.0, -32.0),
(60.0, 240.0, 22.0, -38.0),
];
let bodies = seeds
.iter()
.enumerate()
.map(|(i, &(x, y, vx, vy))| {
SceneBodySpec::dynamic(
NodeCollider::Ball {
radius: 26.0 + (i % 4) as f32 * 7.0,
},
(x, y),
)
.velocity((vx, vy))
.restitution(1.0)
})
.collect();
SceneSpec {
bodies,
gravity: (0.0, 0.0),
default_tangible: false,
perpetual: true,
joints: Vec::new(),
}
}
pub fn chain_scene() -> SceneSpec {
let anchor = (-150.0, -120.0);
let link = 48.0;
let mut bodies = vec![SceneBodySpec::fixed(
NodeCollider::Ball { radius: 10.0 },
anchor,
)];
for i in 0..8 {
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Ball { radius: 16.0 },
(anchor.0 + (i as f32 + 1.0) * link, anchor.1),
)
.restitution(0.2),
);
}
let joints = (0..8)
.map(|i| SceneJointSpec {
body_a: i,
body_b: i + 1,
joint: SceneJoint::Rope {
anchor_a: (0.0, 0.0),
anchor_b: (0.0, 0.0),
length: link,
},
})
.collect();
SceneSpec {
bodies,
gravity: (0.0, 520.0),
default_tangible: false,
perpetual: false,
joints,
}
}
pub fn whirlpool_scene() -> SceneSpec {
use std::f32::consts::TAU;
let mut bodies = Vec::new();
for i in 0..20 {
let (ring, twist) = if i < 10 { (110.0, 0.0) } else { (190.0, 0.31) };
let a = (i % 10) as f32 * TAU / 10.0 + twist;
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Ball {
radius: 14.0 + (i % 3) as f32 * 6.0,
},
(ring * a.cos(), ring * a.sin()),
)
.restitution(0.6),
);
}
SceneSpec {
bodies,
gravity: (0.0, 0.0),
default_tangible: false,
perpetual: false,
joints: Vec::new(),
}
}
pub fn fountain_scene() -> SceneSpec {
let bodies = vec![
SceneBodySpec::fixed(NodeCollider::Square { half: 300.0 }, (0.0, 600.0)).restitution(0.1),
];
SceneSpec {
bodies,
gravity: (0.0, 520.0),
default_tangible: false,
perpetual: false,
joints: Vec::new(),
}
}
pub fn cradle_scene() -> SceneSpec {
let rod = 170.0;
let anchor_y = -220.0;
let r = 22.0;
let mut bodies = Vec::new();
for i in 0..5 {
let x = (i as f32 - 2.0) * (2.0 * r);
bodies.push(SceneBodySpec::fixed(
NodeCollider::Ball { radius: 8.0 },
(x, anchor_y),
));
}
for i in 0..5 {
let x = (i as f32 - 2.0) * (2.0 * r);
let vel = if i == 0 { (-340.0, 0.0) } else { (0.0, 0.0) };
bodies.push(
SceneBodySpec::dynamic(NodeCollider::Ball { radius: r }, (x, anchor_y + rod))
.velocity(vel)
.restitution(0.95),
);
}
let joints = (0..5)
.map(|i| SceneJointSpec {
body_a: i,
body_b: 5 + i,
joint: SceneJoint::Revolute {
anchor_a: (0.0, 0.0),
anchor_b: (0.0, -rod),
motor: None,
},
})
.collect();
SceneSpec {
bodies,
gravity: (0.0, 600.0),
default_tangible: false,
perpetual: true,
joints,
}
}
pub fn bridge_scene() -> SceneSpec {
let halfw = 33.0;
let mut bodies = vec![SceneBodySpec::fixed(
NodeCollider::Ball { radius: 10.0 },
(-297.0, 0.0),
)];
let plank = NodeCollider::Hull {
points: vec![(-halfw, -6.0), (halfw, -6.0), (halfw, 6.0), (-halfw, 6.0)],
fallback: 20.0,
};
for i in 0..9 {
bodies.push(
SceneBodySpec::dynamic(plank.clone(), (-264.0 + i as f32 * 66.0, 0.0))
.restitution(0.05),
);
}
bodies.push(SceneBodySpec::fixed(
NodeCollider::Ball { radius: 10.0 },
(297.0, 0.0),
));
bodies.push(
SceneBodySpec::dynamic(NodeCollider::Ball { radius: 28.0 }, (0.0, -170.0)).restitution(0.1),
);
bodies.push(
SceneBodySpec::dynamic(NodeCollider::Ball { radius: 22.0 }, (90.0, -230.0))
.restitution(0.1),
);
let mut joints = vec![SceneJointSpec {
body_a: 0,
body_b: 1,
joint: SceneJoint::Revolute {
anchor_a: (0.0, 0.0),
anchor_b: (-halfw, 0.0),
motor: None,
},
}];
for i in 1..9 {
joints.push(SceneJointSpec {
body_a: i,
body_b: i + 1,
joint: SceneJoint::Revolute {
anchor_a: (halfw, 0.0),
anchor_b: (-halfw, 0.0),
motor: None,
},
});
}
joints.push(SceneJointSpec {
body_a: 9,
body_b: 10,
joint: SceneJoint::Revolute {
anchor_a: (halfw, 0.0),
anchor_b: (0.0, 0.0),
motor: None,
},
});
SceneSpec {
bodies,
gravity: (0.0, 500.0),
default_tangible: false,
perpetual: false,
joints,
}
}
pub fn ball_and_chain_scene() -> SceneSpec {
let anchor = (140.0, -260.0);
let link = 44.0;
let mut bodies = vec![SceneBodySpec::fixed(
NodeCollider::Ball { radius: 10.0 },
anchor,
)];
for i in 0..4 {
bodies.push(SceneBodySpec::dynamic(
NodeCollider::Ball { radius: 12.0 },
(anchor.0 - (i as f32 + 1.0) * link, anchor.1),
));
}
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Ball { radius: 34.0 },
(anchor.0 - 5.0 * link, anchor.1),
)
.restitution(0.2),
);
bodies.push(
SceneBodySpec::fixed(NodeCollider::Square { half: 300.0 }, (0.0, 300.0)).restitution(0.1),
);
for k in 0..5 {
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Square { half: 16.0 },
(140.0, -16.0 - k as f32 * 32.0),
)
.restitution(0.0),
);
}
let joints = (0..5)
.map(|i| SceneJointSpec {
body_a: i,
body_b: i + 1,
joint: SceneJoint::Rope {
anchor_a: (0.0, 0.0),
anchor_b: (0.0, 0.0),
length: link,
},
})
.collect();
SceneSpec {
bodies,
gravity: (0.0, 540.0),
default_tangible: false,
perpetual: false,
joints,
}
}
pub fn mixer_scene() -> SceneSpec {
use std::f32::consts::TAU;
let arm = 150.0;
let mut bodies = vec![SceneBodySpec::fixed(
NodeCollider::Ball { radius: 6.0 },
(0.0, 0.0),
)];
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Hull {
points: vec![(-arm, -8.0), (arm, -8.0), (arm, 8.0), (-arm, 8.0)],
fallback: 20.0,
},
(0.0, 0.0),
)
.restitution(0.4),
);
for i in 0..18 {
let a = i as f32 * TAU / 18.0;
bodies.push(
SceneBodySpec::fixed(
NodeCollider::Ball { radius: 20.0 },
(250.0 * a.cos(), 250.0 * a.sin()),
)
.restitution(0.5),
);
}
for i in 0..10 {
let a = i as f32 * TAU / 10.0 + 0.3;
bodies.push(
SceneBodySpec::dynamic(
NodeCollider::Ball {
radius: 14.0 + (i % 3) as f32 * 5.0,
},
(200.0 * a.cos(), 200.0 * a.sin()),
)
.restitution(0.6),
);
}
let joints = vec![SceneJointSpec {
body_a: 0,
body_b: 1,
joint: SceneJoint::Revolute {
anchor_a: (0.0, 0.0),
anchor_b: (0.0, 0.0),
motor: Some(JointMotorSpec {
target_vel: 2.5,
factor: 30.0,
}),
},
}];
SceneSpec {
bodies,
gravity: (0.0, 0.0),
default_tangible: false,
perpetual: true,
joints,
}
}
#[cfg(test)]
mod tests {
use crate::NodeKey;
use euclid::default::Point2D;
use super::*;
use crate::Simulation;
#[test]
fn catalog_scenes_load_and_report_perpetual() {
let cases: [(SceneSpec, bool); 11] = [
(drop_bowl_scene(), false),
(pyramid_scene(), false),
(domino_scene(), false),
(galton_scene(), false),
(funnel_scene(), false),
(drift_scene(), true),
(chain_scene(), false),
(cradle_scene(), true),
(bridge_scene(), false),
(ball_and_chain_scene(), false),
(mixer_scene(), true),
];
for (spec, perpetual) in cases {
let want = spec.bodies.len();
let mut sim = Simulation::new();
let node = NodeKey::new(0);
sim.sync_nodes([(node, Point2D::new(0.0, 0.0))]);
sim.load_scene(&spec);
assert!(
want <= 200,
"scene must stay under SCENE_BODY_CAP (was {want})"
);
assert_eq!(
sim.scene_body_count(),
want,
"every spec body became a scene body"
);
assert_eq!(
sim.body_count(),
1,
"the node is not counted as a scene body"
);
assert_eq!(
sim.scene_perpetual(),
perpetual,
"scene reports its perpetual flag"
);
}
}
#[test]
fn perpetual_drifts_while_settling_scene_comes_to_rest() {
let mut settle = Simulation::new();
settle.load_scene(&pyramid_scene());
for _ in 0..600 {
settle.tick(1.0 / 60.0);
}
let a: Vec<_> = settle.scene_bodies().map(|b| b.position).collect();
for _ in 0..30 {
settle.tick(1.0 / 60.0);
}
let b: Vec<_> = settle.scene_bodies().map(|b| b.position).collect();
let settled_motion: f32 = a.iter().zip(&b).map(|(p, q)| (*p - *q).length()).sum();
assert!(
settled_motion < 5.0,
"pyramid should be at rest (moved {settled_motion})"
);
let mut drift = Simulation::new();
drift.load_scene(&drift_scene());
for _ in 0..600 {
drift.tick(1.0 / 60.0);
}
let a: Vec<_> = drift.scene_bodies().map(|b| b.position).collect();
for _ in 0..30 {
drift.tick(1.0 / 60.0);
}
let b: Vec<_> = drift.scene_bodies().map(|b| b.position).collect();
let drift_motion: f32 = a.iter().zip(&b).map(|(p, q)| (*p - *q).length()).sum();
assert!(
drift_motion > 5.0,
"drift orbs should still be moving (moved {drift_motion})"
);
}
#[test]
fn chain_joints_anchor_the_links() {
let mut sim = Simulation::new();
sim.load_scene(&chain_scene());
for _ in 0..300 {
sim.tick(1.0 / 60.0);
}
let max_y = sim
.scene_bodies()
.map(|b| b.position.y)
.fold(f32::NEG_INFINITY, f32::max);
assert!(
max_y < 500.0,
"rope joints should anchor the chain, not let it fall away (max_y {max_y})"
);
assert!(
max_y > 0.0,
"the chain should hang below the anchor (max_y {max_y})"
);
}
#[test]
fn mixer_motor_spins_the_paddle() {
let mut sim = Simulation::new();
sim.load_scene(&mixer_scene());
let paddle_rot = |sim: &Simulation| {
sim.scene_bodies()
.find(|b| matches!(b.collider, crate::NodeCollider::Hull { .. }))
.map(|b| b.rotation)
.expect("the mixer has a paddle")
};
let start = paddle_rot(&sim);
for _ in 0..120 {
sim.tick(1.0 / 60.0);
}
let after = paddle_rot(&sim);
assert!(
(after - start).abs() > 0.5,
"the motor should have rotated the paddle (start {start}, after {after})"
);
}
#[test]
fn bridge_sags_under_the_dropped_weight() {
let mut sim = Simulation::new();
sim.load_scene(&bridge_scene());
for _ in 0..240 {
sim.tick(1.0 / 60.0);
}
let lowest_plank_y = sim
.scene_bodies()
.filter(|b| matches!(b.collider, crate::NodeCollider::Hull { .. }))
.map(|b| b.position.y)
.fold(f32::NEG_INFINITY, f32::max);
assert!(
lowest_plank_y > 20.0,
"the loaded bridge should sag below the deck (was {lowest_plank_y})"
);
assert!(
lowest_plank_y < 600.0,
"the posts should still hold the span, not drop it (was {lowest_plank_y})"
);
}
}