fixed delta infinite issue

master
Red 10 months ago
parent 1f803030f5
commit 7209dd81a2

@ -18,7 +18,7 @@ pub fn main() anyerror!void {
try SetupECS(&reg); // run Entity Registry setup try SetupECS(&reg); // run Entity Registry setup
rl.setTargetFPS(60); // set max fps rl.setTargetFPS(60); // set max fps
var delta: f32 = 0; var delta: f32 = 1;
while (!rl.windowShouldClose()) { while (!rl.windowShouldClose()) {
rl.beginDrawing(); rl.beginDrawing();
defer rl.endDrawing(); defer rl.endDrawing();
@ -31,6 +31,9 @@ pub fn main() anyerror!void {
const text = try std.fmt.allocPrintZ(Aloc.allocator(), "{d}", .{delta}); const text = try std.fmt.allocPrintZ(Aloc.allocator(), "{d}", .{delta});
rl.drawText(text, 500, 10, 11, rl.Color.white); rl.drawText(text, 500, 10, 11, rl.Color.white);
delta = 1.0 / @as(f32, @floatFromInt(rl.getFPS())); delta = 1.0 / @as(f32, @floatFromInt(rl.getFPS()));
if(delta > 10000 or delta < -10000){
delta = 0;
}
} }
} }
pub fn SetupECS(reg: *ecs.Registry) !void { 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); 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 view = reg.view(.{EnSysTypes.Transform, EnSysTypes.Rigidbody, EnSysTypes.Gravity}, .{});
var iter = view.entityIterator(); var iter = view.entityIterator();
while(iter.next())|entity|{ while(iter.next())|entity|{
var Rigidbody = view.get(EnSysTypes.Rigidbody, entity); const Rigidbody = view.get(EnSysTypes.Rigidbody, entity);
var Transform = view.get(EnSysTypes.Transform, entity); const Transform = view.get(EnSysTypes.Transform, entity);
const Gravity = view.getConst(EnSysTypes.Gravity, entity); const Gravity = view.getConst(EnSysTypes.Gravity, entity);
Rigidbody.vel.x += Gravity.vel.x; Rigidbody.*.vel.x += Gravity.vel.x;
Rigidbody.vel.y += Gravity.vel.y; Rigidbody.*.vel.y += Gravity.vel.y;
Transform.pos.x += Rigidbody.vel.x / deltaTime; Transform.pos.x += Rigidbody.vel.x * delta;
Transform.pos.y += Rigidbody.vel.y / deltaTime; Transform.pos.y += Rigidbody.vel.y * delta;
} }
} }

Loading…
Cancel
Save