add procedural attractors
This commit is contained in:
parent
9a8e62ae8c
commit
846b8688a0
42
src/main.zig
42
src/main.zig
@ -100,20 +100,39 @@ const Emitter = struct {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn update(self: *Emitter, texture: rl.Texture2D) void {
|
||||
pub fn update(self: *Emitter,att : []Attractor, texture: rl.Texture2D) void {
|
||||
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 = -200, .y = -200, .z = 0 } } };
|
||||
|
||||
var vec2 : rl.Vector2=undefined;
|
||||
|
||||
att.draw();
|
||||
att2.draw();
|
||||
if (rl.isMouseButtonPressed(rl.MouseButton.mouse_button_right)) {
|
||||
std.debug.print("right pressed\n",.{});
|
||||
|
||||
for (att) |*at|
|
||||
{
|
||||
std.debug.print("loop\n",.{});
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (att) |at|
|
||||
{
|
||||
at.draw();
|
||||
}
|
||||
for (self.particles) |*p| {
|
||||
if (p.show == 1) {
|
||||
p.applyGravity(0);
|
||||
p.applyForce(att.attract(p.*));
|
||||
p.applyForce(att2.attract(p.*));
|
||||
for (att) |at|
|
||||
{
|
||||
p.applyForce(at.attract(p.*));
|
||||
}
|
||||
p.update();
|
||||
|
||||
posx = @intFromFloat(screenWidth / 2 + p.position.a.x);
|
||||
@ -127,6 +146,12 @@ const Emitter = struct {
|
||||
};
|
||||
const Attractor = struct {
|
||||
vec: pr.Vec,
|
||||
init_done : u8 = 0,
|
||||
pub fn init(self : *Attractor, vec : pr.Vec) void
|
||||
{
|
||||
self.vec = vec;
|
||||
self.init_done = 1;
|
||||
}
|
||||
pub fn attract(self: Attractor, particle: Particle) pr.Vec {
|
||||
const distance = self.vec.sub(particle.position);
|
||||
var force: pr.Vec = undefined;
|
||||
@ -187,6 +212,7 @@ pub fn main() anyerror!void {
|
||||
var plotStr : []u8 = undefined;
|
||||
var plotStrBuf : [128]u8 = undefined;
|
||||
try plot.log(file,"acceleration, position, velocity, show\n");
|
||||
var att: [10]Attractor = undefined;
|
||||
|
||||
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
|
||||
rl.beginDrawing();
|
||||
@ -213,7 +239,7 @@ pub fn main() anyerror!void {
|
||||
try plot.log(file,plotStr);
|
||||
|
||||
rl.beginBlendMode(rl.BlendMode.blend_additive);
|
||||
emitter.update(texture);
|
||||
emitter.update(&att,texture);
|
||||
rl.endBlendMode();
|
||||
}
|
||||
plot.close(file);
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user