From e603997055259039cefcdceaece5604e3856e36d Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 15 Apr 2023 01:56:14 -0500 Subject: fix circular reference bug for physics objects --- main.lua | 130 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 63 insertions(+), 67 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index 30581fc..fe97123 100644 --- a/main.lua +++ b/main.lua @@ -35,25 +35,8 @@ end) local entities = ecs.EntityDb() local systems = ecs.SystemDb(entities) -local camera = entities:createEntity() -entities:addComponents(camera, { - camera={ - projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), - }, - transform={ - matrix=Mat4():identity():translate(Vec3{0, 0, -6}), - }, - z = {value=-60}, - script={ - script="cameraRotationScript", - }, - onKey = { - script="cameraKeyHandler", - }, -}) - systems:addSystem(sys.transform) -systems:addSystem(sys.renderCamera, {camera=camera}) +systems:addSystem(sys.renderCamera) systems:addSystem(sys.script) systems:addSystem(sys.physics, {space=space, world=world}) package.loaded['baseRotationScript'] = function(entities, id, dt) @@ -76,39 +59,61 @@ package.loaded['cameraKeyHandler'] = function(entities, id, data) end end - -local id = entities:createEntity() -entities:addComponents(id, { - renderMesh = { - textures = { ourTexture={ filename="77155.png" } }, - shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/icosahedron.obj", index=1 }, - }, - transform = { - matrix = Mat4():identity():rotateZ(math.rad(45)), - }, - physics = { - mass = { - class = "sphere", - density = 1, - radius = 1, +function setupEntities() + local camera = entities:createEntity() + entities:addComponents(camera, { + camera={ + projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), }, - velocity = Vec3{ 0, 10, 0 }, - angularVelocity = Vec3{ 0, 1, 0 }, - }, -}) - -local id2 = entities:createEntity() -entities:addComponents(id2, { - renderMesh = { - shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/tetrahedron.obj", index=1 }, - }, - transform = { - parent=id, - matrix=Mat4():identity():translate(Vec3{0, 2, 0}), - }, -}) + transform={ + matrix=Mat4():identity():translate(Vec3{0, 0, -6}), + }, + z = {value=-60}, + script={ + script="cameraRotationScript", + }, + onKey = { + script="cameraKeyHandler", + }, + onWindowResize = { script = "cameraHandleResize" }, + }) + + + + local id = entities:createEntity() + entities:addComponents(id, { + renderMesh = { + textures = { ourTexture={ filename="77155.png" } }, + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + mesh = { filename="assets/icosahedron.obj", index=1 }, + }, + transform = { + matrix = Mat4():identity():rotateZ(math.rad(45)), + }, + physics = { + mass = { + class = "sphere", + density = 1, + radius = 1, + }, + velocity = Vec3{ 0, 10, 0 }, + angularVelocity = Vec3{ 0, 1, 0 }, + }, + }) + + local id2 = entities:createEntity() + entities:addComponents(id2, { + renderMesh = { + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + mesh = { filename="assets/tetrahedron.obj", index=1 }, + }, + transform = { + parent=id, + matrix=Mat4():identity():translate(Vec3{0, 2, 0}), + }, + }) +end +setupEntities() @@ -121,6 +126,7 @@ window:setKeyCallback(function(_, key, scancode, action) elseif key == glfw.KEY_SPACE then entities:save("save") elseif key == glfw.KEY_L then + entities:load("save") end end end) @@ -136,29 +142,19 @@ package.loaded["cameraHandleResize"] = function(entities, id, data) local camera = entities:getComponent(id, "camera") camera.projection:perspectiveResize(data.width/data.height) end -entities:addComponent(camera, "onWindowResize", { script = "cameraHandleResize" }) - --- averager (for fps) -function averager(memory) - local buf = {} - local avg = 0 - for i=1,memory do table.insert(buf, 0) end - return function(value) - table.insert(buf, value) - local val = table.remove(buf, 1) - avg = avg + value - val - return avg / memory - end -end -local fpsAverage = averager(200) - -entities:load("save") +--entities:load("save") -- main loop +local time = 0 honey.loop(window, function(dt) + time = time + dt + if time > 1 then + time = time-1 + print(collectgarbage("count")) + end gl.ClearColor(0.2, 0.4, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT + gl.STENCIL_BUFFER_BIT) -- cgit v1.2.1