Compare commits

..

No commits in common. "main" and "add-license-1" have entirely different histories.

11 changed files with 286 additions and 663 deletions

1
.gitignore vendored
View File

@ -1,2 +1 @@
/target /target
Cargo.lock

85
Cargo.lock generated Normal file
View File

@ -0,0 +1,85 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hypoloop"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "libc"
version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

View File

@ -1,21 +1,9 @@
[package] [package]
name = "hypoloop" name = "hypoloop"
description = "A low-level control loop for real-time and baked simulations." version = "0.1.0"
version = "0.1.7"
edition = "2018" edition = "2018"
license = "GPL-3.0"
repository = "https://github.com/skyeterran/hypoloop"
keywords = ["gamedev", "simulation", "graphics"]
include = ["/src", "LICENSE", "/examples"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
[dev-dependencies]
env_logger = "0.9"
log = "0.4"
pixels = "0.5.0"
winit = "0.25"
winit_input_helper = "0.10"
rand = "0.8.4" rand = "0.8.4"

View File

@ -1,45 +1,2 @@
# hypoloop # hypoloop
## A flexible game-like loop for real-time simulation and rendering
### Features:
- Constant update rate
- Variable display rate
- Arbitrary simulation timescale
- Support for multiple simultaneous simulations with shared data
- Compatible with other libraries' built-in event loops
- Real-time can be disabled for high-speed simulations
## Example
```rust
use hypoloop::core::{State, Loop};
fn main() {
// create sim with default configuration
let mut sim = Loop::new();
// test variable
let mut x: f32 = 0.0;
// create a closure containing your update logic
let mut update_logic = move |state: &mut State| {
// access loop metadata via the State object
x += state.get_timescale();
print!("x: {} | ", x);
// print information about the current tick's timings
state.debug_time();
};
// create a closure containing your display logic
let display_logic = move |state: &State| {
//
};
// run the simulation with your user-defined update and display logic
// initialize the sim (cleans internal clocks, etc.)
sim.init();
loop {
// "step" the sim forward
sim.step(&mut update_logic, &display_logic);
}
}
```

View File

@ -1,33 +0,0 @@
use hypoloop::core::{State, Loop};
fn main() {
// create a new sim loop
let mut sim = Loop::new();
sim.set_update_interval(20);
// test variable
let mut x: f32 = 0.0;
// create a closure containing your update logic
let mut tick = move |state: &mut State| {
// access loop metadata via the State object
x += state.get_timestep();
print!("x: {} | ", x);
// print information about the current tick's timings
state.debug_time();
};
// create a closure containing your display logic
let mut display = move |state: &mut State| {
//
};
// run the simulation with your user-defined update and display logic
// initialize the sim (cleans internal clocks, etc.)
sim.init();
loop {
// "step" the sim forward
sim.step(&mut tick, &mut display);
}
}

View File

@ -1,137 +0,0 @@
#![deny(clippy::all)]
#![forbid(unsafe_code)]
use hypoloop::core::{State, Loop};
use rand::Rng;
use log::error;
use pixels::{Error, Pixels, SurfaceTexture};
use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
use winit_input_helper::WinitInputHelper;
const WIDTH: u32 = 500;
const HEIGHT: u32 = 500;
const BOX_SIZE: i16 = 64;
/// Representation of the application state. In this example, a box will bounce around the screen.
struct World {
target: [i16; 2]
}
fn main() -> Result<(), Error> {
env_logger::init();
let event_loop = EventLoop::new();
let mut input = WinitInputHelper::new();
let window = {
let size = LogicalSize::new(WIDTH as f64, HEIGHT as f64);
WindowBuilder::new()
.with_title("Hello Pixels")
.with_inner_size(size)
.with_min_inner_size(size)
.build(&event_loop)
.unwrap()
};
let mut pixels = {
let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new(WIDTH, HEIGHT, surface_texture)?
};
let mut world = World::new();
// create sim with default configuration
let mut sim = Loop::new();
//sim.set_update_interval(10);
let mut update_logic = move |state: &mut State| {
// print information about the current tick's timings
state.debug_time();
world.update(state.get_timestep());
world.draw(pixels.get_frame());
if pixels
.render()
.map_err(|e| error!("pixels.render() failed: {}", e))
.is_err()
{
state.pause();
return;
}
};
// create a closure containing your display logic
let mut display_logic = move |state: &mut State| {
// Draw the current frame
window.request_redraw();
};
sim.init();
event_loop.run(move |event, _, control_flow| {
// Handle input events
if input.update(&event) {
// Close events
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
*control_flow = ControlFlow::Exit;
return;
}
}
// step the sim forward
sim.step(&mut update_logic, &mut display_logic);
});
}
impl World {
/// Create a new `World` instance that can draw a moving box.
fn new() -> Self {
Self {
target: [0, 0]
}
}
/// Update the `World` internal state; bounce the box around the screen.
fn update(&mut self, timestep: f32) {
let speed: f32 = 500.0;
let mut new_target = self.target;
// update the target
if new_target[0] < WIDTH as i16 {
new_target[0] += (speed * timestep) as i16;
} else {
new_target[0] = 0;
}
self.target = new_target;
}
/// Draw the `World` state to the frame buffer.
///
/// Assumes the default texture format: `wgpu::TextureFormat::Rgba8UnormSrgb`
fn draw(&self, frame: &mut [u8]) {
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
let x = (i % WIDTH as usize) as i16;
let y = (i / WIDTH as usize) as i16;
let mut old_pixel = [0u8; 4];
for j in 0..4 {
// get the old pixel and decay it
old_pixel[j] = pixel[j];
if old_pixel[j] > 0 {
old_pixel[j] -= 1;
}
}
let condition = x <= self.target[0];
let rgba = if condition {
[0xff, 0x00, 0x00, 0xff]
} else {
old_pixel
};
pixel.copy_from_slice(&rgba);
}
}
}

