From 8344fe2ae2aaad616a2aa4ddecb1adb3741199e4 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Wed, 4 Sep 2024 15:03:54 -0500 Subject: perf: Added a stop if particles aren't close avoids excessive multiplication. --- build.zig | 2 +- src/config.zig | 8 ++++---- src/particle.zig | 25 +++++++++---------------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/build.zig b/build.zig index 8c5f1fe..bc4905a 100644 --- a/build.zig +++ b/build.zig @@ -2,7 +2,7 @@ const std = @import("std"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSafe }); + const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseFast }); const exe = b.addExecutable(.{ .name = "particle-sim", diff --git a/src/config.zig b/src/config.zig index 06d60bb..98eab62 100644 --- a/src/config.zig +++ b/src/config.zig @@ -1,10 +1,10 @@ const rl = @import("raylib"); const part = @import("particle.zig"); -pub const screenWidth = 2560; -pub const screenHeight = 1440; -pub const particleMax = 4000; -pub const initialParticles = 3000; +pub const screenWidth = 2880; +pub const screenHeight = 1620; +pub const particleMax = 5000; +pub const initialParticles = 2000; pub const colorAmnt = colors.len; pub var particleCount: i32 = initialParticles; diff --git a/src/particle.zig b/src/particle.zig index 7b16182..5c97ee5 100644 --- a/src/particle.zig +++ b/src/particle.zig @@ -6,9 +6,9 @@ const rl = @import("raylib"); pub fn initParticles(allocator: std.mem.Allocator, amnt: u32) !std.MultiArrayList(particle) { var particles = std.MultiArrayList(particle){}; try particles.setCapacity(allocator, cfg.particleMax); - for (0..amnt) |_| { + for (0..amnt) |_| try particles.append(allocator, createParticle()); - } + return particles; } @@ -25,29 +25,22 @@ pub fn updateVelocities(particles: std.MultiArrayList(particle), rules: [cfg.col if (i == j) continue; 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; + 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; - } + if (rx > cfg.radius or ry > cfg.radius) continue; + + var r = @sqrt(rx * rx + ry * ry); + + if (r == 0) r = 0.0001; if (r > 0 and r < cfg.radius) { const f = force(r, rules[colorList[i]][colorList[j]]); forceX = forceX + rx / r * f; -- cgit v1.2.3