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
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;
}
}

Loading…
Cancel
Save