View File

@ -1,49 +0,0 @@
use std::time::Duration;
use hypoloop::core::{State, Loop};
// this is a demonstration of running two different Loops simultaneously, both acting on the same data
fn main() {
// create a vector of sim loops
let mut multiverse: Vec<&mut Loop> = vec![];
// Sim A
let mut sim_a = Loop::new();
sim_a.set_update_interval(40);
sim_a.mut_state().set_timescale(1.0);
multiverse.push(&mut sim_a);
// Sim B
// twice the speed and twice the updates
let mut sim_b = Loop::new();
sim_b.set_update_interval(40);
sim_b.mut_state().set_timescale(2.0);
multiverse.push(&mut sim_b);
// shared variable
let mut x = Duration::new(0,0);
// tick behavior
// note how "state" switches between Sim A and B but x doesn't
// I'm purposefully NOT moving ownership of x into this closure so that I can access it later
let mut tick = |state: &mut State| {
// access loop metadata via the State object
x = (x + state.get_sim_time()) / 2;
print!("Average sim time: {} | ", x.as_millis());
// print information about the current tick's timings
state.debug_time();
};
// create a closure containing your display logic
let mut display = move |state: &mut State| {
//
};
// run both sims simultaneously
sim_a.init();
sim_b.init();
loop {
sim_a.step(&mut tick, &mut display);
sim_b.step(&mut tick, &mut display);
}
}

View File

