From b24a64fd45d71cfc18fea0e333906baceda031c8 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 9 May 2023 16:21:47 -0500 Subject: fix bug when getting components no entity has --- honey/ecs/collision.lua | 3 +++ honey/ecs/ecs.lua | 4 +++- honey/ecs/physics.lua | 57 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 44 insertions(+), 20 deletions(-) (limited to 'honey/ecs') diff --git a/honey/ecs/collision.lua b/honey/ecs/collision.lua index 722c256..5c0452b 100644 --- a/honey/ecs/collision.lua +++ b/honey/ecs/collision.lua @@ -26,6 +26,9 @@ local function createGeom(self, id, collision) print(normal, d) geom = ode.CreatePlane(self.space, normal[1], normal[2], normal[3], d) end + + ode.GeomSetData(geom, id) + collision._geom = geom collision._gc = honey.util.gc_canary(function() print("release geom for id"..id) diff --git a/honey/ecs/ecs.lua b/honey/ecs/ecs.lua index 301ed36..dda99cb 100644 --- a/honey/ecs/ecs.lua +++ b/honey/ecs/ecs.lua @@ -179,7 +179,9 @@ end -- get a specific component from an entity function EntityDb.getComponent(self, id, name) self:checkIsValid(id) - return self.components[name].data[id] + local components = self.components[name] + if not components then return nil end + return components.data[id] end diff --git a/honey/ecs/physics.lua b/honey/ecs/physics.lua index e2c9b9a..fe08359 100644 --- a/honey/ecs/physics.lua +++ b/honey/ecs/physics.lua @@ -91,6 +91,43 @@ local function createPhysicsBody(db, world, id, component) component._body = body end + +local function handleCollision(db, self, other) + local handler = db:getComponent(self, "onCollision") + if handler then + handler(db, self, other) + end +end + + +local function collide(self, a, b, collision) + -- check for collision handlers + local idA = ode.GeomGetData(a) + local idB = ode.GeomGetData(b) + handleCollision(self.db, idA, idB) + --handleCollision(self.db, idB, idA) + + -- set up the joint params + local contact = ode.CreateContact{ surface={ + mode = ode.ContactBounce + ode.ContactSoftCFM, + mu = ode.Infinity, + bounce = 0.90, + bounce_vel = 0.1, + soft_cfm = 0.001, + }} + ode.ContactSetGeom(contact, collision) + -- create the joint + local joint = ode.JointCreateContact( + self.world, + self.contactGroup, + contact + ) + -- attach the two bodies + local bodyA = ode.GeomGetBody(a) + local bodyB = ode.GeomGetBody(b) + ode.JointAttach(joint, bodyA, bodyB) +end + --===== physics =====-- @@ -129,25 +166,7 @@ system = function(params) -- check for actual collisions local collisions = ode.Collide(a, b, 1) if #collisions > 0 then - -- set up the joint params - local contact = ode.CreateContact{ surface={ - mode = ode.ContactBounce + ode.ContactSoftCFM, - mu = ode.Infinity, - bounce = 0.90, - bounce_vel = 0.1, - soft_cfm = 0.001, - }} - ode.ContactSetGeom(contact, collisions[1]) - -- create the joint - local joint = ode.JointCreateContact( - self.world, - self.contactGroup, - contact - ) - -- attach the two bodies - local bodyA = ode.GeomGetBody(a) - local bodyB = ode.GeomGetBody(b) - ode.JointAttach(joint, bodyA, bodyB) + collide(self, a, b, collisions[1]) end end) -- update the world -- cgit v1.2.1