This commit is contained in:
Jonathan Wyss 2024-11-27 14:51:03 +01:00
commit 442e0b1972
2 changed files with 14 additions and 10 deletions

View File

@ -78,9 +78,11 @@ const Particle = struct {
self.velocity.a.y += val;
}
pub fn applyForce(self: *Self, vec: pr.Vec) void {
self.acceleration.a.x += vec.a.x * 0.5;
self.acceleration.a.y += vec.a.y * 0.5;
self.acceleration.a.z += vec.a.z;
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.999;
self.velocity.a.y *= 0.999;
}
};
@ -114,13 +116,13 @@ const Emitter = struct {
var posx: i32 = 0;
var posy: i32 = 0;
const att: Attractor = .{ .vec = .{ .a = .{ .x = 10, .y = 10, .z = 10 } } };
const att2: Attractor = .{ .vec = .{ .a = .{ .x = -100, .y = -100, .z = 0 } } };
const att2: Attractor = .{ .vec = .{ .a = .{ .x = -200, .y = -200, .z = 0 } } };
att.draw();
att2.draw();
for (self.particles) |*p| {
if (p.show == 1) {
p.applyGravity(2);
p.applyGravity(0);
p.applyForce(att.attract(p.*));
p.applyForce(att2.attract(p.*));
p.update();
@ -136,12 +138,14 @@ const Emitter = struct {
const Attractor = struct {
vec: pr.Vec,
pub fn attract(self: Attractor, particle: Particle) pr.Vec {
var vec = self.vec.sub(particle.position);
if (vec.a.x != 0 and vec.a.y != 0) {
vec.a.x = vec.a.x / 200;
vec.a.y = vec.a.y / 200;
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.z = 0;
}
return vec;
return force;
}
pub fn draw(self: Attractor) void {
const posx: i32 = @intFromFloat(screenWidth / 2 + self.vec.a.x);

Binary file not shown.