@ -1,135 +0,0 @@
#![deny(clippy::all)]
#![forbid(unsafe_code)]
use hypoloop::core::{State, Loop};
use log::error;
use pixels::{Error, Pixels, SurfaceTexture};
use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
use winit_input_helper::WinitInputHelper;
const WIDTH: u32 = 320;
const HEIGHT: u32 = 240;
const BOX_SIZE: i16 = 64;
/// Representation of the application state. In this example, a box will bounce around the screen.
struct World {
box_x: i16,
box_y: i16,
velocity_x: i16,
velocity_y: i16,
}
fn main() -> Result<(), Error> {
env_logger::init();
let event_loop = EventLoop::new();
let mut input = WinitInputHelper::new();
let window = {
let size = LogicalSize::new(WIDTH as f64, HEIGHT as f64);
WindowBuilder::new()
.with_title("Hello Pixels")
.with_inner_size(size)
.with_min_inner_size(size)
.build(&event_loop)
.unwrap()
};
let mut pixels = {
let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new(WIDTH, HEIGHT, surface_texture)?
};
let mut world = World::new();
// create sim with default configuration
let mut sim = Loop::new();
sim.set_update_interval(10);
let mut update_logic = move |state: &mut State| {
// print information about the current tick's timings
state.debug_time();
world.update(state.get_delta_time(), state.get_timescale());
world.draw(pixels.get_frame());
if pixels
.render()
.map_err(|e| error!("pixels.render() failed: {}", e))
.is_err()
{
state.pause();
return;
}
};
// create a closure containing your display logic
let mut display_logic = move |state: &mut State| {
// Draw the current frame
window.request_redraw();
};
sim.init();
event_loop.run(move |event, _, control_flow| {
// Handle input events
if input.update(&event) {
// Close events
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
*control_flow = ControlFlow::Exit;
return;
}
}
// step the sim forward
sim.step(&mut update_logic, &mut display_logic);
});
}
impl World {
/// Create a new `World` instance that can draw a moving box.
fn new() -> Self {
Self {
box_x: 24,
box_y: 16,
velocity_x: 100,
velocity_y: 100,
}
}
/// Update the `World` internal state; bounce the box around the screen.
fn update(&mut self, delta_time: u32, timescale: f32) {
let timestep: f32 = delta_time as f32 / 1000.0 * timescale;
if self.box_x <= 0 || self.box_x + BOX_SIZE > WIDTH as i16 {
self.velocity_x *= -1;
}
if self.box_y <= 0 || self.box_y + BOX_SIZE > HEIGHT as i16 {
self.velocity_y *= -1;
}
self.box_x += (self.velocity_x as f32 * timestep) as i16;
self.box_y += (self.velocity_y as f32 * timestep) as i16;
}
/// Draw the `World` state to the frame buffer.
///
/// Assumes the default texture format: `wgpu::TextureFormat::Rgba8UnormSrgb`
fn draw(&self, frame: &mut [u8]) {
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
let x = (i % WIDTH as usize) as i16;
let y = (i / WIDTH as usize) as i16;
let inside_the_box = x >= self.box_x
&& x < self.box_x + BOX_SIZE
&& y >= self.box_y
&& y < self.box_y + BOX_SIZE;
let rgba = if inside_the_box {
[0x5e, 0x48, 0xe8, 0xff]
} else {
[0x48, 0xb2, 0xe8, 0xff]
};
pixel.copy_from_slice(&rgba);
}
}
}

View File

@ -1,46 +0,0 @@
use hypoloop::core::{State, Loop};
use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{CursorIcon, WindowBuilder},
};
fn main() {
// create sim with default configuration
let mut sim = Loop::new();
// test variable
let mut x: f32 = 0.0;
// create a winit event loop
let event_loop = EventLoop::new();
// create a winit window
let window = WindowBuilder::new().build(&event_loop).unwrap();
window.set_title("Windowing test with hypoloop");
// create a closure containing your update logic
let mut update_logic = move |state: &mut State| {
// access loop metadata via the State object
x += state.get_timescale();
print!("x: {} | ", x);
// print information about the current tick's timings
state.debug_time();
};
// create a closure containing your display logic
let display_logic = move |state: &mut State| {
// redraw the winit window
window.request_redraw();
};
// initialize the sim (cleans internal clocks, etc.)
sim.init();
// run the winit event loop with embedded hypoloop sim
event_loop.run(move |event, _, control_flow| {
// "step" the sim forward
sim.step(&mut update_logic, &mut display_logic);
});
}

View File

