From e5553bb81c93cd76456d07d3a1d7e8bb1b249b6e Mon Sep 17 00:00:00 2001 From: sanine-a Date: Thu, 23 Mar 2023 13:35:37 -0500 Subject: implement skeleton of physics system --- honey/ecs-systems.lua | 8 ++++---- honey/ecs.lua | 19 +++++++++++-------- honey/mat4.lua | 6 ++++++ honey/quaternion.lua | 30 ++++++++++++++++++++++++++++++ honey/std.lua | 1 + 5 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 honey/quaternion.lua (limited to 'honey') diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index a511b3d..237aa78 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -28,11 +28,11 @@ end -- update transforms transformCascade = { filter=ecs.Filter.AND{"transform", "parent"}, - preUpdate=function(entity) + prepareEntity=function(self, entity) entity._transform = nil entity._transformComputed = false end, - update=function(entity, dt) + update=function(self, entity, dt) recursiveComputeTransform(entity) end, priority=98, @@ -45,7 +45,7 @@ function renderCam(camera, priority) local priority = priority or 99 return { filter=ecs.Filter.AND{"mesh", "shader", "transform"}, - update=function(entity, dt) + update=function(self, entity, dt) entity.shader:use() entity.shader:configure{ matrix={ @@ -64,7 +64,7 @@ end update = { filter=ecs.Filter.AND{"update"}, - update=function(entity, dt) + update=function(self, entity, dt) entity.update(entity, dt) end, priority=50, diff --git a/honey/ecs.lua b/honey/ecs.lua index 23bb0ba..d611b37 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -123,6 +123,9 @@ function Level.addSystem(self, system) system.entities = {} table.insert(self.systems, system) table.sort(self.systems, systemLt) + if system.setup then + system.setup(system) + end end @@ -131,7 +134,7 @@ local function addEntityToSystem(system, id, entity) if system.entities[id] then return end if system.onAddEntity then - system.onAddEntity(id, entity) + system:onAddEntity(id, entity) end system.entities[id] = true end @@ -142,7 +145,7 @@ local function removeEntityFromSystem(system, id, entity) if not system.entities[id] then return end if system.onRemoveEntity then - system.onRemoveEntity(id, entity) + system:onRemoveEntity(id, entity) end system.entities[id] = nil end @@ -195,20 +198,20 @@ end function Level.update(self, dt) for _, system in ipairs(self.systems) do if system.preUpdate then + system:preUpdate() + end + if system.prepareEntity then for id in pairs(system.entities) do local entity = self.entities[id] - system.preUpdate(entity) + system:prepareEntity(entity) end end for id in pairs(system.entities) do local entity = self.entities[id] - system.update(entity, dt) + system:update(entity, dt) end if system.postUpdate then - for id in pairs(system.entities) do - local entity = self.entities[id] - system.postUpdate(entity) - end + system:postUpdate() end end end diff --git a/honey/mat4.lua b/honey/mat4.lua index 6ea17fd..cdc5e7d 100644 --- a/honey/mat4.lua +++ b/honey/mat4.lua @@ -114,6 +114,12 @@ function Mat4.zero(self) end +function Mat4.mul(self, other) + glm.mat4_mul(self.data, other.data, self.data) + return self +end + + function Mat4.translate(self, vec) glm.translate(self.data, vec.data) return self diff --git a/honey/quaternion.lua b/honey/quaternion.lua new file mode 100644 index 0000000..6fbfdd5 --- /dev/null +++ b/honey/quaternion.lua @@ -0,0 +1,30 @@ +local glm = honey.glm +local Mat4 = require "honey.mat4" + +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) + +Quaternion = {} +Quaternion.__index = Quaternion + + +function Quaternion.new(_, tbl) + local tbl = tbl or { 0, 0, 0, 0 } + local self = {} + self.data = glm.quat_create() + glm.quat_init(self.data, unpack(tbl)) + setmetatable(self, Quaternion) + return self +end +setmetatable(Quaternion, {__call=Quaternion.new}) + + +function Quaternion.toMat4(self) + local m = Mat4() + glm.quat_mat4(self.data, m.data) + return m +end + + +return module.Quaternion diff --git a/honey/std.lua b/honey/std.lua index 7cb7695..80b071e 100644 --- a/honey/std.lua +++ b/honey/std.lua @@ -11,3 +11,4 @@ honey.mesh = require 'honey.mesh' honey.Shader = require 'honey.shader' honey.Vec3 = require 'honey.vec3' honey.Window = require 'honey.window' +honey.Quaternion = require 'honey.quaternion' -- cgit v1.2.1