From 5a0234c0eff069e13fdef204d810d994ab7858f9 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Mon, 9 Sep 2024 17:32:19 -0500 Subject: UI: Expanded configuration options --- src/config.zig | 11 ++++++----- src/imgui.zig | 19 ++++++++++++++++--- src/main.zig | 3 ++- src/particle.zig | 49 +++++++++++++++++++++++++++++-------------------- src/rules.zig | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/config.zig b/src/config.zig index 36de99a..fd088ea 100644 --- a/src/config.zig +++ b/src/config.zig @@ -7,11 +7,14 @@ pub const screenHeight = 1080; pub const particleMax = 10000; pub const initialParticles = 2000; pub const colorAmnt = colors.len; -pub const numThreads = 12; +pub const numThreads = 16; pub var particleCount: i32 = initialParticles; -pub var radius: f32 = 100.0; pub var minDistance: f32 = 20.0; +pub var friction: f32 = 0.95; +pub var radius: [colorAmnt]f32 = undefined; +pub var speed: [colorAmnt]i32 = undefined; +pub var rules: [colorAmnt][colorAmnt]f32 = undefined; pub var colors = [_]rl.Color{ rl.Color.red, rl.Color.green, @@ -23,7 +26,7 @@ pub var colors = [_]rl.Color{ rl.Color.gray, }; -pub fn customColors() [8]rl.Color { +pub fn customColors() [colorAmnt]rl.Color { return .{ rl.getColor(0xF38BA8FF), rl.getColor(0xA6E3A1FF), @@ -35,5 +38,3 @@ pub fn customColors() [8]rl.Color { rl.getColor(0xCBA6F7FF), }; } - -pub var rules: [colorAmnt][colorAmnt]f32 = undefined; diff --git a/src/imgui.zig b/src/imgui.zig index 0796cdb..bcc6b32 100644 --- a/src/imgui.zig +++ b/src/imgui.zig @@ -20,12 +20,25 @@ pub fn update(alloc: std.mem.Allocator, buf: [:0]u8) !void { if (z.collapsingHeader("General Settings", .{ .default_open = true })) { if (z.button("Reset", .{})) { cfg.particleCount = cfg.initialParticles; - cfg.radius = 100.0; + // cfg.radius = 100.0; cfg.minDistance = 20.0; } _ = z.sliderInt("Particles", .{ .v = &cfg.particleCount, .min = 1, .max = cfg.particleMax }); - _ = z.sliderFloat("Radius", .{ .v = &cfg.radius, .min = cfg.minDistance, .max = 500 }); - _ = z.sliderFloat("Minimum Distance", .{ .v = &cfg.minDistance, .min = 1.0, .max = cfg.radius }); + _ = z.sliderFloat("Friction", .{ .v = &cfg.friction, .min = 0, .max = 1 }); + // _ = z.sliderFloat("Radius", .{ .v = &cfg.radius, .min = cfg.minDistance, .max = 500 }); + _ = z.sliderFloat("Minimum Distance", .{ .v = &cfg.minDistance, .min = 1.0, .max = 500 }); + } + if (z.collapsingHeader("Radius", .{ .default_open = true })) { + for (&cfg.radius, 0..) |*r, i| { + const str = try std.fmt.allocPrintZ(alloc, "{s} Radius", .{rul.colorToString(i)}); + _ = z.sliderFloat(str, .{ .v = r, .min = cfg.minDistance, .max = 500 }); + } + } + if (z.collapsingHeader("Speed", .{ .default_open = true })) { + for (&cfg.speed, 0..) |*s, i| { + const str = try std.fmt.allocPrintZ(alloc, "{s} Speed", .{rul.colorToString(i)}); + _ = z.sliderInt(str, .{ .v = s, .min = 1, .max = 1000 }); + } } if (z.collapsingHeader("Ruleset", .{ .default_open = true })) { _ = z.beginTable("Rules", .{ diff --git a/src/main.zig b/src/main.zig index b0705be..00dd477 100644 --- a/src/main.zig +++ b/src/main.zig @@ -47,7 +47,8 @@ pub fn main() !void { while (!rl.windowShouldClose()) { if (particles.items(.x).len < cfg.particleCount) { for (0..@intCast(cfg.particleCount - @as(i32, @intCast(particles.items(.x).len)))) |_| { - std.debug.print("without this print statement it breaks on arm idk why {d}\n", .{cfg.particleCount}); + //std.debug.print("without this print statement it breaks on arm idk why {d}\n", .{cfg.particleCount}); + _ = cfg.particleCount; try particles.append(gpa.allocator(), part.createParticle()); } } diff --git a/src/particle.zig b/src/particle.zig index 70261b8..6768869 100644 --- a/src/particle.zig +++ b/src/particle.zig @@ -29,13 +29,14 @@ pub fn updateVelocities( var xvel = particles.items(.xvel); var yvel = particles.items(.yvel); var i: usize = threadidx; - while (i <= particles.len) : (i += cfg.numThreads) { + while (i < particles.len) : (i += cfg.numThreads) { const p = particles.get(i); + const radius = cfg.radius[p.colorId]; var forceX: f32 = 0.0; var forceY: f32 = 0.0; var j: usize = threadidx; - while (j <= particles.len) : (j += 1) { + while (j < particles.len) : (j += 1) { const p2 = particles.get(j); if (i == j) continue; var check2x = p.x - rl.getScreenWidth(); @@ -51,33 +52,41 @@ pub fn updateVelocities( if (@abs(distance_x) > @abs(check2rx)) distance_x = check2rx; if (@abs(distance_y) > @abs(check2ry)) distance_y = check2ry; - if (distance_x > cfg.radius or distance_y > cfg.radius) continue; + if (distance_x > radius or distance_y > radius) continue; var distance = @sqrt(distance_x * distance_x + distance_y * distance_y); if (distance == 0) distance = 0.0001; - if (distance > 0 and distance < cfg.radius) { - const f = -force(distance, rules[colorList[i]][colorList[j]]); + if (distance > 0 and distance < radius) { + const f = -force(distance, radius, rules[colorList[i]][colorList[j]]); forceX += (distance_x / distance) * f; forceY += (distance_y / distance) * f; } } - forceX = forceX * cfg.minDistance / cfg.radius; - forceY = forceY * cfg.minDistance / cfg.radius; + forceX = forceX * cfg.minDistance / radius; + forceY = forceY * cfg.minDistance / radius; - xvel[i] = xvel[i] * 0.95 + forceX; - yvel[i] = yvel[i] * 0.95 + forceY; + xvel[i] = xvel[i] * cfg.friction + forceX; + yvel[i] = yvel[i] * cfg.friction + forceY; } } /// Applies the particles velocity and updates position pub fn updatePosition(particles: std.MultiArrayList(particle)) void { - for (particles.items(.y), particles.items(.yvel)) |*y, yvel| // (y + yvel) % screenHeight - y.* = @mod(@as(i32, @intFromFloat(@round((@as(f32, @floatFromInt(y.*)) + yvel)))), rl.getScreenHeight()); - - for (particles.items(.x), particles.items(.xvel)) |*x, xvel| // (x + xvel) % screenWidth - x.* = @mod(@as(i32, @intFromFloat(@round((@as(f32, @floatFromInt(x.*)) + xvel)))), rl.getScreenWidth()); + for ( + particles.items(.colorId), + particles.items(.y), + particles.items(.yvel), + ) |col, *y, yvel| // (y + yvel) % screenHeight + y.* = @intFromFloat(@round(@as(f32, @floatFromInt(@mod(@as(i32, @intFromFloat(@round((@as(f32, @floatFromInt(y.*)) + (@as(f32, @floatFromInt(cfg.speed[col])) / 1000.0) * yvel)))), rl.getScreenHeight()))))); + + for ( + particles.items(.colorId), + particles.items(.x), + particles.items(.xvel), + ) |col, *x, xvel| // (y + yvel) % screenHeight + x.* = @intFromFloat(@round(@as(f32, @floatFromInt(@mod(@as(i32, @intFromFloat(@round((@as(f32, @floatFromInt(x.*)) + (@as(f32, @floatFromInt(cfg.speed[col])) / 1000.0) * xvel)))), rl.getScreenWidth()))))); } /// Draw the particles onto the screen using raylib @@ -86,9 +95,9 @@ pub fn draw(particles: std.MultiArrayList(particle)) void { rl.drawRectangle(x.*, y.*, 5, 5, cfg.colors[colorId]); } -fn force(distance: f32, attraction: f32) f32 { - const beta = cfg.minDistance / cfg.radius; - const r: f32 = distance / cfg.radius; +fn force(distance: f32, radius: f32, attraction: f32) f32 { + const beta = cfg.minDistance / radius; + const r: f32 = distance / radius; if (r < beta) return ((beta - r) / (beta - 1.0)); if (beta <= r and r < 1) @@ -114,10 +123,10 @@ pub fn createParticle() particle { //TODO: Create tests test "Force values" { const expect = std.testing.expect; - cfg.radius = 50; + const radius = 50; cfg.minDistance = 20; - const belowMin = force(5.0, 0.5); - const aboveMin = force(25.0, 0.5); + const belowMin = force(5.0, radius, 0.5); + const aboveMin = force(25.0, radius, 0.5); try expect(aboveMin > 0); try expect(belowMin < 0); } diff --git a/src/rules.zig b/src/rules.zig index af299ec..fa3d5cb 100644 --- a/src/rules.zig +++ b/src/rules.zig @@ -13,6 +13,8 @@ pub fn ruleMatrix() [cfg.colorAmnt][cfg.colorAmnt]f32 { if (isNeg == 1) val = 0 - val; rules[i][j] = val; } + cfg.radius[i] = prng.random().float(f32) * 500; + cfg.speed[i] = prng.random().intRangeAtMost(i32, 1, 1000); } return rules; } @@ -46,6 +48,28 @@ pub fn loadRules(allocator: std.mem.Allocator, absolutePath: [:0]u8) !void { } try reader.skipBytes(1, .{}); } + for (&cfg.speed) |*s| { + const buf = try reader.readUntilDelimiterAlloc(allocator, ',', 16); + defer allocator.free(buf); + s.* = try std.fmt.parseInt(i32, buf, 10); + } + try reader.skipBytes(1, .{}); + for (&cfg.radius) |*r| { + const buf = try reader.readUntilDelimiterAlloc(allocator, ',', 16); + defer allocator.free(buf); + r.* = try std.fmt.parseFloat(f32, buf); + } + try reader.skipBytes(1, .{}); + { + const buf = try reader.readUntilDelimiterAlloc(allocator, ',', 16); + defer allocator.free(buf); + cfg.minDistance = try std.fmt.parseFloat(f32, buf); + } + { + const buf = try reader.readUntilDelimiterAlloc(allocator, ',', 16); + defer allocator.free(buf); + cfg.friction = try std.fmt.parseFloat(f32, buf); + } } /// Save rules to a csv @@ -59,6 +83,16 @@ pub fn saveRules(absolutePath: [:0]u8) !void { } _ = try writer.write("\n"); } + for (cfg.speed) |s| { + try writer.print("{d},", .{s}); + } + _ = try writer.write("\n"); + for (cfg.radius) |r| { + try writer.print("{d:.3},", .{r}); + } + _ = try writer.write("\n"); + try writer.print("{d:.3},", .{cfg.minDistance}); + try writer.print("{d:.3},", .{cfg.friction}); } /// Convert the color index to a string -- cgit v1.2.3