Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions src/geometry/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,18 @@ impl Vector3d {
}
}

// Internal helper to get SIMD representation (padding with 0.0)
#[inline(always)]
fn to_simd(&self) -> f32x4 {
f32x4::new([self.x, self.y, self.z, 0.0])
}

// Internal helper to create Vector3d from SIMD
#[inline(always)]
fn from_simd(simd: f32x4) -> Self {
let arr = simd.to_array();
Self {
x: arr[0],
y: arr[1],
z: arr[2],
}
}

// A vector of 3d must have basic arithmetic calculations to represent it's location on the environment
pub fn add(&self, other: &Vector3d) -> Vector3d {
Self::from_simd(self.to_simd() + other.to_simd())
Self { x: self.x + other.x, y: self.y + other.y, z: self.z + other.z }
}

pub fn subtract(&self, other: &Vector3d) -> Vector3d {
Self::from_simd(self.to_simd() - other.to_simd())
Self { x: self.x - other.x, y: self.y - other.y, z: self.z - other.z }
}

// Scalar multiplication has the purpose to control vector speed without changing its direction
pub fn scale(&self, scalar: f32) -> Vector3d {
let scalar_simd = f32x4::splat(scalar);
Self::from_simd(self.to_simd() * scalar_simd)
Self { x: self.x * scalar, y: self.y * scalar, z: self.z * scalar }
}

// This represents the length or size of the vector
Expand All @@ -72,9 +54,7 @@ impl Vector3d {

// Look at where the vector is pointing at and it's relative direction compared to other vectors
pub fn dot(&self, other: &Vector3d) -> f32 {
let mul = self.to_simd() * other.to_simd();
let arr = mul.to_array();
arr[0] + arr[1] + arr[2]
self.x * other.x + self.y * other.y + self.z * other.z
}

// This can be used to calculate many things like perpendicular directions,
Expand Down
2 changes: 1 addition & 1 deletion src/physics/ccd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn calculate_toi_sphere_sphere(
// We want the smallest positive root
let t = (-b - discriminant.d_sqrt()) / (2.0 * a);

if t >= 0.0 && t <= 1.0 {
if (0.0..=1.0).contains(&t) {
Some(t)
} else {
None
Expand Down
4 changes: 2 additions & 2 deletions src/physics/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl GpuContext {
// Dispatch enough workgroups to cover all items (divided by 3 because we process Vector3d).
// Workgroup size in shader is 64.
let items = (input_data.len() / 3) as u32;
let workgroups = (items + 63) / 64;
let workgroups = items.div_ceil(64);
cpass.dispatch_workgroups(workgroups, 1, 1);
}

Expand All @@ -118,7 +118,7 @@ impl GpuContext {
);

// Submit the commands to the queue
let submission_index = self.queue.submit(Some(encoder.finish()));
let _submission_index = self.queue.submit(Some(encoder.finish()));

// Map the staging buffer so we can read it on CPU
let buffer_slice = staging_buffer.slice(..);
Expand Down
2 changes: 1 addition & 1 deletion src/physics/rigid_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn update(components: &mut RigidBodyComponents, index: usize, dt: f32) {
let mass = components.masses[index];
let force = components.forces[index];

let mut accel = force.scale(1.0 / mass);
let accel = force.scale(1.0 / mass);
components.accelerations[index] = accel;

let mut vel = components.velocities[index];
Expand Down
28 changes: 14 additions & 14 deletions src/physics/world.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{geometry::polygon::Polygon, physics::components::RigidBodyComponents, physics::rigid_body};
use crate::{geometry::polygon::Polygon, physics::components::RigidBodyComponents};
use rayon::prelude::*;
use wide::f32x4;
use crate::geometry::vector::Vector3d;
Expand All @@ -10,12 +10,12 @@ pub struct World {
pub next_entity: usize,

// SoA (Struct of Arrays) layout for DOD
pub positions: Vec<Position>,
pub velocities: Vec<Velocity>,
pub accelerations: Vec<Acceleration>,
pub forces: Vec<Force>,
pub masses: Vec<Mass>,
pub shapes: Vec<Shape>,
pub positions: Vec<Vector3d>,
pub velocities: Vec<Vector3d>,
pub accelerations: Vec<Vector3d>,
pub forces: Vec<Vector3d>,
pub masses: Vec<f32>,
pub shapes: Vec<Polygon>,
pub active_entities: Vec<bool>, // true if entity is active
}

Expand Down Expand Up @@ -68,9 +68,9 @@ impl World {
let mut f_z = f32x4::from([forces[0].z, forces[1].z, forces[2].z, forces[3].z]);

// Apply gravity (F = F + mg)
f_x = f_x + (grav_x * mass_simd);
f_y = f_y + (grav_y * mass_simd);
f_z = f_z + (grav_z * mass_simd);
f_x += grav_x * mass_simd ;
f_y += grav_y * mass_simd ;
f_z += grav_z * mass_simd ;

// Calculate acceleration (a = F / m)
let a_x = f_x * inv_mass;
Expand All @@ -83,9 +83,9 @@ impl World {
let mut v_z = f32x4::from([velocities[0].z, velocities[1].z, velocities[2].z, velocities[3].z]);

// Update velocities (v = v + a * dt)
v_x = v_x + (a_x * dt_simd);
v_y = v_y + (a_y * dt_simd);
v_z = v_z + (a_z * dt_simd);
v_x += a_x * dt_simd ;
v_y += a_y * dt_simd ;
v_z += a_z * dt_simd ;

// Store back
let a_x_arr: [f32; 4] = a_x.into();
Expand Down Expand Up @@ -116,7 +116,7 @@ impl World {
for i in start..end {
let mass = self.bodies.masses[i];
let gravity_force = crate::physics::constants::GRAVITY.scale(mass);
let mut force = self.bodies.forces[i].add(&gravity_force);
let force = self.bodies.forces[i].add(&gravity_force);

let accel = force.scale(1.0 / mass);
self.bodies.accelerations[i] = accel;
Expand Down