From a2ae7aae8357c8c2684d85fd58b0c5a0563ebab9 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 9 May 2023 11:31:17 -0500 Subject: refactor: split ecs systems into multiple files --- honey/ecs/collision.lua | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 honey/ecs/collision.lua (limited to 'honey/ecs/collision.lua') 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 -- cgit v1.2.1