use super::utils::{Helipad, CHUNKS, MIDDLE};
use crate::env::rapier::{lunar_lander::config::LunarLanderConfig, world::PhysicsWorld};
use nalgebra::point;
use rand::rngs::StdRng;
use rand::Rng;
use rapier2d::prelude::{
ActiveEvents, ColliderBuilder, RigidBodyBuilder, RigidBodyHandle, RigidBodyType,
};
pub fn generate_moon(
config: &LunarLanderConfig,
world: &mut PhysicsWorld,
rng: &mut StdRng,
) -> (Helipad, RigidBodyHandle) {
let w = config.get_scaled_width();
let h = config.get_scaled_height();
let mut helipad = Helipad {
y: h / 4.0,
..Helipad::default()
};
let height: Vec<f32> = (0..=CHUNKS)
.map(|i| match i {
x if ((MIDDLE - 2)..=(MIDDLE + 2)).contains(&x) => helipad.y,
_ => rng.random_range(0.0..h / 2.0),
})
.collect();
let chunk_x: Vec<f32> = (0..CHUNKS)
.map(|i| w / (CHUNKS - 1) as f32 * i as f32)
.collect();
helipad.x1 = chunk_x[MIDDLE - 1];
helipad.x2 = chunk_x[MIDDLE + 1];
let smooth_y: Vec<f32> = (0..CHUNKS)
.map(|i| {
0.33 * (height[i]
+ height[i + 1]
+ match i {
0 => height[CHUNKS],
_ => height[i - 1],
})
})
.collect();
let moon_body = RigidBodyBuilder::new(RigidBodyType::Fixed).build();
let moon_handle = world.rigid_body_set.insert(moon_body);
(0..(CHUNKS - 1)).for_each(|i| {
let p1 = point![chunk_x[i], smooth_y[i]];
let p2 = point![chunk_x[i + 1], smooth_y[i + 1]];
let coll = ColliderBuilder::segment(p1, p2)
.friction(0.1)
.active_events(ActiveEvents::COLLISION_EVENTS)
.build();
world
.collider_set
.insert_with_parent(coll, moon_handle, &mut world.rigid_body_set);
});
(helipad, moon_handle)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::env::rapier::lunar_lander::config::LunarLanderConfig;
use crate::env::rapier::world::PhysicsWorld;
use rand::SeedableRng;
use rapier2d::prelude::{ColliderSet, RigidBodySet};
fn create_test_world() -> PhysicsWorld {
PhysicsWorld {
rigid_body_set: RigidBodySet::new(),
collider_set: ColliderSet::new(),
..Default::default()
}
}
#[test]
fn test_moon_generation_basics() {
let config = LunarLanderConfig::new()
.with_viewport_size(600, 400)
.with_scale(30.0);
let mut world = create_test_world();
let mut rng = StdRng::seed_from_u64(0);
let (helipad, moon_handle) = generate_moon(&config, &mut world, &mut rng);
assert!(
world.rigid_body_set.contains(moon_handle),
"Moon body should be inserted into the set"
);
let body = &world.rigid_body_set[moon_handle];
assert!(body.is_fixed(), "Moon should be a fixed rigid body");
let h = config.get_scaled_height();
let w = config.get_scaled_width();
assert_eq!(helipad.y, h / 4.0);
let step = w / 10.0;
let expected_x1 = step * (5 - 1) as f32; let expected_x2 = step * (5 + 1) as f32;
assert!((helipad.x1 - expected_x1).abs() < 1e-5);
assert!((helipad.x2 - expected_x2).abs() < 1e-5);
}
#[test]
fn test_moon_colliders_count() {
let config = LunarLanderConfig::default();
let mut world = create_test_world();
let mut rng = StdRng::seed_from_u64(0);
let (_, moon_handle) = generate_moon(&config, &mut world, &mut rng);
let collider_count = world
.collider_set
.iter()
.filter(|(_, collider)| collider.parent() == Some(moon_handle))
.count();
assert_eq!(
collider_count, 10,
"Should generate CHUNKS - 1 ground segments"
);
}
#[test]
fn test_moon_colliders_properties() {
let config = LunarLanderConfig::default();
let mut world = create_test_world();
let mut rng = StdRng::seed_from_u64(0);
let (_, moon_handle) = generate_moon(&config, &mut world, &mut rng);
for (_, collider) in world.collider_set.iter() {
if collider.parent() == Some(moon_handle) {
assert_eq!(
collider.friction(),
0.1,
"Ground segments should have 0.1 friction"
);
assert!(
collider.shape().as_segment().is_some(),
"Ground should be made of segments"
);
}
}
}
#[test]
fn test_helipad_flatness() {
let config = LunarLanderConfig::default();
let mut world = create_test_world();
let mut rng = StdRng::seed_from_u64(0);
let (_, moon_handle) = generate_moon(&config, &mut world, &mut rng);
let mut segments_found = false;
for (_, collider) in world.collider_set.iter() {
if collider.parent() == Some(moon_handle) {
if let Some(segment) = collider.shape().as_segment() {
let _a = segment.a;
let _b = segment.b;
segments_found = true;
}
}
}
assert!(segments_found, "Should find segments attached to moon");
}
}