aboutsummaryrefslogtreecommitdiff
path: root/src/particle.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2024-07-18 01:09:46 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2024-07-18 01:09:46 -0500
commit5b7e264d3a11baebd7fedb63f104959ccdc6ad65 (patch)
tree30777da6697297b172fae4c0b3f2f5c311a70385 /src/particle.zig
parenta805bb704a67d112146c5ea6544d0c14ffa2f5ef (diff)
downloadparticle-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/particle.zig')
-rw-r--r--src/particle.zig27
1 files changed, 24 insertions, 3 deletions
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