diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-07-18 01:09:46 -0500 | 
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-07-18 01:09:46 -0500 | 
| commit | 5b7e264d3a11baebd7fedb63f104959ccdc6ad65 (patch) | |
| tree | 30777da6697297b172fae4c0b3f2f5c311a70385 /src | |
| parent | a805bb704a67d112146c5ea6544d0c14ffa2f5ef (diff) | |
| download | particle-sim-5b7e264d3a11baebd7fedb63f104959ccdc6ad65.tar.gz | |
fix(force calculation): Added calculations for wrapping
Introduces a new issue with performance, need to add multithreading in the next update
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.zig | 3 | ||||
| -rw-r--r-- | src/particle.zig | 27 | 
2 files changed, 26 insertions, 4 deletions
| diff --git a/src/config.zig b/src/config.zig index 162f71c..06d60bb 100644 --- a/src/config.zig +++ b/src/config.zig @@ -5,6 +5,8 @@ pub const screenWidth = 2560;  pub const screenHeight = 1440;  pub const particleMax = 4000;  pub const initialParticles = 3000; +pub const colorAmnt = colors.len; +  pub var particleCount: i32 = initialParticles;  pub var radius: f32 = 100.0;  pub var minDistance: f32 = 20.0; @@ -18,5 +20,4 @@ pub const colors = [_]rl.Color{      rl.Color.orange,      rl.Color.gray,  }; -pub const colorAmnt = colors.len;  pub var rules: [colorAmnt][colorAmnt]f32 = undefined; diff --git a/src/particle.zig b/src/particle.zig index bf36cfb..7b16182 100644 --- a/src/particle.zig +++ b/src/particle.zig @@ -23,9 +23,28 @@ pub fn updateVelocities(particles: std.MultiArrayList(particle), rules: [cfg.col          for (particles.items(.x), particles.items(.y), 0..) |x2, y2, j| {              if (i == j) continue; -            const rx: f32 = @floatFromInt(x - x2); -            const ry: f32 = @floatFromInt(y - y2); -            var r = @sqrt(rx * rx + ry * ry); +            var check2x = x - cfg.screenWidth; +            var check2y = y - cfg.screenWidth; +            if (x < cfg.screenWidth / 2) +                check2x = x + cfg.screenWidth; +            if (y < cfg.screenHeight / 2) +                check2y = y + cfg.screenHeight; + +            var rx: f32 = @floatFromInt(x - x2); +            var ry: f32 = @floatFromInt(y - y2); +            const check2rx: f32 = @floatFromInt(check2x - x2); +            const check2ry: f32 = @floatFromInt(check2y - y2); + +            const r1 = @sqrt(rx * rx + ry * ry); +            const r2 = @sqrt(rx * rx + check2ry * check2ry); +            const r3 = @sqrt(check2rx * check2rx + check2ry * check2ry); +            const r4 = @sqrt(check2rx * check2rx + ry * ry); + +            var r = @min(@min(r1, r2), @min(r3, r4)); + +            if (@abs(rx) > @abs(check2rx)) rx = check2rx; +            if (@abs(ry) > @abs(check2ry)) ry = check2ry; +              if (r == 0) {                  r = 0.0001;              } @@ -91,3 +110,5 @@ pub fn createParticle() particle {          .yvel = 0,      };  } + +//TODO: Create tests | 
