From 447ef02774712b36f3c96f1016e77b3218870fd1 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 26 Mar 2023 00:09:14 -0500 Subject: fix invisible physics objects bug --- main.lua | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/main.lua b/main.lua index ceba194..2463bdd 100644 --- a/main.lua +++ b/main.lua @@ -1,5 +1,6 @@ require 'honey.std' local paused = true +local balls = {} local function formatize(f) return function(fmt, ...) @@ -44,7 +45,7 @@ local vw, vh = 640, 480 -- create camera matrices local camera = { - view=Mat4():identity():translate(Vec3{0, 0, -20}), + view=Mat4():identity():translate(Vec3{0, 0, -30}), projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), } @@ -89,6 +90,8 @@ level:addSystem{ } local collisions = ode.Collide(o1, o2, 1) if #collisions > 0 then + local e1 = ode.GeomGetData(o1) + local e2 = ode.GeomGetData(o2) ode.ContactSetGeom(contact, collisions[1]) local joint = ode.JointCreateContact(self.world, self.contactgroup, contact) ode.JointAttach(joint, b1, b2) @@ -108,6 +111,11 @@ level:addSystem{ local d, a, b, c = ode.BodyGetQuaternion(entity.physicsBody) entity.transform:identity():translate(Vec3{x, y, z}):mul(Quaternion{a, b, c, d}:toMat4()) end, + onRemoveEntity = function(self, id, entity) + print("remove", id) + ode.GeomDestroy(entity.collisionShape) + ode.BodyDestroy(entity.physicsBody) + end, } -- create shader @@ -152,11 +160,11 @@ local leaf = { mesh=tetra, shader=shader, } ---local root = growLine(leaf, 24) ---root.update = function(self, dt) --- self.transform:rotateY(0.2 * math.pi * dt) ---end ---level:addEntity(root) +local root = growLine(leaf, 24) +root.update = function(self, dt) + self.transform:rotateY(0.2 * math.pi * dt) +end +level:addEntity(root) local groundPlane = { transform=Mat4():identity():translate(Vec3{0, -2, 0}):scale(Vec3{10, 10, 10}), @@ -167,7 +175,6 @@ local groundPlane = { level:addEntity(groundPlane) -local balls = {} local function createNewBall() local ball = { transform=Mat4():identity(), @@ -177,7 +184,9 @@ local function createNewBall() collisionShape=ode.CreateSphere(space, 1.0), physicsBody=ode.BodyCreate(world), } + local id = level:addEntity(ball) + ball.id = id table.insert(balls, id) if #balls > 200 then level:removeEntity(table.remove(balls, 1)) @@ -191,6 +200,7 @@ local function createNewBall() ode.GeomSetBody(ball.collisionShape, ball.physicsBody) ode.BodySetPosition(ball.physicsBody, x, y, z) ode.BodySetLinearVel(ball.physicsBody, x, y, z) + ode.GeomSetData(ball.collisionShape, ball) end createNewBall() @@ -203,7 +213,15 @@ window:setKeyCallback(function(_, key, scancode, action) window:setShouldClose(true) elseif key == glfw.KEY_SPACE then paused = not paused - elseif key == glfw.KEY_B or key == glfw.KEY_V then + elseif + key == glfw.KEY_Z or + key == glfw.KEY_X or + key == glfw.KEY_C or + key == glfw.KEY_V or + key == glfw.KEY_B or + key == glfw.KEY_N or + key == glfw.KEY_M + then createNewBall() end end -- cgit v1.2.1