fix not init bug with attractors

This commit is contained in:
jonathan santis 2024-11-28 15:04:42 +01:00
parent 846b8688a0
commit 774f9ba91a
2 changed files with 22 additions and 14 deletions

View File

@ -83,8 +83,8 @@ const Emitter = struct {
var yrnr = randMinMaxFloat(-0.5, 0.5); var yrnr = randMinMaxFloat(-0.5, 0.5);
self.particles = try allocator.alloc(Particle, 10000); self.particles = try allocator.alloc(Particle, 10000);
for (self.particles) |*p| { for (self.particles) |*p| {
xrnr = randMinMaxFloat(0, 2); xrnr = randMinMaxFloat(-2, 2);
yrnr = randMinMaxFloat(0, 2); yrnr = randMinMaxFloat(-2, 2);
p.* = Particle{ .velocity = .{ .a = .{ .x = 0, .y = 0 } }, .acceleration = .{ .a = .{ .x = xrnr, .y = yrnr } } }; p.* = Particle{ .velocity = .{ .a = .{ .x = 0, .y = 0 } }, .acceleration = .{ .a = .{ .x = xrnr, .y = yrnr } } };
} }
} }
@ -115,7 +115,6 @@ const Emitter = struct {
if(at.init_done != 1) if(at.init_done != 1)
{ {
at.init(.{.a=.{.x=@floatFromInt(rl.getMouseX() - screenWidth / 2),.y=@floatFromInt(rl.getMouseY() - screenHeight / 2),.z=0}}); at.init(.{.a=.{.x=@floatFromInt(rl.getMouseX() - screenWidth / 2),.y=@floatFromInt(rl.getMouseY() - screenHeight / 2),.z=0}});
std.debug.print("attractor allocated\n",.{}); std.debug.print("attractor allocated\n",.{});
break; break;
} }
@ -128,9 +127,10 @@ const Emitter = struct {
} }
for (self.particles) |*p| { for (self.particles) |*p| {
if (p.show == 1) { if (p.show == 1) {
p.applyGravity(0); //p.applyGravity(2);
for (att) |at| for (att) |at|
{ {
//std.debug.print("force:{}\n",.{at.attract(p.*)});
p.applyForce(at.attract(p.*)); p.applyForce(at.attract(p.*));
} }
p.update(); p.update();
@ -153,19 +153,29 @@ const Attractor = struct {
self.init_done = 1; 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); if(self.init_done == 1)
var force: pr.Vec = undefined; {
if (distance.a.x != 0 and distance.a.y != 0) { const distance = self.vec.sub(particle.position);
force.a.x = distance.a.x / 200; var force: pr.Vec = undefined;
force.a.y = distance.a.y / 200; if (distance.a.x != 0 and distance.a.y != 0) {
force.a.z = 0; force.a.x = distance.a.x / 100;
force.a.y = distance.a.y / 100;
force.a.z = 0;
}
return force;
}
else
{
return .{.a=.{.x=0,.y=0,.z=0}};
} }
return force;
} }
pub fn draw(self: Attractor) void { pub fn draw(self: Attractor) void {
const posx: i32 = @intFromFloat(screenWidth / 2 + self.vec.a.x); const posx: i32 = @intFromFloat(screenWidth / 2 + self.vec.a.x);
const posy: i32 = @intFromFloat(screenHeight / 2 + self.vec.a.y); 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 }); if (self.init_done == 1)
{
rl.drawRectangle(posx, posy, 10, 10, rl.Color{ .r = 10, .g = 10, .b = 120, .a = 255 });
}
} }
}; };
fn ItoF(x: i32) f32 { fn ItoF(x: i32) f32 {
@ -233,8 +243,6 @@ pub fn main() anyerror!void {
emitter.emit(@as(u32, @intFromFloat(value)), value2, value3); emitter.emit(@as(u32, @intFromFloat(value)), value2, value3);
} }
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}); 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});
try plot.log(file,plotStr); try plot.log(file,plotStr);

Binary file not shown.