@ -1,204 +0,0 @@
pub mod core {
use std::time::{Duration, Instant};
/// Contains mutable simulation state which can be changed via callback functions
#[derive(Copy, Clone)]
pub struct State {
timescale: f32,
simulate: bool,
clock_start: Instant,
last_tick: Instant,
delta_time: u32,
timestep: f32,
irl_time: Duration,
sim_time: Duration
}
impl State {
/// Creates a default State object
pub fn new() -> State {
// Create default state object
let new_state = State {
timescale: 1.0,
simulate: true,
clock_start: Instant::now(),
last_tick: Instant::now(),
delta_time: 0,
timestep: 0.0,
irl_time: Duration::new(0,0),
sim_time: Duration::new(0,0)
};
// Return this default state
new_state
}
/// Returns the current "delta time", the real time (in ms) elapsed since the last update tick
pub fn get_delta_time(self) -> u32 {
self.delta_time
}
/// Returns the current "timestep", the virtual time (in s) elapsed since the last update tick (necessary for scaling physics simulations, etc.)
pub fn get_timestep(self) -> f32 {
self.timestep
}
/// Returns the current real time elapsed since the start of the simulation
pub fn get_irl_time(self) -> Duration {
self.irl_time
}
/// Returns the current simulation time elapsed since the start of the simulation
pub fn get_sim_time(self) -> Duration {
self.sim_time
}
/// Returns the current "timescale", the speed of simulation time relative to real time
pub fn get_timescale(self) -> f32 {
self.timescale
}
/// Returns the time of the last tick
pub fn get_last_tick(self) -> Instant {
self.last_tick
}
/// Pauses the simulation from within update logic
pub fn pause(&mut self) {
self.simulate = false;
}
/// Resumes the simulation from within update logic
pub fn resume(&mut self) {
self.simulate = true;
}
/// Changes the simulation timescale
pub fn set_timescale(&mut self, timescale: f32) {
self.timescale = timescale;
}
/// Prints a string of information about the current step's timings
///
/// # Example:
/// `IRL time: 4443ms | Sim time: 4443ms | Delta time (tick): 40ms | Delta time (step): 40.0638ms | Timestep: 0.04s`
/// # Terminology:
/// - *IRL time:* Real time (in ms) elapsed since the start of the simulation
/// - *Sim time:* Virtual time (in ms) elapsed since the start of the simulation
/// - *Delta time (tick):* Real time (in ms) elapsed between the last tick and the previous tick
/// - *Delta time (step):* Real time (in ms with ns accuracy) elapsed since the last tick
/// - *Timestep:* Virtual time (in s with ms accuracy) elapsed since the last tick
pub fn debug_time(self) {
let elapsed_time = Instant::now().duration_since(self.last_tick);
let loop_delay_ms = elapsed_time.as_nanos() as f32 / 1_000_000.0;
println!("IRL time: {}ms | Sim time: {}ms | Delta time (tick): {}ms | Delta time (step): {}ms | Timestep: {}s", self.irl_time.as_millis(), self.sim_time.as_millis(), self.delta_time, loop_delay_ms, self.timestep);
}
}
/// The simulation loop itself
pub struct Loop {
state: State,
realtime: bool,
update_interval: u32
}
impl Loop {
/// Creates a new simulation with default values
pub fn new() -> Loop {
// Create a new State object
let mut new_state = State::new();
// Create a Loop object with a default State
let mut new_loop = Loop {
state: new_state,
realtime: true,
update_interval: 40
};
// Initialize the delta time to be the same as the update interval (to prevent division by zero)
new_loop.state.delta_time = new_loop.update_interval;
// Initialize the timestep based on the new delta time
new_loop.state.timestep = timestep(new_loop.state.delta_time, new_loop.state.timescale);
// Return the now-initialized Loop
new_loop
}
/// Initializes or re-initializes the simulation
pub fn init(&mut self) {
// Make sure the simulation will run
self.state.simulate = true;
// reset the internal clocks
self.state.clock_start = Instant::now();
self.state.irl_time = Duration::new(0,0);
self.state.sim_time = Duration::new(0,0);
}
/// Returns a mutable reference to the Loop's State object
pub fn mut_state(&mut self) -> &mut State {
&mut self.state
}
/// Executes the per-loop logic (can be triggered manually so that hypoloop can be tied into external event loops)
pub fn step(&mut self, mut update_callback: impl FnMut(&mut State), mut display_callback: impl FnMut(&mut State)) {
// don't run if the simulation is paused
if self.state.simulate {
// TODO - support frameskips
if !self.realtime || delta_time(self.state.last_tick) >= self.update_interval {
// mutable delta time and timescale for flexibility
let elapsed_time = Instant::now().duration_since(self.state.last_tick);
// update clocks
if self.realtime {
self.state.delta_time = delta_time(self.state.last_tick);
self.state.sim_time += elapsed_time.mul_f32(self.state.timescale);
self.state.irl_time += elapsed_time;
} else {
self.state.delta_time = self.update_interval;
self.state.sim_time += Duration::from_millis(self.update_interval as u64);
self.state.irl_time = Instant::now().duration_since(self.state.clock_start);
}
self.state.timestep = timestep(self.state.delta_time, self.state.timescale);
// update
update_callback(&mut self.state);
// record last tick time
self.state.last_tick = Instant::now();
}
// display
if self.realtime {
display_callback(&mut self.state);
}
}
}
/// Turns real-time mode on/off
pub fn set_realtime(&mut self, realtime: bool) {
self.realtime = realtime;
}
/// Returns the "update interval", the minimum time (in ms) which will elapse between update ticks
pub fn get_update_interval(self) -> u32 {
self.update_interval
}
/// Changes the update interval
pub fn set_update_interval(&mut self, update_interval: u32) {
self.update_interval = update_interval;
}
}
// gets the real time (in ms) that's elapsed since the earlier Instant
fn delta_time(earlier: Instant) -> u32 {
Instant::now().duration_since(earlier).as_millis() as u32
}
// returns the fractional timestep (in s) based on delta time and timescale
fn timestep(delta_time: u32, timescale: f32) -> f32 {
delta_time as f32 / 1000.0 * timescale
}
}

