summaryrefslogtreecommitdiff
path: root/honey/ecs-systems.lua
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-04-15 20:04:57 -0500
committersanine <sanine.not@pm.me>2023-04-15 20:04:57 -0500
commit02c6f822e2cca41d5d28afd9f3a05211316587fd (patch)
treeafb1fec88273f8fe123ae8750a75c146969f4d9a /honey/ecs-systems.lua
parente603997055259039cefcdceaece5604e3856e36d (diff)
add collision system
Diffstat (limited to 'honey/ecs-systems.lua')
-rw-r--r--honey/ecs-systems.lua61
1 files changed, 56 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