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/physics.lua | 57 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'honey/ecs/physics.lua') 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