From 64194866e9a1d6991efc9f44a0e3075905ab6470 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 14 Apr 2023 00:55:50 -0500 Subject: begin physics reimplementation --- honey/ecs-systems.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) (limited to 'honey') diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index ed51e5d..cfd5675 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -1,6 +1,10 @@ local ecs = require 'honey.ecs' +local glm = require 'honey.glm' +local Vec3 = glm.Vec3 +local Quaternion = glm.Quaternion local gl = honey.gl local glfw = honey.glfw +local ode = honey.ode local module = {} @@ -36,6 +40,8 @@ end transform = function(params) return { db = params.db, + + priority = 1, update = function(self, dt) local entities = self.db:queryComponent("transform") @@ -66,7 +72,6 @@ transform = function(params) recursiveTransform(transform) end end, - priority = 0, } end @@ -160,9 +165,50 @@ physics = function(params) world=params.world, contactGroup=ode.JointGroupCreate(groupSize), time=interval, + + priority=0, update=function(self, dt) local query = self.db:queryComponent("physics") - -- TODO: create physics bodies + for id, physics in pairs(query) do + if not physics._body then + physics._body = ode.BodyCreate(self.world) + local mass = ode.MassCreate() + local class = physics.mass.class + if not class then + -- configure mass manually + elseif class == "sphere" then + ode.MassSetSphere( + mass, + physics.mass.density, + physics.mass.radius + ) + end + ode.BodySetMass(physics._body, mass) + local m = self.db:getComponent(id, "transform").matrix + ode.BodySetPosition( + physics._body, + m[1][4], m[2][4], m[3][4] + ) + ode.BodySetRotation( + physics._body, + m[1][1], m[1][2], m[1][3], + m[2][1], m[2][2], m[2][3], + m[3][1], m[3][2], m[3][3] + ) + ode.BodySetLinearVel( + physics._body, + physics.velocity[1], + physics.velocity[2], + physics.velocity[3] + ) + ode.BodySetAngularVel( + physics._body, + physics.angularVelocity[1], + physics.angularVelocity[2], + physics.angularVelocity[3] + ) + end + end self.time = self.time + dt -- only run the update every [interval] seconds @@ -204,7 +250,9 @@ physics = function(params) for id, physics in pairs(query) do local x,y,z = ode.BodyGetPosition(physics._body) local d,a,b,c = ode.BodyGetQuaternion(physics._body) - entity.transform + local transform = self.db:getComponent(id, "transform") + local q = Quaternion{a,b,c,d} + transform.matrix :identity() :translate(Vec3{x,y,z}) :mul(Quaternion{a,b,c,d}:toMat4()) -- cgit v1.2.1