Skip to content

Commit

Permalink
Time (#41)
Browse files Browse the repository at this point in the history
* added slider for adjusting time

* adjusting dual based on speed, too
  • Loading branch information
organizedgrime authored Nov 28, 2024
1 parent 7f07d1c commit 372fafb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
25 changes: 12 additions & 13 deletions src/bones/polyhedron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use crate::{
use std::time::{Duration, Instant};
use ultraviolet::{Lerp, Vec3};

const TICK_SPEED: f32 = 10.0;
const SPEED_DAMPENING: f32 = 0.92;

// Operations
impl PolyGraph {
fn apply_spring_forces(&mut self, second: f32) {
fn apply_spring_forces(&mut self, speed: f32, second: f32) {
let diameter = *self.dist.values().max().unwrap_or(&1) as f32;
let diameter_spring_length = self.edge_length * 2.0;
let (edges, contracting): (std::collections::hash_set::Iter<Edge>, bool) =
Expand All @@ -28,12 +27,12 @@ impl PolyGraph {
let diff = v_position - u_position;
let spring_length = diff.mag();
if contracting {
let f = ((self.edge_length / TICK_SPEED * second) * 10.0) / spring_length;
let f = ((self.edge_length / speed * second) * 10.0) / spring_length;
*self.positions.entry(v).or_default() = v_position.lerp(u_position, f);
*self.positions.entry(u).or_default() = u_position.lerp(v_position, f);
} else {
let target_length = diameter_spring_length * (self.dist[e] as f32 / diameter);
let f = diff * (target_length - spring_length) / TICK_SPEED * second;
let f = diff * (target_length - spring_length) / speed * second;
*self.speeds.entry(v).or_default() = (self.speeds[&v] + f) * SPEED_DAMPENING;
*self.speeds.entry(u).or_default() = (self.speeds[&u] - f) * SPEED_DAMPENING;
*self.positions.entry(v).or_default() += self.speeds[&v];
Expand All @@ -51,17 +50,17 @@ impl PolyGraph {
}
}

fn resize(&mut self, second: f32) {
fn resize(&mut self, speed: f32, second: f32) {
let mean_length = self.positions.values().map(|p| p.mag()).fold(0.0, f32::max);
let distance = mean_length - 1.0;
self.edge_length -= distance / TICK_SPEED * second;
self.edge_length -= distance / speed * second;
}

pub fn update(&mut self, second: f32) {
pub fn update(&mut self, speed: f32, second: f32) {
self.center();
self.resize(second);
self.apply_spring_forces(second);
self.process_transactions();
self.resize(speed, second);
self.apply_spring_forces(speed, second);
self.process_transactions(speed);
}

pub fn face_positions(&self, face_index: usize) -> Vec<Vec3> {
Expand All @@ -77,14 +76,14 @@ impl PolyGraph {
vertices.iter().fold(Vec3::zero(), |a, &b| a + b) / vertices.len() as f32
}

pub fn process_transactions(&mut self) {
pub fn process_transactions(&mut self, speed: f32) {
if let Some(transaction) = self.transactions.first().cloned() {
use Transaction::*;
match transaction {
Contraction(edges) => {
if !edges
.iter()
.any(|e| (self.positions[&e.v()] - self.positions[&e.u()]).mag() > 0.08)
.any(|e| (self.positions[&e.v()] - self.positions[&e.u()]).mag() > 0.02)
{
// Contract them in the graph
self.contract_edges(edges);
Expand All @@ -110,7 +109,7 @@ impl PolyGraph {
Dual => {
let edges = self.expand(false);
vec![
Wait(Instant::now() + Duration::from_millis(500)),
Wait(Instant::now() + Duration::from_millis((65.0 * speed) as u64)),
Contraction(edges),
Name('d'),
]
Expand Down
1 change: 1 addition & 0 deletions src/render/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl MenuAble<'static, Controls> for RenderMessage {
Self::checkbox("Rotating", state.rotating, Rotating),
Self::slider(0.0..=10.0, state.line_thickness, LineThickness, 1.0),
Self::slider(1.0..=5.0, state.zoom, ZoomChanged, 0.05),
Self::slider(5.0..=50.0, state.speed, SpeedChanged, 10.0),
Self::slider(
0.0..=(std::f32::consts::PI * 2.0),
state.camera.fov_y,
Expand Down
5 changes: 5 additions & 0 deletions src/render/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub enum RenderMessage {
Rotating(bool),
FovChanged(f32),
ZoomChanged(f32),
SpeedChanged(f32),
LineThickness(f32),
ColorMethod(ColorMethodMessage),
ColorPicker(ColorPickerMessage),
Expand Down Expand Up @@ -216,6 +217,10 @@ impl ProcessMessage<RenderState> for RenderMessage {
state.zoom = *zoom;
Task::none()
}
SpeedChanged(speed) => {
state.speed = *speed;
Task::none()
}
LineThickness(thickness) => {
state.line_thickness = *thickness;
Task::none()
Expand Down
4 changes: 3 additions & 1 deletion src/render/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct AppState {
pub struct RenderState {
pub camera: Camera,
pub zoom: f32,
pub speed: f32,
pub start: Instant,
pub frame: Instant,
pub rotation_duration: Duration,
Expand All @@ -48,6 +49,7 @@ impl Default for RenderState {
Self {
camera: Camera::default(),
zoom: 1.0,
speed: 10.0,
start: Instant::now(),
frame: Instant::now(),
rotation_duration: Duration::from_secs(0),
Expand Down Expand Up @@ -120,7 +122,7 @@ impl AppState {
frame_difference
};

self.model.polyhedron.update(second);
self.model.polyhedron.update(self.render.speed, second);
self.render.frame = time;

let time = if self.render.rotating {
Expand Down

0 comments on commit 372fafb

Please sign in to comment.