add procedural attractors

This commit is contained in:
jonathan santis 2024-11-28 14:30:31 +01:00
parent 9a8e62ae8c
commit 846b8688a0
2 changed files with 34 additions and 8 deletions

View File

@ -100,20 +100,39 @@ const Emitter = struct {
} }
} }
} }
pub fn update(self: *Emitter, texture: rl.Texture2D) void { pub fn update(self: *Emitter,att : []Attractor, texture: rl.Texture2D) void {
var posx: i32 = 0; var posx: i32 = 0;
var posy: i32 = 0; var posy: i32 = 0;
const att: Attractor = .{ .vec = .{ .a = .{ .x = 10, .y = 10, .z = 10 } } };
const att2: Attractor = .{ .vec = .{ .a = .{ .x = -200, .y = -200, .z = 0 } } };
var vec2 : rl.Vector2=undefined; var vec2 : rl.Vector2=undefined;
att.draw(); if (rl.isMouseButtonPressed(rl.MouseButton.mouse_button_right)) {
att2.draw(); std.debug.print("right pressed\n",.{});
for (att) |*at|
{
std.debug.print("loop\n",.{});
if(at.init_done != 1)
{
at.init(.{.a=.{.x=@floatFromInt(rl.getMouseX() - screenWidth / 2),.y=@floatFromInt(rl.getMouseY() - screenHeight / 2),.z=0}});
std.debug.print("attractor allocated\n",.{});
break;
}
}
}
for (att) |at|
{
at.draw();
}
for (self.particles) |*p| { for (self.particles) |*p| {
if (p.show == 1) { if (p.show == 1) {
p.applyGravity(0); p.applyGravity(0);
p.applyForce(att.attract(p.*)); for (att) |at|
p.applyForce(att2.attract(p.*)); {
p.applyForce(at.attract(p.*));
}
p.update(); p.update();
posx = @intFromFloat(screenWidth / 2 + p.position.a.x); posx = @intFromFloat(screenWidth / 2 + p.position.a.x);
@ -127,6 +146,12 @@ const Emitter = struct {
}; };
const Attractor = struct { const Attractor = struct {
vec: pr.Vec, vec: pr.Vec,
init_done : u8 = 0,
pub fn init(self : *Attractor, vec : pr.Vec) void
{
self.vec = vec;
self.init_done = 1;
}
pub fn attract(self: Attractor, particle: Particle) pr.Vec { pub fn attract(self: Attractor, particle: Particle) pr.Vec {
const distance = self.vec.sub(particle.position); const distance = self.vec.sub(particle.position);
var force: pr.Vec = undefined; var force: pr.Vec = undefined;
@ -187,6 +212,7 @@ pub fn main() anyerror!void {
var plotStr : []u8 = undefined; var plotStr : []u8 = undefined;
var plotStrBuf : [128]u8 = undefined; var plotStrBuf : [128]u8 = undefined;
try plot.log(file,"acceleration, position, velocity, show\n"); try plot.log(file,"acceleration, position, velocity, show\n");
var att: [10]Attractor = undefined;
while (!rl.windowShouldClose()) { // Detect window close button or ESC key while (!rl.windowShouldClose()) { // Detect window close button or ESC key
rl.beginDrawing(); rl.beginDrawing();
@ -213,7 +239,7 @@ pub fn main() anyerror!void {
try plot.log(file,plotStr); try plot.log(file,plotStr);
rl.beginBlendMode(rl.BlendMode.blend_additive); rl.beginBlendMode(rl.BlendMode.blend_additive);
emitter.update(texture); emitter.update(&att,texture);
rl.endBlendMode(); rl.endBlendMode();
} }
plot.close(file); plot.close(file);

Binary file not shown.