From b24a64fd45d71cfc18fea0e333906baceda031c8 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 9 May 2023 16:21:47 -0500 Subject: fix bug when getting components no entity has --- honey/ecs/collision.lua | 3 +++ honey/ecs/ecs.lua | 4 +++- honey/ecs/physics.lua | 57 ++++++++++++++++++++++++++++++++----------------- save | 24 ++++++++++----------- 4 files changed, 56 insertions(+), 32 deletions(-) diff --git a/honey/ecs/collision.lua b/honey/ecs/collision.lua index 722c256..5c0452b 100644 --- a/honey/ecs/collision.lua +++ b/honey/ecs/collision.lua @@ -26,6 +26,9 @@ local function createGeom(self, id, collision) print(normal, d) geom = ode.CreatePlane(self.space, normal[1], normal[2], normal[3], d) end + + ode.GeomSetData(geom, id) + collision._geom = geom collision._gc = honey.util.gc_canary(function() print("release geom for id"..id) diff --git a/honey/ecs/ecs.lua b/honey/ecs/ecs.lua index 301ed36..dda99cb 100644 --- a/honey/ecs/ecs.lua +++ b/honey/ecs/ecs.lua @@ -179,7 +179,9 @@ end -- get a specific component from an entity function EntityDb.getComponent(self, id, name) self:checkIsValid(id) - return self.components[name].data[id] + local components = self.components[name] + if not components then return nil end + return components.data[id] end diff --git a/honey/ecs/physics.lua b/honey/ecs/physics.lua index e2c9b9a..fe08359 100644 --- a/honey/ecs/physics.lua +++ b/honey/ecs/physics.lua @@ -91,6 +91,43 @@ local function createPhysicsBody(db, world, id, component) component._body = body end + +local function handleCollision(db, self, other) + local handler = db:getComponent(self, "onCollision") + if handler then + handler(db, self, other) + end +end + + +local function collide(self, a, b, collision) + -- check for collision handlers + local idA = ode.GeomGetData(a) + local idB = ode.GeomGetData(b) + handleCollision(self.db, idA, idB) + --handleCollision(self.db, idB, idA) + + -- set up the joint params + local contact = ode.CreateContact{ surface={ + mode = ode.ContactBounce + ode.ContactSoftCFM, + mu = ode.Infinity, + bounce = 0.90, + bounce_vel = 0.1, + soft_cfm = 0.001, + }} + ode.ContactSetGeom(contact, collision) + -- create the joint + local joint = ode.JointCreateContact( + self.world, + self.contactGroup, + contact + ) + -- attach the two bodies + local bodyA = ode.GeomGetBody(a) + local bodyB = ode.GeomGetBody(b) + ode.JointAttach(joint, bodyA, bodyB) +end + --===== physics =====-- @@ -129,25 +166,7 @@ system = function(params) -- check for actual collisions local collisions = ode.Collide(a, b, 1) if #collisions > 0 then - -- set up the joint params - local contact = ode.CreateContact{ surface={ - mode = ode.ContactBounce + ode.ContactSoftCFM, - mu = ode.Infinity, - bounce = 0.90, - bounce_vel = 0.1, - soft_cfm = 0.001, - }} - ode.ContactSetGeom(contact, collisions[1]) - -- create the joint - local joint = ode.JointCreateContact( - self.world, - self.contactGroup, - contact - ) - -- attach the two bodies - local bodyA = ode.GeomGetBody(a) - local bodyB = ode.GeomGetBody(b) - ode.JointAttach(joint, bodyA, bodyB) + collide(self, a, b, collisions[1]) end end) -- update the world diff --git a/save b/save index d46ce82..5e27ad1 100644 --- a/save +++ b/save @@ -1,12 +1,12 @@ -Entity("d4e23dec-aaf4-40fa-a26a-e69a8d37d698", {node={parent="f12474d1-7f6a-4417-9961-31dc12c3b2ae",matrix=Mat4{0.0872, -0.3243, 0.9419, 0.0000, 0.0000, 0.9455, 0.3256, 0.0000, -0.9962, -0.0284, 0.0824, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000},name="pivot"},onCursorPos={script="cameraCursorPos"},pitchyaw={yaw=85,pitch=-19}}) -Entity("ba792e78-0dcd-4b06-9c38-284c949bac85", {node={parent="d4e23dec-aaf4-40fa-a26a-e69a8d37d698",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 20.0000, 0.0000, 0.0000, 0.0000, 1.0000}},camera={projection=Mat4{1.8107, 0.0000, 0.0000, 0.0000, 0.0000, 2.4142, 0.0000, 0.0000, 0.0000, 0.0000, -1.0002, -0.2000, 0.0000, 0.0000, -1.0000, 0.0000},render="screen"},onWindowResize={script="scripts.cameraHandleResize"}}) -Entity("6f875858-6cae-42e6-a6ba-91f1bda03e02", {node={parent="e007393d-a45e-4666-9a9a-4f7f1d34a201",matrix=Mat4{2.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0000, -2.0000, 0.0000, 0.0000, 2.0000, -0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={mesh={index=1,filename="assets/skybox.obj"},textures={ourTexture={filename="assets/skyboxsun5deg2_tn.jpg"}},shader={vertex="vertex.glsl",fragment="fragment.glsl"}}}) -Entity("84711390-7b69-4b26-adb1-5a2531f9eceb", {node={matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0000, -1.0000, -0.0000, 0.0000, 1.0000, -0.0000, 10.0000, 0.0000, 0.0000, 0.0000, 1.0000}},ccamera={projection=Mat4{0.7500, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, -1.0002, -0.2000, 0.0000, 0.0000, -1.0000, 0.0000},render="screen"},onFramebufferSize={script="scripts.cameraHandleResize"}}) -Entity("12354901-ac00-480f-9c58-ae4537113e24", {onKey={script="scripts.loadSaveQuit"},onFramebufferSize={script="scripts.viewportResize"}}) -Entity("9c6a3bb3-fd5e-4cf2-b9aa-d7917ce70d07", {node={matrix=Mat4{0.9962, -0.0872, 0.0000, 0.0000, 0.0872, 0.9962, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},collision={class="plane"}}) -Entity("a693ee99-ca8d-41db-ba62-2bc9f1ddfac6", {node={parent="9c6a3bb3-fd5e-4cf2-b9aa-d7917ce70d07",matrix=Mat4{20.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0000, -20.0000, 0.0000, 0.0000, 20.0000, -0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={mesh={index=1,filename="builtin.quad"},textures={ourTexture={filename="assets/green+grass-1024x1024.jpg"}},shader={vertex="vertex.glsl",fragment="fragment.glsl"}}}) -Entity("744004be-b572-4bb2-8f45-10bcd9dc5363", {node={matrix=Mat4{-0.9713, -0.2377, 0.0040, 320.2245, -0.1047, 0.4425, 0.8906, 29.0165, -0.2135, 0.8647, -0.4547, -133.0247, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={mesh={index=1,filename="assets/icosahedron.obj"},textures={ourTexture={filename="77155.png"}},shader={vertex="vertex.glsl",fragment="fragment.glsl"}},collision={class="sphere",radius=1},physics={mass={density=1,class="sphere",radius=1},velocity=Vec3{-2.1659, -0.1895, -19.4739},angularVelocity=Vec3{-18.9198, -7.1841, 2.1742}}}) -Entity("41ce559d-445f-44ed-8966-4d08485d6f8d", {node={parent="744004be-b572-4bb2-8f45-10bcd9dc5363",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 2.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={mesh={index=1,filename="builtin.quad"},shader={vertex="vertex.glsl",fragment="fragment.glsl"}}}) -Entity("32118ff8-d697-41bf-bb7d-f0235bb18bc5", {renderQuad={textures={ourTexture={filename="44d9a0ec1c18e6126a5e9d9d9317f5ac.png"}},shader={vertex="vertex.glsl",fragment="fragment.glsl"}}}) -Entity("e007393d-a45e-4666-9a9a-4f7f1d34a201", {node={matrix=Mat4{1.0000, 0.0000, 0.0000, 262.8474, 0.0000, 0.0000, -1.0000, 26.1377, 0.0000, 1.0000, 0.0000, -65.3207, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={mesh={index=1,filename="assets/capsule.obj"},shader={vertex="vertex.glsl",fragment="fragment.glsl"}},collision={radius=1,length=2,class="capsule"},physics={mass={radius=1,density=1,class="capsule",length=2,direction=3},velocity=Vec3{-52.5918, -5.6735, -4.3262},maxAngularSpeed=0,angularVelocity=Vec3{0.0000, 0.0000, 0.0000}},script={script="scripts.capsuleMove"}}) -Entity("f12474d1-7f6a-4417-9961-31dc12c3b2ae", {node={parent="e007393d-a45e-4666-9a9a-4f7f1d34a201",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0000, 1.0000, 0.0000, 0.0000, -1.0000, -0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000},name="p"}}) +Entity("e95489ac-116c-49ca-981a-df5550eae1f5", {renderQuad={shader={fragment="fragment.glsl",vertex="vertex.glsl"},textures={ourTexture={filename="44d9a0ec1c18e6126a5e9d9d9317f5ac.png"}}}}) +Entity("069ecd42-7047-4b64-90f8-316bfe32fd67", {node={matrix=Mat4{1.0000, 0.0000, 0.0000, 4.0833, 0.0000, 0.0000, -1.0000, 2.3528, 0.0000, 1.0000, 0.0000, -0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={shader={fragment="fragment.glsl",vertex="vertex.glsl"},mesh={filename="assets/capsule.obj",index=1}},collision={radius=1,length=2,class="capsule"},physics={angularVelocity=Vec3{0.0000, 0.0000, 0.0000},mass={radius=1,length=2,density=1,direction=3,class="capsule"},velocity=Vec3{-0.0229, -0.0020, 0.0000},maxAngularSpeed=0},script={script="scripts.capsuleMove"}}) +Entity("4b19c122-9ca7-4cfa-941a-01a22624dbab", {node={parent="069ecd42-7047-4b64-90f8-316bfe32fd67",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0000, 1.0000, 0.0000, 0.0000, -1.0000, -0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000},name="p"}}) +Entity("a57cc635-fe17-42a2-9f96-0edf92e59aee", {node={parent="4b19c122-9ca7-4cfa-941a-01a22624dbab",matrix=Mat4{-0.9063, -0.2596, 0.3335, 0.0000, 0.0000, 0.7891, 0.6143, 0.0000, -0.4226, 0.5567, -0.7152, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000},name="pivot"},pitchyaw={yaw=-205,pitch=-37.9},onCursorPos={script="cameraCursorPos"}}) +Entity("b768e8d7-39b4-4414-b98b-fac4fbbaa516", {onWindowResize={script="scripts.cameraHandleResize"},camera={projection=Mat4{1.8107, 0.0000, 0.0000, 0.0000, 0.0000, 2.4142, 0.0000, 0.0000, 0.0000, 0.0000, -1.0002, -0.2000, 0.0000, 0.0000, -1.0000, 0.0000},render="screen"},node={parent="a57cc635-fe17-42a2-9f96-0edf92e59aee",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 20.0000, 0.0000, 0.0000, 0.0000, 1.0000}}}) +Entity("73d66887-4a79-4265-969d-1b9720e521fc", {node={parent="069ecd42-7047-4b64-90f8-316bfe32fd67",matrix=Mat4{2.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0000, -2.0000, 0.0000, 0.0000, 2.0000, -0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={shader={fragment="fragment.glsl",vertex="vertex.glsl"},mesh={filename="assets/skybox.obj",index=1},textures={ourTexture={filename="assets/skyboxsun5deg2_tn.jpg"}}}}) +Entity("526d5532-aa4e-4263-b701-a5743cb77cd1", {ccamera={projection=Mat4{0.7500, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, -1.0002, -0.2000, 0.0000, 0.0000, -1.0000, 0.0000},render="screen"},onFramebufferSize={script="scripts.cameraHandleResize"},node={matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0000, -1.0000, -0.0000, 0.0000, 1.0000, -0.0000, 10.0000, 0.0000, 0.0000, 0.0000, 1.0000}}}) +Entity("c2cb5cb0-d0fb-49a6-9226-9a47e22e568f", {onKey={script="scripts.loadSaveQuit"},onFramebufferSize={script="scripts.viewportResize"}}) +Entity("969e4aea-ffef-4e60-8d83-6307c7d6480a", {collision={class="plane"},node={matrix=Mat4{0.9962, -0.0872, 0.0000, 0.0000, 0.0872, 0.9962, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}}}) +Entity("5910b165-cd58-430d-8f55-726df47baaf8", {node={parent="969e4aea-ffef-4e60-8d83-6307c7d6480a",matrix=Mat4{20.0000, 0.0000, 0.0000, 0.0000, 0.0000, -0.0000, -20.0000, 0.0000, 0.0000, 20.0000, -0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={textures={ourTexture={filename="assets/green+grass-1024x1024.jpg"}},mesh={filename="builtin.quad",index=1},shader={fragment="fragment.glsl",vertex="vertex.glsl"}}}) +Entity("5bb55536-bbf8-47b6-b8c6-f00594fffcca", {node={matrix=Mat4{0.7256, 0.6882, 0.0000, 1.5353, -0.6882, 0.7256, 0.0000, 1.1348, 0.0000, -0.0000, 1.0000, -0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={textures={ourTexture={filename="77155.png"}},mesh={filename="assets/icosahedron.obj",index=1},shader={fragment="fragment.glsl",vertex="vertex.glsl"}},collision={class="sphere",radius=1},physics={mass={density=1,class="sphere",radius=1},velocity=Vec3{-0.8081, -0.0707, -0.0000},angularVelocity=Vec3{-0.0000, -0.0000, 0.8112}}}) +Entity("127cb719-4960-463b-baaf-641316baabc2", {node={parent="5bb55536-bbf8-47b6-b8c6-f00594fffcca",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 2.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={shader={fragment="fragment.glsl",vertex="vertex.glsl"},mesh={filename="builtin.quad",index=1}}}) -- cgit v1.2.1