aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2021-10-28 14:34:32 +0300
committereug-vs <eugene@eug-vs.xyz>2021-10-28 14:34:32 +0300
commit9b76572e7bd9d02cd25a581010869d09163307be (patch)
tree17b7192052bfda5d82c322f005f67d7c71c2ed3c
parentff8e486274d789e9c50b367e5eccf174b85a370d (diff)
downloadpistol-9b76572e7bd9d02cd25a581010869d09163307be.tar.gz
feat: make gear rotating
-rw-r--r--src/camera.rs18
-rw-r--r--src/main.rs5
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));