198
src/main.rs Normal file
View File

@ -0,0 +1,198 @@
use core::time;
use std::{ops::Bound, time::{Duration, Instant}};
use rand::Rng;
// loop constants
const UPDATE_INTERVAL: u32 = 40;
// debug constants
const DEBUG_LOOP: bool = false;
const DEBUG_TIME: bool = false;
const DEBUG_PARTICLES: bool = true;
// scene constants
const TIMESCALE: f32 = 1.0;
const SIM_BOUNDS: BoundingBox = BoundingBox {
size: [10.0, 10.0, 10.0],
offset: [-5.0, -5.0, 0.0]
};
const GRAVITY: f32 = -9.8;
const PARTICLE_COUNT: i32 = 1;
// a struct made for defining a bounding box
#[derive(Copy, Clone)]
struct BoundingBox {
size: [f32; 3],
offset: [f32; 3]
}
impl BoundingBox {
// return the lower bounds
fn lower(&self) -> [f32; 3] {
self.offset
}
// return the upper bounds
fn upper(&self) -> [f32; 3] {
let mut bounds: [f32; 3] = [0.0; 3];
for i in 0..3 {
bounds[i] = self.size[i] + self.offset[i];
}
bounds
}
}
#[derive(Copy, Clone)]
struct PlaneCollider {
location: [f32; 3],
normal: [f32; 3]
}
impl PlaneCollider {
// returns if a location is outside the plane's space (past the plane in the direction of the plane's normal)
fn is_outside(&self, point: [f32; 3]) -> bool {
true
}
}
// a struct made for physics particles
#[derive(Copy, Clone)]
struct Particle {
location: [f32; 3],
velocity: [f32; 3],
acceleration: [f32; 3],
gravity_enabled: bool
}
fn main() {
let mut simulate: bool = true;
// start the clock to keep track of real time
let clock_start = Instant::now();
// keep track of the last tick time
let mut last_tick = Instant::now();
// a vector of particles
let mut particles: Vec<Particle> = vec![];
for i in 0..PARTICLE_COUNT {
// each particle spawned at a random location within the sim bounds
let new_particle = Particle {
location: [0.0, 0.0, 5.0],
velocity: [0.0; 3],
acceleration: random_vector3([100.0; 3], [-50.0; 3]),
gravity_enabled: false
};
particles.push(new_particle)
}
while simulate {
// update
if delta_time(last_tick) >= UPDATE_INTERVAL {
// DEBUG
if DEBUG_TIME {
println!("Real time: {}ms | Delta time: {}ms", delta_time(clock_start), delta_time(last_tick));
}
if DEBUG_PARTICLES {
for i in 0..particles.len() {
println!("Time: {}ms | Delta time: {} | Particle {} Location: {:?}, Velocity: {:?}, Acceleration: {:?}", delta_time(clock_start), delta_time(last_tick), i, particles[i].location, particles[i].velocity, particles[i].acceleration);
}
}
update(delta_time(last_tick), &mut particles);
// record last tick time
last_tick = Instant::now();
}
//display(delta_time, &test_particle);
}
}
// update function
// TODO - I think I'm resetting delta time in the wrong place or just using it incorrectly; frameskips aren't happening at all
fn update(delta_time: u32, particles: &mut Vec<Particle>) {
if DEBUG_LOOP {
println!("Updating");
}
// calculate the exact timestep (fractional time in seconds) from delta time
let timestep: f32 = delta_time as f32 / 1000.0;
for particle in particles.iter_mut() {
// add gravitational constant to instantaneous acceleration
if particle.gravity_enabled {
// acceleration += constant
particle.acceleration[2] += GRAVITY;
}
// update velocity
for i in 0..3 {
// velocity += timestep * acceleration
particle.velocity[i] = timestep.mul_add(particle.acceleration[i], particle.velocity[i]);
// kill instantaneous acceleration
particle.acceleration[i] = 0.0;
}
// update location
for i in 0..3 {
// location += timestep * velocity
particle.location[i] = timestep.mul_add(particle.velocity[i], particle.location[i]);
}
// prevent the particle from exiting the sim bounds by clamping its position and killing its velocity upon "hitting" a wall
let upper_bounds = SIM_BOUNDS.upper();
let lower_bounds = SIM_BOUNDS.lower();
for i in 0..3 {
if particle.location[i] >= upper_bounds[i] {
particle.location[i] = upper_bounds[i];
particle.velocity[i] = 0.0;
} else if particle.location[i] <= lower_bounds[i] {
particle.location[i] = lower_bounds[i];
particle.velocity[i] = 0.0;
}
}
}
}
// render function
fn display(delta_time: u32, particle: &Particle) {
// calculate interpolation via delta time
let interpolation: f32 = delta_time as f32 / UPDATE_INTERVAL as f32;
// DEBUG
if DEBUG_LOOP {
println!("Displaying | Delta Time: {}ms | Relative Time: {}", delta_time, interpolation);
}
// DEBUG
// println!("test_particle | Location: {:?} | Velocity: {:?}", render_particle.location, render_particle.velocity);
}
// 1D linear interpolation
fn lerp_1d(x: f64, y: f64, a: f64) -> f64 {
x + ((y - x) * a)
}
// returns a vector3 with random components
fn random_vector3(scale: [f32; 3], offset: [f32; 3]) -> [f32; 3] {
let mut rng = rand::thread_rng();
let mut vector3: [f32; 3] = [0.0; 3];
for i in 0..3 {
let component = rng.gen::<f32>();
vector3[i] = component.mul_add(scale[i], offset[i]);
}
vector3
}
// gets the time in milliseconds that's elapsed since the earlier Instant
fn delta_time(earlier: Instant) -> u32 {
Instant::now().duration_since(earlier).as_millis() as u32
}