summaryrefslogtreecommitdiff
path: root/physics/src/particle_system.rs
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2024-12-15 13:17:43 +0100
committereug-vs <eugene@eug-vs.xyz>2024-12-15 13:17:43 +0100
commit70afc5a7d871919776a64782e8b93404e6b0defd (patch)
treec3d8a273bddf4cbc3c55d06c751766b93b961a1f /physics/src/particle_system.rs
parent297efa5127e83bea57132c503680dd348a725db5 (diff)
downloadparticle-physics-70afc5a7d871919776a64782e8b93404e6b0defd.tar.gz
feat!: add raylib rendering
Diffstat (limited to 'physics/src/particle_system.rs')
-rw-r--r--physics/src/particle_system.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/physics/src/particle_system.rs b/physics/src/particle_system.rs
new file mode 100644
index 0000000..30255ec
--- /dev/null
+++ b/physics/src/particle_system.rs
@@ -0,0 +1,53 @@
+use crate::{
+ algebra::{Point, Scalar, Vector},
+ constraint::Constraint,
+ force::Force,
+};
+
+#[derive(Debug)]
+pub struct Particle {
+ pub mass: Scalar,
+ pub position: Point,
+ pub velocity: Vector,
+
+ /// Force accumulator
+ pub force: Vector,
+}
+
+impl Particle {
+ pub fn new(position: Point, mass: Scalar) -> Self {
+ Self {
+ mass,
+ position,
+ velocity: Vector::zeros(),
+ force: Vector::zeros(),
+ }
+ }
+
+ pub fn apply_force(&mut self, force: Vector) {
+ self.force += force;
+ }
+ pub fn reset_force(&mut self) {
+ self.force = Vector::zeros()
+ }
+}
+
+// #[derive(Debug)]
+pub struct ParticleSystem {
+ pub particles: Vec<Particle>,
+ pub constraints: Vec<Box<dyn Constraint>>,
+ pub forces: Vec<Box<dyn Force>>,
+
+ /// Simulation clock
+ pub t: Scalar,
+}
+
+impl ParticleSystem {
+ pub fn get_kinetic_energy(&self) -> Scalar {
+ self.particles.iter().fold(0.0, |acc, p| {
+ let velocity = p.velocity.norm();
+ let energy = p.mass * velocity * velocity / 2.0;
+ acc + energy
+ })
+ }
+}