aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2024-09-09 17:32:19 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2024-09-09 17:32:19 -0500
commit5a0234c0eff069e13fdef204d810d994ab7858f9 (patch)
tree5110248546a7887a4c9a6e886dd6c47d78e450f7
parent3bb4d81a9e32feabd67783e163287c26b519f92d (diff)
downloadparticle-sim-5a0234c0eff069e13fdef204d810d994ab7858f9.tar.gz
UI: Expanded configuration options
-rw-r--r--README.md3
-rw-r--r--build.zig.zon2
-rw-r--r--src/config.zig11
-rw-r--r--src/imgui.zig19
-rw-r--r--src/main.zig3
-rw-r--r--src/particle.zig49
-rw-r--r--src/rules.zig34
7 files changed, 91 insertions, 30 deletions
diff --git a/README.md b/README.md
index a413a95..3605b0f 100644
--- a/README.md
+++ b/README.md
@@ -4,3 +4,6 @@ This is a simple particle simulator written in zig using [Raylib](https://www.ra
- slightly modified rlImgui bindings (commented out 3 random lines)
- zgui
- raylib
+## Running
+Compile with `zig build --release=fast` and run with `./zig-out/bin/particle-sim`
+Alternatively, just run `zig build --release=fast run`
diff --git a/build.zig.zon b/build.zig.zon
index f31e53c..61aaa5e 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -1,6 +1,6 @@
.{
.name = "particle-sim",
- .version = "0.2.0",
+ .version = "0.2.1",
.paths = .{ "src", "vendor" },
.dependencies = .{
.@"raylib-zig" = .{
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