From 7209dd81a2ad8436885dc491bd188af7b3a8e00c Mon Sep 17 00:00:00 2001 From: Red Date: Tue, 27 Feb 2024 14:08:32 +0100 Subject: [PATCH] fixed delta infinite issue --- src/main.zig | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.zig b/src/main.zig index cba5f09..fd8ed84 100644 --- a/src/main.zig +++ b/src/main.zig @@ -18,7 +18,7 @@ pub fn main() anyerror!void { try SetupECS(®); // run Entity Registry setup rl.setTargetFPS(60); // set max fps - var delta: f32 = 0; + var delta: f32 = 1; while (!rl.windowShouldClose()) { rl.beginDrawing(); defer rl.endDrawing(); @@ -31,6 +31,9 @@ pub fn main() anyerror!void { const text = try std.fmt.allocPrintZ(Aloc.allocator(), "{d}", .{delta}); rl.drawText(text, 500, 10, 11, rl.Color.white); delta = 1.0 / @as(f32, @floatFromInt(rl.getFPS())); + if(delta > 10000 or delta < -10000){ + delta = 0; + } } } pub fn SetupECS(reg: *ecs.Registry) !void { @@ -49,18 +52,18 @@ pub fn DrawSystem(reg: *ecs.Registry) !void { rl.drawTextureEx(Sprite.image, Transform.pos, Transform.rot, 1, rl.Color.white); } } -pub fn PhysicsSystem(reg: *ecs.Registry, deltaTime: f32) !void { +pub fn PhysicsSystem(reg: *ecs.Registry, delta: f32) !void { var view = reg.view(.{EnSysTypes.Transform, EnSysTypes.Rigidbody, EnSysTypes.Gravity}, .{}); var iter = view.entityIterator(); while(iter.next())|entity|{ - var Rigidbody = view.get(EnSysTypes.Rigidbody, entity); - var Transform = view.get(EnSysTypes.Transform, entity); + const Rigidbody = view.get(EnSysTypes.Rigidbody, entity); + const Transform = view.get(EnSysTypes.Transform, entity); const Gravity = view.getConst(EnSysTypes.Gravity, entity); - Rigidbody.vel.x += Gravity.vel.x; - Rigidbody.vel.y += Gravity.vel.y; - Transform.pos.x += Rigidbody.vel.x / deltaTime; - Transform.pos.y += Rigidbody.vel.y / deltaTime; + Rigidbody.*.vel.x += Gravity.vel.x; + Rigidbody.*.vel.y += Gravity.vel.y; + Transform.pos.x += Rigidbody.vel.x * delta; + Transform.pos.y += Rigidbody.vel.y * delta; } }