#![allow(dead_code)]
use std::cell::Cell;
use box2d_rust::collision::Polygon;
use box2d_rust::geometry::{make_polygon, make_square};
use box2d_rust::hull::compute_hull;
use box2d_rust::math_functions::{make_rot, Rot, Vec2, PI};
pub const RAND_LIMIT: i32 = 32767;
pub const RAND_SEED: u32 = 12345;
thread_local! {
static G_RANDOM_SEED: Cell<u32> = const { Cell::new(RAND_SEED) };
}
pub fn reset_random_seed() {
G_RANDOM_SEED.with(|seed| seed.set(RAND_SEED));
}
pub fn random_int() -> i32 {
G_RANDOM_SEED.with(|seed| {
let mut x = seed.get();
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
seed.set(x);
(x % (RAND_LIMIT as u32 + 1)) as i32
})
}
pub fn random_int_range(lo: i32, hi: i32) -> i32 {
lo + random_int() % (hi - lo + 1)
}
pub fn random_float() -> f32 {
let mut r = (random_int() & RAND_LIMIT) as f32;
r /= RAND_LIMIT as f32;
r = 2.0 * r - 1.0;
r
}
pub fn random_float_range(lo: f32, hi: f32) -> f32 {
let mut r = (random_int() & RAND_LIMIT) as f32;
r /= RAND_LIMIT as f32;
r = (hi - lo) * r + lo;
r
}
pub fn random_vec2(lo: f32, hi: f32) -> Vec2 {
Vec2 {
x: random_float_range(lo, hi),
y: random_float_range(lo, hi),
}
}
pub fn random_rot() -> Rot {
let angle = random_float_range(-PI, PI);
make_rot(angle)
}
pub fn random_polygon(extent: f32) -> Polygon {
let count = 3 + random_int() % 6;
let mut points = Vec::with_capacity(count as usize);
for _ in 0..count {
points.push(random_vec2(-extent, extent));
}
let hull = compute_hull(&points);
if hull.count > 0 {
return make_polygon(&hull, 0.0);
}
make_square(extent)
}