added colliders

This commit is contained in:
skyeshroom 2021-08-10 13:24:16 -07:00
parent e3818ea2c9
commit 65829c0d7c

View File

@ -1,5 +1,5 @@
use core::time; use core::time;
use std::time::{Duration, Instant}; use std::{ops::Bound, time::{Duration, Instant}};
use rand::Rng; use rand::Rng;
// loop constants // loop constants
@ -12,9 +12,51 @@ const DEBUG_PARTICLES: bool = true;
// scene constants // scene constants
const TIMESCALE: f32 = 1.0; 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 GRAVITY: f32 = -9.8;
const PARTICLE_COUNT: i32 = 1; 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 // a struct made for physics particles
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
struct Particle { struct Particle {
@ -30,20 +72,18 @@ fn main() {
// start the clock to keep track of real time // start the clock to keep track of real time
let clock_start = Instant::now(); let clock_start = Instant::now();
// track the end time of the last loop
let mut end_time = Instant::now();
// keep track of the last tick time // keep track of the last tick time
let mut last_tick = Instant::now(); let mut last_tick = Instant::now();
// a vector of particles // a vector of particles
let mut particles: Vec<Particle> = vec![]; let mut particles: Vec<Particle> = vec![];
for i in 0..PARTICLE_COUNT { for i in 0..PARTICLE_COUNT {
// each particle spawned at a random location within the sim bounds
let new_particle = Particle { let new_particle = Particle {
location: [0.0, 0.0, 10.0], location: [0.0, 0.0, 5.0],
velocity: [0.0; 3], velocity: [0.0; 3],
acceleration: [0.0; 3], acceleration: random_vector3([100.0; 3], [-50.0; 3]),
gravity_enabled: true gravity_enabled: false
}; };
particles.push(new_particle) particles.push(new_particle)
@ -104,22 +144,29 @@ fn update(delta_time: u32, particles: &mut Vec<Particle>) {
particle.location[i] = timestep.mul_add(particle.velocity[i], particle.location[i]); particle.location[i] = timestep.mul_add(particle.velocity[i], particle.location[i]);
} }
// prevent the particle from going below the ground plane, and kill downward velocity upon "hitting" the ground plane // prevent the particle from exiting the sim bounds by clamping its position and killing its velocity upon "hitting" a wall
if particle.location[2] <= 0.0 { let upper_bounds = SIM_BOUNDS.upper();
particle.location[2] = 0.0; let lower_bounds = SIM_BOUNDS.lower();
particle.velocity[2] = particle.velocity[2].max(0.0); 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 // render function
fn display(delta_time: Duration, particle: &Particle) { fn display(delta_time: u32, particle: &Particle) {
// calculate interpolation via delta time // calculate interpolation via delta time
let interpolation: f32 = delta_time.as_millis() as f32 / UPDATE_INTERVAL as f32; let interpolation: f32 = delta_time as f32 / UPDATE_INTERVAL as f32;
// DEBUG // DEBUG
if DEBUG_LOOP { if DEBUG_LOOP {
println!("Displaying | Delta Time: {}ms | Relative Time: {}", delta_time.as_millis(), interpolation); println!("Displaying | Delta Time: {}ms | Relative Time: {}", delta_time, interpolation);
} }
// DEBUG // DEBUG
@ -132,14 +179,14 @@ fn lerp_1d(x: f64, y: f64, a: f64) -> f64 {
} }
// returns a vector3 with random components // returns a vector3 with random components
fn make_vector3() -> [f32; 3] { fn random_vector3(scale: [f32; 3], offset: [f32; 3]) -> [f32; 3] {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut vector3: [f32; 3] = [0.0; 3]; let mut vector3: [f32; 3] = [0.0; 3];
for i in 0..3 { for i in 0..3 {
let component = rng.gen::<f32>(); let component = rng.gen::<f32>();
vector3[i] = component; vector3[i] = component.mul_add(scale[i], offset[i]);
} }
vector3 vector3