diff options
author | sanine <sanine.not@pm.me> | 2023-04-15 20:04:57 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-04-15 20:04:57 -0500 |
commit | 02c6f822e2cca41d5d28afd9f3a05211316587fd (patch) | |
tree | afb1fec88273f8fe123ae8750a75c146969f4d9a /honey | |
parent | e603997055259039cefcdceaece5604e3856e36d (diff) |
add collision system
Diffstat (limited to 'honey')
-rw-r--r-- | honey/ecs-systems.lua | 61 | ||||
-rw-r--r-- | honey/glm.lua | 6 |
2 files changed, 62 insertions, 5 deletions
diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index 47c0f6d..c287d06 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -1,5 +1,6 @@ local ecs = require 'honey.ecs' local glm = require 'honey.glm' +local Mat4 = glm.Mat4 local Vec3 = glm.Vec3 local Quaternion = glm.Quaternion local gl = honey.gl @@ -41,7 +42,7 @@ transform = function(params) return { db = params.db, - priority = 1, + priority = 2, update = function(self, dt) local entities = self.db:queryComponent("transform") @@ -169,7 +170,7 @@ physics = function(params) contactGroup=ode.JointGroupCreate(groupSize), time=interval, - priority=0, + priority=1, update=function(self, dt) for i, ref in ipairs(refs) do print(i, ref.tbl, ref.physics) @@ -186,6 +187,12 @@ physics = function(params) body = nil end) + local collision = self.db:getComponent(id, "collision") + if collision then + print(id, collision.class) + ode.GeomSetBody(collision._geom, body) + end + local mass = ode.MassCreate() local class = physics.mass.class if not class then @@ -247,12 +254,12 @@ physics = function(params) -- create the joint local joint = ode.JointCreateContact( self.world, - self.contactgroup, + self.contactGroup, contact ) -- attach the two bodies - local bodyA = ode.GeomGetData(a) - local bodyB = ode.GeomGetData(b) + local bodyA = ode.GeomGetBody(a) + local bodyB = ode.GeomGetBody(b) ode.JointAttach(joint, bodyA, bodyB) end end) @@ -283,4 +290,48 @@ physics = function(params) end +--===== 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 == "plane" then + local transform = self.db:getComponent(id, "transform") + local m = transform.matrix + local normal = transform.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 + +function collision(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 diff --git a/honey/glm.lua b/honey/glm.lua index 3357815..c950b9a 100644 --- a/honey/glm.lua +++ b/honey/glm.lua @@ -167,6 +167,7 @@ end function Vec3.normalize(self) glm.vec3_normalize(self.data) + return self end function Vec3.normalizeTo(self, dest) glm.vec3_normalize_to(self.data, dest.data) @@ -288,6 +289,11 @@ function Mat4.mul(self, other) glm.mat4_mul(self.data, other.data, self.data) return self end +function Mat4.mulv3(self, v) + local dest = Vec3() + glm.mat4_mulv3(self.data, v.data, 1.0, dest.data) + return dest +end function Mat4.translate(self, vec) |