const pr = @import("primitives.zig"); const utils = @import("utils.zig"); const std = @import("std"); const rl = @import("raylib"); var prng = std.rand.DefaultPrng.init(124346234556); const rand = prng.random(); pub const Particle = struct { const Self = @This(); position: pr.Vec = .{ .a = .{ .x = 0, .y = 0, .z = 0 } }, velocity: pr.Vec = .{ .a = .{ .x = 0, .y = 0, .z = 0 } }, acceleration: pr.Vec = .{ .a = .{ .x = 0, .y = 0, .z = 0 } }, lifespan: u8 = 0, show: u8 = 0, size: f32 = 0, max_x: i32 = 0, max_y: i32 = 0, pub fn new(self: *Self, max_x: i32, max_y: i32) void { self.max_x = max_x; self.max_y = max_y; } pub fn update(self: *Self) void { self.velocity.add(self.acceleration); self.position.add(self.velocity); //this is ambigous //must fix!! if (self.position.a.y > @as(f32, @floatFromInt(self.max_y))) { self.velocity.a.y *= -0.9; // self.acceleration.a.y *= -0.5; } if (self.lifespan > 0) { self.lifespan -= 1; } else { self.show = 0; } self.acceleration.a.x = 0; self.acceleration.a.y = 0; self.acceleration.a.z = 0; } pub fn spawn(self: *Self, xaccel: f32, yaccel: f32) void { const xrnr = utils.randMinMaxFloat(-xaccel, xaccel); const yrnr = utils.randMinMaxFloat(-yaccel, yaccel); self.size = utils.randMinMaxFloat(-2, 2); self.lifespan = rand.intRangeAtMost(u8, 75, 255); self.acceleration.a.x = xrnr; self.acceleration.a.y = yrnr; self.position.a.x = @floatFromInt(rl.getMouseX() - @divTrunc(self.max_x, 2)); self.position.a.y = @floatFromInt(rl.getMouseY() - @divTrunc(self.max_y, 2)); self.velocity.a.x = 0; self.velocity.a.y = 0; self.position.a.color.r = rand.intRangeAtMost(u8, 0, 30); self.position.a.color.g = rand.intRangeAtMost(u8, 0, 255); self.position.a.color.b = rand.intRangeAtMost(u8, 0, 255); self.show = 1; } pub fn applyGravity(self: *Self, val: f32) void { self.velocity.a.y += val; } pub fn applyForce(self: *Self, vec: pr.Vec) void { self.acceleration.a.x += vec.a.x * 0.8; self.acceleration.a.y += vec.a.y * 0.8; self.acceleration.a.z = vec.a.z; self.velocity.a.x *= 0.995; self.velocity.a.y *= 0.995; } }; pub const Emitter = struct { particleCount: u32 = 0, particles: []Particle = undefined, max_x: i32 = 0, max_y: i32 = 0, pub fn init(self: *Emitter, allocator: std.mem.Allocator, max_x: i32, max_y: i32) !void { var xrnr = utils.randMinMaxFloat(-1, 1); var yrnr = utils.randMinMaxFloat(-0.5, 0.5); self.max_x = max_x; self.max_y = max_y; self.particles = try allocator.alloc(Particle, 500000); for (self.particles) |*p| { xrnr = utils.randMinMaxFloat(-2, 2); yrnr = utils.randMinMaxFloat(-2, 2); p.* = Particle{ .velocity = .{ .a = .{ .x = 0, .y = 0 } }, .acceleration = .{ .a = .{ .x = xrnr, .y = yrnr } } }; p.*.new(self.max_x, self.max_y); } } pub fn emit(self: *Emitter, count: u32, xaccel: f32, yaccel: f32) void { var i: u32 = 0; for (self.particles) |*p| { if (p.show == 0) { i += 1; p.spawn(xaccel, yaccel); } if (i >= count) { break; } } } pub fn update(self: *Emitter, att: []Attractor, texture: rl.Texture2D, color: rl.Color) void { var posx: i32 = 0; var posy: i32 = 0; var vec2: rl.Vector2 = undefined; if (rl.isMouseButtonPressed(rl.MouseButton.mouse_button_right)) { for (att) |*at| { if (at.init_done != 1) { at.init(.{ .a = .{ .x = @floatFromInt(rl.getMouseX() - @divTrunc(self.max_x, 2)), .y = @floatFromInt(rl.getMouseY() - @divTrunc(self.max_y, 2)), .z = 0 } }, self.max_x, self.max_y); at.mode = 1; break; } } } if (rl.isMouseButtonPressed(rl.MouseButton.mouse_button_middle)) { for (att) |*at| { if (at.init_done != 1) { at.init(.{ .a = .{ .x = @floatFromInt(rl.getMouseX() - @divTrunc(self.max_x, 2)), .y = @floatFromInt(rl.getMouseY() - @divTrunc(self.max_y, 2)), .z = 0 } }, self.max_x, self.max_y); at.mode = 0; break; } } } for (att) |at| { at.draw(); } for (self.particles) |*p| { if (p.show == 1) { //p.applyGravity(2); for (att) |at| { if (at.mode == 0) { p.applyForce(at.attract(p.*)); } else { p.applyForce(at.repel(p.*)); } } p.update(); if (p.position.a.x < 10000 and p.position.a.x > -10000) { posx = @intFromFloat(@as(f32, @floatFromInt(self.max_x)) / 2 + p.position.a.x); } if (p.position.a.y < 10000 and p.position.a.y > -10000) { posy = @intFromFloat(@as(f32, @floatFromInt(self.max_y)) / 2 + p.position.a.y); } //rl.drawRectangle(posx,posy,2,2,rl.Color{.r=p.position.a.color.r,.g=p.position.a.color.g,.b=p.position.a.color.b,.a=255}); vec2 = .{ .x = (@as(f32, @floatFromInt(self.max_x)) / 2 + p.position.a.x), .y = (@as(f32, @floatFromInt(self.max_y)) / 2 + p.position.a.y) }; rl.drawTextureEx(texture, vec2, 0.5, p.size, color); } } } }; pub const Attractor = struct { vec: pr.Vec, init_done: u8 = 0, mode: u8 = 0, max_x: i32 = 0, max_y: i32 = 0, pub fn init(self: *Attractor, vec: pr.Vec, max_x: i32, max_y: i32) void { self.vec = vec; self.init_done = 1; self.max_x = max_x; self.max_y = max_y; } pub fn attract(self: Attractor, particle: Particle) pr.Vec { if (self.init_done == 1) { const distance = self.vec.sub(particle.position); var force: pr.Vec = undefined; if (distance.a.x != 0 and distance.a.y != 0) { force.a.x = distance.a.x / 10 * @abs(1 / distance.a.x); force.a.y = distance.a.y / 10 * @abs(1 / distance.a.y); force.a.z = 0; } return force; } else { return .{ .a = .{ .x = 0, .y = 0, .z = 0 } }; } } pub fn repel(self: Attractor, particle: Particle) pr.Vec { if (self.init_done == 1) { const distance = self.vec.sub(particle.position); var force: pr.Vec = undefined; if (distance.a.x != 0 and distance.a.y != 0) { force.a.x = -distance.a.x / 10 * @abs(1 / distance.a.x); force.a.y = -distance.a.y / 10 * @abs(1 / distance.a.y); force.a.z = 0; } return force; } else { return .{ .a = .{ .x = 0, .y = 0, .z = 0 } }; } } pub fn draw(self: Attractor) void { const posx: i32 = @intFromFloat(@as(f32, @floatFromInt(self.max_x)) / 2 + self.vec.a.x); const posy: i32 = @intFromFloat(@as(f32, @floatFromInt(self.max_y)) / 2 + self.vec.a.y); if (self.init_done == 1) { rl.drawRectangle(posx, posy, 10, 10, rl.Color{ .r = 10, .g = 10, .b = 120, .a = 255 }); } } };