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 --- honey/ecs-systems.lua | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'honey/ecs-systems.lua') diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index 54bac89..47c0f6d 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -157,9 +157,11 @@ end --===== physics =====-- + physics = function(params) local interval = params.interval or 0.016 local groupSize = params.groupSize or 20 + local refs = {} return { db=params.db, space=params.space, @@ -169,14 +171,21 @@ physics = function(params) priority=0, update=function(self, dt) + for i, ref in ipairs(refs) do + print(i, ref.tbl, ref.physics) + end local query = self.db:queryComponent("physics") + for id, physics in pairs(query) do if not physics._body then - physics._body = ode.BodyCreate(self.world) + print("add physics body for "..id) + local body = ode.BodyCreate(self.world) physics._gc = honey.util.gc_canary(function() print("releasing physics body for " .. id) - ode.BodyDestroy(physics._body) + ode.BodyDestroy(body) + body = nil end) + local mass = ode.MassCreate() local class = physics.mass.class if not class then @@ -188,30 +197,31 @@ physics = function(params) physics.mass.radius ) end - ode.BodySetMass(physics._body, mass) + ode.BodySetMass(body, mass) local m = self.db:getComponent(id, "transform").matrix ode.BodySetPosition( - physics._body, + body, m[1][4], m[2][4], m[3][4] ) ode.BodySetRotation( - physics._body, + body, m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3] ) ode.BodySetLinearVel( - physics._body, + body, physics.velocity[1], physics.velocity[2], physics.velocity[3] ) ode.BodySetAngularVel( - physics._body, + body, physics.angularVelocity[1], physics.angularVelocity[2], physics.angularVelocity[3] ) + physics._body = body end end -- cgit v1.2.1