Compare commits
46 Commits
bd824f8a10
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76fde82477 | ||
|
|
e07b6caa19 | ||
|
|
ec5fc69ae5 | ||
|
|
7fbd24b85d | ||
|
|
af65248181 | ||
|
|
c6cb39e3b0 | ||
| ea5cda0635 | |||
|
|
899a796c99 | ||
|
|
34c79af668 | ||
| 4f66e7cb1c | |||
|
|
b4cd631703 | ||
|
|
a1e4bf7034 | ||
|
|
dba759114d | ||
|
|
89159e07d0 | ||
|
|
c1717edef8 | ||
|
|
04a0f83208 | ||
|
|
b569e2fc23 | ||
|
|
009bb10175 | ||
|
|
232f3c00d3 | ||
|
|
774f9ba91a | ||
|
|
846b8688a0 | ||
|
|
9a8e62ae8c | ||
|
|
858163939a | ||
|
|
a22f8965c8 | ||
|
|
2669a213fc | ||
|
|
d2722d316c | ||
|
|
81a039a049 | ||
|
|
c3cd0f5aa9 | ||
|
|
ff1bcaa7dc | ||
|
|
e3886c3d6f | ||
|
|
ea8a74e323 | ||
|
|
6233e706d2 | ||
| 442e0b1972 | |||
|
|
8544978351 | ||
| fe6cd670e8 | |||
|
|
3149c01bc1 | ||
|
|
16541e4a69 | ||
|
|
7295b4deaf | ||
|
|
1f06051e0a | ||
|
|
352cc7bcf5 | ||
|
|
b938e220d4 | ||
|
|
791d49aad9 | ||
|
|
47fefd70b9 | ||
|
|
785f129312 | ||
|
|
d368a4c09a | ||
|
|
1afcdadcfa |
@@ -25,7 +25,7 @@
|
|||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.@"raylib-zig" = .{
|
.@"raylib-zig" = .{
|
||||||
.url = "https://github.com/Not-Nik/raylib-zig/archive/devel.tar.gz",
|
.url = "https://github.com/Not-Nik/raylib-zig/archive/devel.tar.gz",
|
||||||
.hash = "12202f8c415153088be8df39a51e0a4c9d402afd403422a0dcc9afdd417e437a6fdb",
|
.hash = "122058d3ea6318efb819d0bffba630afd1a459fa3a99b4bfe4b680a937d5de04d2fc",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
|
|||||||
7
gnuplot.note
Normal file
7
gnuplot.note
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
gnuplot
|
||||||
|
set datafile separator ','
|
||||||
|
set key autotitle columnhead
|
||||||
|
plot 'plot.dat' using 0:1, 'plot.dat' using 0:2, 'plot.dat' using 0:3, 'plot.dat' using 0:4
|
||||||
|
pause 3
|
||||||
|
reread
|
||||||
|
|
||||||
6
live_plot
Normal file
6
live_plot
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
while (1) {
|
||||||
|
set datafile separator ','
|
||||||
|
set key autotitle columnhead
|
||||||
|
|
||||||
|
plot 'plot.dat' using 0:1, 'plot.dat' using 0:2, 'plot.dat' using 0:3, 'plot.dat' using 0:4
|
||||||
|
}
|
||||||
BIN
resources/blut.png
Normal file
BIN
resources/blut.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
BIN
resources/test.png
Normal file
BIN
resources/test.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 190 KiB |
BIN
resources/test_64.png
Normal file
BIN
resources/test_64.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.7 KiB |
274
src/main.zig
274
src/main.zig
@@ -2,210 +2,118 @@ const std = @import("std");
|
|||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
const rg = @import("raygui");
|
const rg = @import("raygui");
|
||||||
const pr = @import("primitives.zig");
|
const pr = @import("primitives.zig");
|
||||||
|
const particles = @import("particles.zig");
|
||||||
|
const plot = @import("plot.zig");
|
||||||
|
|
||||||
const allocator = std.heap.page_allocator;
|
const allocator = std.heap.page_allocator;
|
||||||
|
const DEBUG = true;
|
||||||
|
|
||||||
var prng = std.rand.DefaultPrng.init(124346556);
|
const screenWidth = 1200;
|
||||||
|
const screenHeight = 800;
|
||||||
|
|
||||||
|
fn ItoF(x: i32) f32 {
|
||||||
const rand = prng.random();
|
return @as(f32, @floatFromInt(x));
|
||||||
|
|
||||||
fn randMinMaxFloat(min:f32,max:f32) f32 {
|
|
||||||
var r1 = rand.float(f32);
|
|
||||||
r1 = 2*(r1 - 0.5);//-0.5..0.5
|
|
||||||
return (@abs(min)+@abs(max))/2 * r1;
|
|
||||||
}
|
}
|
||||||
|
fn rectangle(x: u16, y: u16, width: u16, height: u16) rl.Rectangle {
|
||||||
test "random" {
|
return rl.Rectangle{ .x = @as(f32, @floatFromInt(x)), .y = @as(f32, @floatFromInt(y)), .width = @as(f32, @floatFromInt(width)), .height = @as(f32, @floatFromInt(height)) };
|
||||||
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});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const screenWidth = 800;
|
const Settings = struct {
|
||||||
const screenHeight = 450;
|
particleCount: f32 = 1,
|
||||||
|
spread_x: f32 = 0,
|
||||||
const Particle = struct {
|
spread_y: f32 = 0,
|
||||||
const Self = @This();
|
color: rl.Color = .{ .r = 255, .g = 255, .b = 0, .a = 100 },
|
||||||
position : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
|
active: i32 = 0,
|
||||||
velocity : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
|
editMode: bool = false,
|
||||||
acceleration : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
|
|
||||||
lifespan : u8 = 0,
|
|
||||||
show : u8 = 0,
|
|
||||||
pub fn new(self:Self) void {
|
|
||||||
_ = self;
|
|
||||||
}
|
|
||||||
pub fn update(self:*Self) void {
|
|
||||||
self.velocity.add(self.acceleration);
|
|
||||||
self.position.add(self.velocity);
|
|
||||||
if (self.position.a.y > screenHeight)
|
|
||||||
{
|
|
||||||
self.velocity.a.y *= -0.9;
|
|
||||||
// self.acceleration.a.y *= -0.5;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self.lifespan > 0) {
|
|
||||||
self.lifespan -= 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
self.show = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn spawn(self:*Self) void
|
|
||||||
{
|
|
||||||
const xrnr = randMinMaxFloat(-1,1);
|
|
||||||
const yrnr = randMinMaxFloat(-0.5,0.5);
|
|
||||||
|
|
||||||
self.lifespan = rand.intRangeAtMost(u8,75,255);
|
|
||||||
self.acceleration.a.x = xrnr;
|
|
||||||
self.acceleration.a.y = yrnr;
|
|
||||||
self.position.a.x = @floatFromInt(rl.getMouseX()-screenWidth/2);
|
|
||||||
self.position.a.y = @floatFromInt(rl.getMouseY()-screenHeight/2);
|
|
||||||
self.velocity.a.x = 0;
|
|
||||||
self.velocity.a.y = -20;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Emitter = struct
|
|
||||||
{
|
|
||||||
|
|
||||||
particleCount : u32 = 0,
|
|
||||||
particles : []Particle = undefined,
|
|
||||||
|
|
||||||
pub fn init(self:*Emitter) !void
|
|
||||||
{
|
|
||||||
var xrnr = randMinMaxFloat(-1,1);
|
|
||||||
var yrnr = randMinMaxFloat(-0.5,0.5);
|
|
||||||
self.particles = try allocator.alloc(Particle,500000);
|
|
||||||
for (self.particles) |*p| {
|
|
||||||
xrnr = randMinMaxFloat(0,2);
|
|
||||||
yrnr = randMinMaxFloat(0,2);
|
|
||||||
p.* = Particle{.velocity = .{.a = .{.x = 1,.y = -3}},.acceleration = .{.a = .{.x=xrnr,.y=yrnr}}};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn emit(self:*Emitter,count : u32) void
|
|
||||||
{
|
|
||||||
var i : u32 = 0;
|
|
||||||
for(self.particles) |*p|
|
|
||||||
{
|
|
||||||
if(p.show == 0)
|
|
||||||
{
|
|
||||||
i+=1;
|
|
||||||
p.spawn();
|
|
||||||
}
|
|
||||||
if(i >= count)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn update(self:*Emitter) void
|
|
||||||
{
|
|
||||||
var posx : i32 = 0;
|
|
||||||
var posy : i32 = 0;
|
|
||||||
|
|
||||||
|
|
||||||
for (self.particles) |*p| {
|
|
||||||
if(p.show == 1)
|
|
||||||
{
|
|
||||||
p.update();
|
|
||||||
p.applyGravity(2);
|
|
||||||
if(p.position.a.x >= -(screenWidth/2)){
|
|
||||||
posx = @intFromFloat(screenWidth / 2 + p.position.a.x);
|
|
||||||
} else {
|
|
||||||
posx = 0;
|
|
||||||
}
|
|
||||||
if(p.position.a.y >= -(screenHeight/2)){
|
|
||||||
posy = @intFromFloat(screenHeight / 2 + p.position.a.y);
|
|
||||||
} else {
|
|
||||||
posy = 0;
|
|
||||||
}
|
|
||||||
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});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
fn ItoF(x:i32) f32
|
|
||||||
{
|
|
||||||
return @as(f32,@floatFromInt(x));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
// Initialization
|
if(DEBUG) {
|
||||||
//--------------------------------------------------------------------------------------
|
std.debug.print("DEBUG mode enabled",.{});
|
||||||
|
}
|
||||||
|
|
||||||
const rect2 = rl.Rectangle{ .x = @as(f32, @floatFromInt(10)), .y = @as(f32, @floatFromInt(10)), .width = @as(f32, @floatFromInt(300)), .height = @as(f32, @floatFromInt(100)) };
|
var settings = Settings{};
|
||||||
const rect3 = rl.Rectangle{ .x = @as(f32, @floatFromInt(10)), .y = @as(f32, @floatFromInt(150)), .width = @as(f32, @floatFromInt(600)), .height = @as(f32, @floatFromInt(10)) };
|
var emitter = particles.Emitter{};
|
||||||
var msg_res : i32=-1;
|
try emitter.init(allocator, screenWidth, screenHeight);
|
||||||
var state : i32=-1;
|
|
||||||
|
|
||||||
var seed:u64 = undefined;
|
|
||||||
try std.posix.getrandom(std.mem.asBytes(&seed));
|
|
||||||
|
|
||||||
prng = std.rand.DefaultPrng.init(seed);
|
|
||||||
|
|
||||||
var value : f32 = 0;
|
|
||||||
|
|
||||||
var emitter = Emitter{};
|
|
||||||
try emitter.init();
|
|
||||||
|
|
||||||
//const img : rl.Texture = rl.loadTexture("img.png");
|
//const img : rl.Texture = rl.loadTexture("img.png");
|
||||||
|
|
||||||
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
|
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
|
||||||
defer rl.closeWindow(); // Close window and OpenGL context
|
defer rl.closeWindow(); // Close window and OpenGL context
|
||||||
|
|
||||||
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
|
var texture: rl.Texture2D = undefined;
|
||||||
|
const image: rl.Image = rl.loadImage("resources/blut.png");
|
||||||
|
if (image.width > 0) {
|
||||||
|
texture = rl.loadTextureFromImage(image);
|
||||||
|
} else {
|
||||||
|
std.debug.print("error opening resource", .{});
|
||||||
|
std.posix.exit(1);
|
||||||
|
}
|
||||||
|
rl.unloadImage(image);
|
||||||
|
rl.setTargetFPS(60);
|
||||||
|
|
||||||
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
|
const file = try plot.open("plot.dat");
|
||||||
|
|
||||||
|
try plot.log(file, "acceleration, position, velocity, show\n");
|
||||||
|
|
||||||
|
var att: [10]particles.Attractor = undefined;
|
||||||
|
|
||||||
|
while (!rl.windowShouldClose()) {
|
||||||
rl.beginDrawing();
|
rl.beginDrawing();
|
||||||
defer rl.endDrawing();
|
defer rl.endDrawing();
|
||||||
|
|
||||||
rl.clearBackground(rl.Color{.r=181,
|
updateGui(&settings);
|
||||||
.g =177,
|
|
||||||
.b =154,
|
|
||||||
.a = 255,
|
|
||||||
});
|
|
||||||
|
|
||||||
if(state != 0){
|
if (rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) {
|
||||||
msg_res = rg.guiMessageBox(rect2, "Title", "question?", "Nice;Cool");
|
emitter.emit(
|
||||||
if(msg_res > -1){
|
@as(u32, @intFromFloat(settings.particleCount)),
|
||||||
state = msg_res;
|
settings.spread_x,
|
||||||
}
|
settings.spread_y
|
||||||
rl.clearBackground(rl.Color.white);
|
);
|
||||||
switch(state){
|
|
||||||
1 => {
|
|
||||||
rl.drawText("1", 190, 200, 20, rl.Color.red);
|
|
||||||
},
|
|
||||||
2 => {
|
|
||||||
rl.drawText("2", 190, 200, 20, rl.Color.red);
|
|
||||||
},
|
|
||||||
else =>{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ = rg.guiSlider(rect3,"0","5000",&value,ItoF(0),ItoF(5000));
|
|
||||||
|
|
||||||
if(rl.isMouseButtonDown(rl.MouseButton.mouse_button_left))
|
|
||||||
{
|
|
||||||
emitter.emit(@as(u32,@intFromFloat(value)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
emitter.update();
|
rl.beginBlendMode(rl.BlendMode.blend_additive);
|
||||||
|
emitter.update(&att, texture, settings.color);
|
||||||
|
rl.endBlendMode();
|
||||||
|
|
||||||
|
try log_emitter(file,0,emitter);
|
||||||
|
|
||||||
|
}
|
||||||
|
plot.close(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn log_emitter(file : std.fs.File, index : u32, emitter : particles.Emitter) !void
|
||||||
|
{
|
||||||
|
var plotStr: []u8 = undefined;
|
||||||
|
var plotStrBuf: [128]u8 = undefined;
|
||||||
|
plotStr = try std.fmt.bufPrint(&plotStrBuf, "{d}, {d}, {d}, {d}\n", .{
|
||||||
|
emitter.particles[index].acceleration.a.x,
|
||||||
|
emitter.particles[index].position.a.x,
|
||||||
|
emitter.particles[index].velocity.a.x,
|
||||||
|
emitter.particles[index].show });
|
||||||
|
try plot.log(file, plotStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn updateGui(set: *Settings) void {
|
||||||
|
rl.clearBackground(rl.Color{
|
||||||
|
.r = 0,
|
||||||
|
.g = 0,
|
||||||
|
.b = 0,
|
||||||
|
.a = 255,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (rg.guiDropdownBox(rectangle(10, 10, 100, 20), "Settings;Particles", &set.active, set.editMode) != 0) {
|
||||||
|
set.editMode = !set.editMode;
|
||||||
|
std.debug.print("dropdownbox: {}", .{set.active});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (set.active == 1) {
|
||||||
|
if (rg.guiWindowBox(rectangle(10, 10, 800, 600), "Window Box") == 1) {
|
||||||
|
set.active = 0;
|
||||||
|
set.editMode = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = rg.guiSlider(rectangle(10, 150, 600, 10), "0", "500", &set.particleCount, 0, 500);
|
||||||
|
_ = rg.guiSlider(rectangle(10, 170, 600, 10), "-3", "3", &set.spread_x, -100, 100);
|
||||||
|
_ = rg.guiSlider(rectangle(10, 190, 600, 10), "-3", "3", &set.spread_y, -100, 100);
|
||||||
|
_ = rg.guiColorPicker(rectangle(10, 200, 300, 100), "Choose Color", &set.color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
202
src/particles.zig
Normal file
202
src/particles.zig
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
19
src/plot.zig
Normal file
19
src/plot.zig
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
pub fn init() void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
pub fn log(self : std.fs.File,data : []const u8) !void
|
||||||
|
{
|
||||||
|
try self.writeAll(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open(file : []const u8) !std.fs.File
|
||||||
|
{
|
||||||
|
const fileh = try std.fs.cwd().createFile(file,.{.read = true});
|
||||||
|
return fileh;
|
||||||
|
}
|
||||||
|
pub fn close(file : std.fs.File) void
|
||||||
|
{
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
@@ -24,6 +24,18 @@ pub const Vec = struct {
|
|||||||
self.a.y = self.a.y + m1.a.y;
|
self.a.y = self.a.y + m1.a.y;
|
||||||
self.a.z = self.a.z + m1.a.z;
|
self.a.z = self.a.z + m1.a.z;
|
||||||
}
|
}
|
||||||
|
pub fn self_sub(self:*Self,m1:Self) void {
|
||||||
|
self.a.x = self.a.x - m1.a.x;
|
||||||
|
self.a.y = self.a.y - m1.a.y;
|
||||||
|
self.a.z = self.a.z - m1.a.z;
|
||||||
|
}
|
||||||
|
pub fn sub(self:Self,m1:Self) Vec {
|
||||||
|
var vec : Vec = undefined;
|
||||||
|
vec.a.x = self.a.x - m1.a.x;
|
||||||
|
vec.a.y = self.a.y - m1.a.y;
|
||||||
|
vec.a.z = self.a.z - m1.a.z;
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
pub const Vec2 = struct {
|
pub const Vec2 = struct {
|
||||||
a: Point = .{ .x = 0, .y = 0, .z = 0},
|
a: Point = .{ .x = 0, .y = 0, .z = 0},
|
||||||
|
|||||||
16
src/utils.zig
Normal file
16
src/utils.zig
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
var prng = std.rand.DefaultPrng.init(124346234556);
|
||||||
|
const rand = prng.random();
|
||||||
|
|
||||||
|
pub fn init() void {
|
||||||
|
var seed: u64 = undefined;
|
||||||
|
try std.posix.getrandom(std.mem.asBytes(&seed));
|
||||||
|
prng = std.rand.DefaultPrng.init(seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn randMinMaxFloat(min: f32, max: f32) f32 {
|
||||||
|
var r1 = rand.float(f32);
|
||||||
|
r1 = 2 * (r1 - 0.5); //-0.5..0.5
|
||||||
|
return (@abs(min) + @abs(max)) / 2 * r1;
|
||||||
|
}
|
||||||
Binary file not shown.
BIN
zig-out/bin/raylib-test.exe
Normal file
BIN
zig-out/bin/raylib-test.exe
Normal file
Binary file not shown.
BIN
zig-out/bin/test.zip
Normal file
BIN
zig-out/bin/test.zip
Normal file
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user