From 9b76572e7bd9d02cd25a581010869d09163307be Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 28 Oct 2021 14:34:32 +0300 Subject: feat: make gear rotating --- src/camera.rs | 18 +++++++++++------- src/main.rs | 5 ++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 4a985a5..ed8117e 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -58,7 +58,7 @@ fn sd_box(point: Vector, center: Vector, size: Vector) -> f32 { impl Camera { - pub fn sd_gear(&self, point: Vector, center: Vector, radius: f32, thickness: f32) -> f32 { + pub fn sd_gear(&self, point: Vector, center: Vector, radius: f32, thickness: f32, turn_rate: f32) -> f32 { let mut dist: f32; // Ring @@ -69,27 +69,31 @@ impl Camera { // Teeth { + let sector_angle = Rad::full_turn() / 12.0; + let center = Vector { x: 0.0, y: radius + thickness / 2.0, z: 0.0 }; let size = Vector::new(thickness, thickness * 2.0, thickness); - let sector_angle = Rad::full_turn() / 12.0; - let point_angle = (point.z / point.y).atan(); + // Account for rotation with time + let rotated_point = Matrix3::from_angle_x(sector_angle * self.time / turn_rate) * point; + + // Map all space to the first sector + let point_angle = (rotated_point.z / rotated_point.y).atan(); let sector = (point_angle / sector_angle.0).round(); - let mut mapped_point = Matrix3::from_angle_x(-sector_angle * sector) * point; + let mut mapped_point = Matrix3::from_angle_x(-sector_angle * sector) * rotated_point; mapped_point.y = mapped_point.y.abs(); - // Make teeth smooth by subtracting some amount dist = dist.min(sd_box(mapped_point, center, size) - thickness / 4.0); } - // Tak a slice + // Take a slice dist = dist.max(point.x.abs() - thickness / 2.0); return dist; } pub fn sdf(&self, point: Vector) -> f32 { - self.sd_gear(point, Vector::zero(), 3.0, 0.6) + self.sd_gear(point, Vector::zero(), 3.0, 0.6, 10.0) } pub fn screen(&self) -> (f32, f32) { diff --git a/src/main.rs b/src/main.rs index 2574746..c139888 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,18 +26,17 @@ fn main() { initscr(); - let time = 0; - while true { clear(); flushinp(); // Render - cam.time = (time as f32 / 60.0) * 2.0 * PI; + cam.time += 1.0; let timestamp = Instant::now(); cam.render(); addstr(&cam.buffer.to_string()); addstr(&format!("\nRendered in {:?} ({:.0} FPS)\n", timestamp.elapsed(), 1.0 / timestamp.elapsed().as_secs_f64())); + addstr(&format!("\nTime: {:?}\n", cam.time)); addstr(&format!("Camera: {:?}\n", cam.position)); addstr(&format!("Facing: {:?}, Up: {:?}\n", cam.direction, cam.up)); addstr(&format!("Light: {:?}\n", cam.light)); -- cgit v1.2.3