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);
self.particles = try allocator.alloc(Particle, 10000);
for (self.particles) |*p| {
xrnr = randMinMaxFloat(0, 2);
yrnr = randMinMaxFloat(0, 2);
xrnr = randMinMaxFloat(-2, 2);
yrnr = randMinMaxFloat(-2, 2);
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)
{
at.init(.{.a=.{.x=@floatFromInt(rl.getMouseX() - screenWidth / 2),.y=@floatFromInt(rl.getMouseY() - screenHeight / 2),.z=0}});
std.debug.print("attractor allocated\n",.{});
break;
}
@ -128,9 +127,10 @@ const Emitter = struct {
}
for (self.particles) |*p| {
if (p.show == 1) {
p.applyGravity(0);
//p.applyGravity(2);
for (att) |at|
{
//std.debug.print("force:{}\n",.{at.attract(p.*)});
p.applyForce(at.attract(p.*));
}
p.update();
@ -153,20 +153,30 @@ const Attractor = struct {
self.init_done = 1;
}
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 / 200;
force.a.y = distance.a.y / 200;
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}};
}
}
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);
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 {
return @as(f32, @floatFromInt(x));
@ -233,8 +243,6 @@ pub fn main() anyerror!void {
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});
try plot.log(file,plotStr);

Binary file not shown.