2024-11-08 09:34:08 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const rl = @import("raylib");
|
|
|
|
const rg = @import("raygui");
|
2024-11-08 15:00:57 +00:00
|
|
|
const pr = @import("primitives.zig");
|
2024-11-28 09:40:05 +00:00
|
|
|
const plot = @import("plot.zig");
|
2024-11-15 12:36:32 +00:00
|
|
|
|
2024-11-28 09:40:05 +00:00
|
|
|
const allocator = std.heap.page_allocator;
|
|
|
|
var prng = std.rand.DefaultPrng.init(124346234556);
|
2024-11-15 12:36:32 +00:00
|
|
|
const rand = prng.random();
|
|
|
|
|
2024-11-28 09:40:05 +00:00
|
|
|
const screenWidth = 1200;
|
|
|
|
const screenHeight = 800;
|
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
fn randMinMaxFloat(min: f32, max: f32) f32 {
|
2024-11-15 12:36:32 +00:00
|
|
|
var r1 = rand.float(f32);
|
2024-11-27 12:57:56 +00:00
|
|
|
r1 = 2 * (r1 - 0.5); //-0.5..0.5
|
|
|
|
return (@abs(min) + @abs(max)) / 2 * r1;
|
2024-11-15 12:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Particle = struct {
|
2024-11-08 15:00:57 +00:00
|
|
|
const Self = @This();
|
2024-11-27 12:57:56 +00:00
|
|
|
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,
|
|
|
|
pub fn new(self: Self) void {
|
2024-11-08 15:00:57 +00:00
|
|
|
_ = self;
|
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
pub fn update(self: *Self) void {
|
2024-11-15 12:36:32 +00:00
|
|
|
self.velocity.add(self.acceleration);
|
|
|
|
self.position.add(self.velocity);
|
2024-11-27 12:57:56 +00:00
|
|
|
if (self.position.a.y > screenHeight) {
|
2024-11-25 14:17:35 +00:00
|
|
|
self.velocity.a.y *= -0.9;
|
2024-11-27 12:57:56 +00:00
|
|
|
// self.acceleration.a.y *= -0.5;
|
2024-11-25 14:17:35 +00:00
|
|
|
}
|
2024-11-15 12:36:32 +00:00
|
|
|
|
|
|
|
if (self.lifespan > 0) {
|
|
|
|
self.lifespan -= 1;
|
2024-11-27 12:57:56 +00:00
|
|
|
} else {
|
2024-11-26 10:40:50 +00:00
|
|
|
self.show = 0;
|
2024-11-15 12:36:32 +00:00
|
|
|
}
|
2024-11-27 12:43:12 +00:00
|
|
|
self.acceleration.a.x = 0;
|
|
|
|
self.acceleration.a.y = 0;
|
|
|
|
self.acceleration.a.z = 0;
|
2024-11-15 15:14:46 +00:00
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
pub fn spawn(self: *Self, xaccel: f32, yaccel: f32) void {
|
|
|
|
const xrnr = randMinMaxFloat(-xaccel, xaccel);
|
|
|
|
const yrnr = randMinMaxFloat(-yaccel, yaccel);
|
2024-11-26 10:40:50 +00:00
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
self.lifespan = rand.intRangeAtMost(u8, 75, 255);
|
2024-11-26 10:40:50 +00:00
|
|
|
self.acceleration.a.x = xrnr;
|
|
|
|
self.acceleration.a.y = yrnr;
|
2024-11-27 12:57:56 +00:00
|
|
|
self.position.a.x = @floatFromInt(rl.getMouseX() - screenWidth / 2);
|
|
|
|
self.position.a.y = @floatFromInt(rl.getMouseY() - screenHeight / 2);
|
2024-11-26 10:40:50 +00:00
|
|
|
self.velocity.a.x = 0;
|
2024-11-27 12:28:55 +00:00
|
|
|
self.velocity.a.y = 0;
|
2024-11-27 12:57:56 +00:00
|
|
|
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);
|
2024-11-26 10:40:50 +00:00
|
|
|
self.show = 1;
|
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
pub fn applyGravity(self: *Self, val: f32) void {
|
2024-11-15 15:14:46 +00:00
|
|
|
self.velocity.a.y += val;
|
2024-11-08 15:00:57 +00:00
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
pub fn applyForce(self: *Self, vec: pr.Vec) void {
|
2024-11-27 13:59:12 +00:00
|
|
|
self.acceleration.a.x += vec.a.x * 0.8;
|
|
|
|
self.acceleration.a.y += vec.a.y * 0.8;
|
2024-11-27 13:48:10 +00:00
|
|
|
self.acceleration.a.z = vec.a.z;
|
2024-11-27 13:52:29 +00:00
|
|
|
self.velocity.a.x *= 0.995;
|
|
|
|
self.velocity.a.y *= 0.995;
|
2024-11-27 10:08:40 +00:00
|
|
|
}
|
2024-11-08 15:00:57 +00:00
|
|
|
};
|
2024-11-26 10:40:50 +00:00
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
const Emitter = struct {
|
|
|
|
particleCount: u32 = 0,
|
|
|
|
particles: []Particle = undefined,
|
2024-11-26 10:40:50 +00:00
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
pub fn init(self: *Emitter) !void {
|
|
|
|
var xrnr = randMinMaxFloat(-1, 1);
|
|
|
|
var yrnr = randMinMaxFloat(-0.5, 0.5);
|
2024-11-28 10:35:04 +00:00
|
|
|
self.particles = try allocator.alloc(Particle, 10000);
|
2024-11-26 10:40:50 +00:00
|
|
|
for (self.particles) |*p| {
|
2024-11-27 12:57:56 +00:00
|
|
|
xrnr = randMinMaxFloat(0, 2);
|
|
|
|
yrnr = randMinMaxFloat(0, 2);
|
2024-11-28 10:35:04 +00:00
|
|
|
p.* = Particle{ .velocity = .{ .a = .{ .x = 0, .y = 0 } }, .acceleration = .{ .a = .{ .x = xrnr, .y = yrnr } } };
|
2024-11-26 10:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
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);
|
2024-11-26 10:40:50 +00:00
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
if (i >= count) {
|
2024-11-26 10:40:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
pub fn update(self: *Emitter, texture: rl.Texture2D) void {
|
|
|
|
var posx: i32 = 0;
|
|
|
|
var posy: i32 = 0;
|
|
|
|
const att: Attractor = .{ .vec = .{ .a = .{ .x = 10, .y = 10, .z = 10 } } };
|
2024-11-27 13:51:03 +00:00
|
|
|
const att2: Attractor = .{ .vec = .{ .a = .{ .x = -200, .y = -200, .z = 0 } } };
|
2024-11-26 10:40:50 +00:00
|
|
|
|
2024-11-27 10:08:40 +00:00
|
|
|
att.draw();
|
2024-11-27 12:28:55 +00:00
|
|
|
att2.draw();
|
2024-11-26 10:40:50 +00:00
|
|
|
for (self.particles) |*p| {
|
2024-11-27 12:57:56 +00:00
|
|
|
if (p.show == 1) {
|
2024-11-27 13:48:10 +00:00
|
|
|
p.applyGravity(0);
|
2024-11-27 10:08:40 +00:00
|
|
|
p.applyForce(att.attract(p.*));
|
2024-11-27 10:12:16 +00:00
|
|
|
p.applyForce(att2.attract(p.*));
|
2024-11-27 12:28:55 +00:00
|
|
|
p.update();
|
2024-11-27 10:08:40 +00:00
|
|
|
|
2024-11-27 08:39:11 +00:00
|
|
|
posx = @intFromFloat(screenWidth / 2 + p.position.a.x);
|
|
|
|
posy = @intFromFloat(screenHeight / 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});
|
2024-11-27 12:57:56 +00:00
|
|
|
rl.drawTexture(texture, posx, posy, rl.Color.white);
|
2024-11-26 10:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-11-27 12:57:56 +00:00
|
|
|
const Attractor = struct {
|
|
|
|
vec: pr.Vec,
|
|
|
|
pub fn attract(self: Attractor, particle: Particle) pr.Vec {
|
2024-11-27 13:48:10 +00:00
|
|
|
const distance = self.vec.sub(particle.position);
|
2024-11-27 13:51:03 +00:00
|
|
|
var force: pr.Vec = undefined;
|
|
|
|
if (distance.a.x != 0 and distance.a.y != 0) {
|
|
|
|
force.a.x = distance.a.x / 200;
|
|
|
|
force.a.y = distance.a.y / 200;
|
2024-11-27 13:48:10 +00:00
|
|
|
force.a.z = 0;
|
2024-11-27 12:28:55 +00:00
|
|
|
}
|
2024-11-27 13:48:10 +00:00
|
|
|
return force;
|
2024-11-27 10:08:40 +00:00
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
pub fn draw(self: Attractor) void {
|
|
|
|
const posx: i32 = @intFromFloat(screenWidth / 2 + self.vec.a.x);
|
|
|
|
const posy: i32 = @intFromFloat(screenHeight / 2 + self.vec.a.y);
|
|
|
|
rl.drawRectangle(posx, posy, 10, 10, rl.Color{ .r = 10, .g = 10, .b = 120, .a = 255 });
|
2024-11-27 10:08:40 +00:00
|
|
|
}
|
|
|
|
};
|
2024-11-27 12:57:56 +00:00
|
|
|
fn ItoF(x: i32) f32 {
|
|
|
|
return @as(f32, @floatFromInt(x));
|
2024-11-25 14:17:35 +00:00
|
|
|
}
|
2024-11-27 12:57:56 +00:00
|
|
|
fn rectangle(x: u16, y: u16, width: u16, height: u16) rl.Rectangle {
|
2024-11-26 13:58:48 +00:00
|
|
|
return rl.Rectangle{ .x = @as(f32, @floatFromInt(x)), .y = @as(f32, @floatFromInt(y)), .width = @as(f32, @floatFromInt(width)), .height = @as(f32, @floatFromInt(height)) };
|
|
|
|
}
|
2024-11-08 09:34:08 +00:00
|
|
|
|
|
|
|
pub fn main() anyerror!void {
|
2024-11-27 12:57:56 +00:00
|
|
|
const rect3 = rectangle(10, 150, 600, 10);
|
|
|
|
const rect4 = rectangle(10, 170, 600, 10);
|
|
|
|
const rect5 = rectangle(10, 190, 600, 10);
|
2024-11-15 15:14:46 +00:00
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
var seed: u64 = undefined;
|
2024-11-15 12:36:32 +00:00
|
|
|
try std.posix.getrandom(std.mem.asBytes(&seed));
|
|
|
|
|
|
|
|
prng = std.rand.DefaultPrng.init(seed);
|
2024-11-08 15:00:57 +00:00
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
var value: f32 = 0;
|
|
|
|
var value2: f32 = 0;
|
|
|
|
var value3: f32 = 0;
|
2024-11-15 12:36:32 +00:00
|
|
|
|
2024-11-26 10:40:50 +00:00
|
|
|
var emitter = Emitter{};
|
|
|
|
try emitter.init();
|
2024-11-08 15:00:57 +00:00
|
|
|
|
2024-11-25 10:17:00 +00:00
|
|
|
//const img : rl.Texture = rl.loadTexture("img.png");
|
2024-11-08 09:34:08 +00:00
|
|
|
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
|
|
|
|
defer rl.closeWindow(); // Close window and OpenGL context
|
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
var texture: rl.Texture2D = undefined;
|
|
|
|
const image: rl.Image = rl.loadImage("resources/blut.png");
|
|
|
|
if (image.width > 0) {
|
2024-11-26 13:58:48 +00:00
|
|
|
texture = rl.loadTextureFromImage(image);
|
2024-11-27 12:57:56 +00:00
|
|
|
} else {
|
|
|
|
std.debug.print("error opening resource", .{});
|
2024-11-26 13:58:48 +00:00
|
|
|
std.posix.exit(1);
|
|
|
|
}
|
|
|
|
rl.unloadImage(image);
|
|
|
|
|
2024-11-08 09:34:08 +00:00
|
|
|
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
|
|
|
2024-11-28 09:40:05 +00:00
|
|
|
const file = try plot.open("plot.dat");
|
|
|
|
var plotStr : []u8 = undefined;
|
|
|
|
var plotStrBuf : [128]u8 = undefined;
|
2024-11-28 10:35:04 +00:00
|
|
|
try plot.log(file,"acceleration, position, velocity, show\n");
|
2024-11-28 09:40:05 +00:00
|
|
|
|
2024-11-08 09:34:08 +00:00
|
|
|
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
|
2024-11-08 15:00:57 +00:00
|
|
|
rl.beginDrawing();
|
|
|
|
defer rl.endDrawing();
|
2024-11-08 09:34:08 +00:00
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
rl.clearBackground(rl.Color{
|
|
|
|
.r = 0,
|
|
|
|
.g = 0,
|
|
|
|
.b = 0,
|
|
|
|
.a = 255,
|
2024-11-08 15:00:57 +00:00
|
|
|
});
|
2024-11-08 09:34:08 +00:00
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
_ = rg.guiSlider(rect3, "0", "500", &value, ItoF(0), ItoF(500));
|
2024-11-27 14:01:31 +00:00
|
|
|
_ = rg.guiSlider(rect4, "-3", "3", &value2, ItoF(-100), ItoF(100));
|
|
|
|
_ = rg.guiSlider(rect5, "-3", "3", &value3, -100, 100);
|
2024-11-26 10:40:50 +00:00
|
|
|
|
2024-11-27 12:57:56 +00:00
|
|
|
if (rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) {
|
|
|
|
emitter.emit(@as(u32, @intFromFloat(value)), value2, value3);
|
2024-11-15 12:36:32 +00:00
|
|
|
}
|
2024-11-28 09:40:05 +00:00
|
|
|
|
2024-11-27 09:26:45 +00:00
|
|
|
rl.beginBlendMode(rl.BlendMode.blend_additive);
|
2024-11-28 09:40:05 +00:00
|
|
|
|
2024-11-27 09:26:45 +00:00
|
|
|
rl.endBlendMode();
|
2024-11-28 09:40:05 +00:00
|
|
|
|
2024-11-28 10:35:04 +00:00
|
|
|
plotStr = try std.fmt.bufPrint(&plotStrBuf,"{d}, {d}, {d}, {d}\n",.{emitter.particles[0].acceleration.a.x,emitter.particles[0].position.a.x,emitter.particles[0].velocity.a.x,emitter.particles[0].show});
|
2024-11-28 09:40:05 +00:00
|
|
|
try plot.log(file,plotStr);
|
|
|
|
|
|
|
|
emitter.update(texture);
|
2024-11-08 09:34:08 +00:00
|
|
|
}
|
2024-11-28 09:40:05 +00:00
|
|
|
plot.close(file);
|
2024-11-08 09:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test "simple test" {
|
|
|
|
var list = std.ArrayList(i32).init(std.testing.allocator);
|
|
|
|
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
|
|
|
try list.append(42);
|
|
|
|
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
|
|
|
}
|
2024-11-28 09:40:05 +00:00
|
|
|
|
|
|
|
test "random" {
|
|
|
|
var seed: u64 = undefined;
|
|
|
|
var r1: f32 = undefined;
|
|
|
|
try std.posix.getrandom(std.mem.asBytes(&seed));
|
|
|
|
|
|
|
|
prng = std.rand.DefaultPrng.init(seed);
|
|
|
|
while (true) {
|
|
|
|
r1 = randMinMaxFloat(-10, 10);
|
|
|
|
//std.debug.print("randMinMaxFloat:{}\n",.{r1});
|
|
|
|
if (r1 > 8 or r1 < -8) {
|
|
|
|
std.debug.print("Greater:{}\n", .{r1});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|