attractor
This commit is contained in:
parent
b938e220d4
commit
352cc7bcf5
27
src/main.zig
27
src/main.zig
@ -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));
|
||||
|
@ -24,6 +24,18 @@ pub const Vec = struct {
|
||||
self.a.y = self.a.y + m1.a.y;
|
||||
self.a.z = self.a.z + m1.a.z;
|
||||
}
|
||||
pub fn self_sub(self:*Self,m1:Self) void {
|
||||
self.a.x = self.a.x - m1.a.x;
|
||||
self.a.y = self.a.y - m1.a.y;
|
||||
self.a.z = self.a.z - m1.a.z;
|
||||
}
|
||||
pub fn sub(self:Self,m1:Self) Vec {
|
||||
var vec : Vec = undefined;
|
||||
vec.a.x = self.a.x - m1.a.x;
|
||||
vec.a.y = self.a.y - m1.a.y;
|
||||
vec.a.z = self.a.z - m1.a.z;
|
||||
return vec;
|
||||
}
|
||||
};
|
||||
pub const Vec2 = struct {
|
||||
a: Point = .{ .x = 0, .y = 0, .z = 0},
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user