attractor

This commit is contained in:
jonathan santis
2024-11-27 11:08:40 +01:00
parent b938e220d4
commit 352cc7bcf5
3 changed files with 38 additions and 1 deletions

View File

@@ -79,6 +79,11 @@ const Particle = struct {
pub fn applyGravity(self : *Self,val : f32) void {
self.velocity.a.y += val;
}
pub fn applyForce(self : *Self,vec : pr.Vec) void {
self.velocity.a.x += vec.a.x;
self.velocity.a.y += vec.a.y;
self.velocity.a.z += vec.a.z;
}
};
const Emitter = struct
@@ -118,12 +123,16 @@ const Emitter = struct
{
var posx : i32 = 0;
var posy : i32 = 0;
const att : Attractor = .{.vec = .{.a=.{.x=200,.y=200,.z=0}}};
att.draw();
for (self.particles) |*p| {
if(p.show == 1)
{
p.update();
p.applyGravity(2);
p.applyGravity(0);
p.applyForce(att.attract(p.*));
posx = @intFromFloat(screenWidth / 2 + p.position.a.x);
posy = @intFromFloat(screenHeight / 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});
@@ -133,6 +142,22 @@ 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);
vec.a.x /= 100;
vec.a.y /= 100;
vec.a.z /= 100;
return vec;
}
pub fn draw(self:Attractor) void
{
rl.drawRectangle(@intFromFloat(self.vec.a.x),@intFromFloat(self.vec.a.y),10,10,rl.Color{.r=10,.g=10,.b=120,.a=255});
}
};
fn ItoF(x:i32) f32
{
return @as(f32,@floatFromInt(x));