diff options
Diffstat (limited to 'honey/ecs/collision.lua')
-rw-r--r-- | honey/ecs/collision.lua | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/honey/ecs/collision.lua b/honey/ecs/collision.lua new file mode 100644 index 0000000..722c256 --- /dev/null +++ b/honey/ecs/collision.lua @@ -0,0 +1,57 @@ +local glm = require 'honey.glm' +local Vec3 = glm.Vec3 +local ode = honey.ode + +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) + + +--===== collision space =====-- + + +local function createGeom(self, id, collision) + local geom + if collision.class == "sphere" then + geom = ode.CreateSphere(self.space, collision.radius) + elseif collision.class == "capsule" then + geom = ode.CreateCapsule(self.space, collision.radius, collision.length) + elseif collision.class == "plane" then + local node = self.db:getComponent(id, "node") + local m = node.matrix + local normal = node.matrix:mulv3(Vec3{0,1,0}):normalize() + local position = Vec3{m[1][4], m[2][4], m[3][4]} + print(position) + local d = normal:dot(position) + print(normal, d) + geom = ode.CreatePlane(self.space, normal[1], normal[2], normal[3], d) + end + collision._geom = geom + collision._gc = honey.util.gc_canary(function() + print("release geom for id"..id) + ode.GeomDestroy(geom) + end) +end + +system = function(params) + local db = params.db + local space = params.space + return { + db=db, + space=space, + priority=0, + update = function(self, dt) + local query = self.db:queryComponent("collision") + for id, collision in pairs(query) do + if not collision._geom then + createGeom(self, id, collision) + print(id, collision._geom) + end + end + end + } +end + + + +return module |