diff options
Diffstat (limited to 'honey/ecs/physics.lua')
-rw-r--r-- | honey/ecs/physics.lua | 57 |
1 files changed, 38 insertions, 19 deletions
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 |