diff options
author | sanine <sanine.not@pm.me> | 2023-04-14 00:55:50 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-04-14 00:55:50 -0500 |
commit | 64194866e9a1d6991efc9f44a0e3075905ab6470 (patch) | |
tree | 301db50b714db1ef5d9f103ec1c9a431a1a8de34 /main.lua | |
parent | 371e3a188e190a9c8b53dc6eef02d4eea72db826 (diff) |
begin physics reimplementation
Diffstat (limited to 'main.lua')
-rw-r--r-- | main.lua | 86 |
1 files changed, 56 insertions, 30 deletions
@@ -7,6 +7,7 @@ local Mat4 = honey.Mat4 local Quaternion = honey.Quaternion local ecs = honey.ecs local sys = honey.standardSystems +local dispatch = sys.dispatch local ode = honey.ode local nvg = honey.nvg @@ -19,6 +20,16 @@ local vg = nvg.CreateContext() local november = nvg.CreateFont(vg, "November", "assets/november.regular.ttf") local vw, vh = 640, 480 +-- setup physics +local space = ode.HashSpaceCreate(ode.Space0) +local world = ode.WorldCreate() +ode.WorldSetGravity(world, 0, -10, 0) +ode.WorldSetCFM(world, 1e-5) +local physicsGc = honey.util.gc_canary(function() + ode.SpaceDestroy(space) + ode.WorldDestroy(world) +end) + -- setup ecs local entities = ecs.EntityDb() @@ -32,63 +43,78 @@ entities:addComponents(camera, { transform={ matrix=Mat4():identity():translate(Vec3{0, 0, -6}), }, + z = {value=-6}, script={ script="cameraRotationScript", }, + onKey = { + script="cameraKeyHandler", + }, }) ---entities:addComponent(camera, "camera", { --- projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), ---}) ---entities:addComponent(camera, "transform", { --- matrix=Mat4():identity():translate(Vec3{0, 0, -6}), ---}) ---entities:addComponent(camera, "script", { --- name = "cameraRotationScript", ---}) --- systems:addSystem(sys.transform) systems:addSystem(sys.renderCamera, {camera=camera}) systems:addSystem(sys.script) +systems:addSystem(sys.physics, {space=space, world=world}) package.loaded['baseRotationScript'] = function(entities, id, dt) local transform = entities:getComponent(id, "transform") transform.matrix:rotateZ(math.pi * dt) end package.loaded['cameraRotationScript'] = function(entities, id, dt) local transform = entities:getComponent(id, "transform") - transform.matrix:identity():translate(Vec3{0, 0, -6 + math.sin(math.pi * glfw.GetTime())}) + local z = entities:getComponent(id, "z") + transform.matrix + :identity() + :translate(Vec3{0, 0, z.value + math.sin(math.pi * glfw.GetTime())}) +end +package.loaded['cameraKeyHandler'] = function(entities, id, data) + local z = entities:getComponent(id, "z") + if data.key == glfw.KEY_W then + z.value = z.value + 1 + elseif data.key == glfw.KEY_S then + z.value = z.value - 1 + end end local id = entities:createEntity() -entities:addComponent(id, "renderMesh", { - textures = { - ourTexture={ filename="77155.png" }, +entities:addComponents(id, { + renderMesh = { + textures = { ourTexture={ filename="77155.png" } }, + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + mesh = { filename="assets/icosahedron.obj", index=1 }, + }, + transform = { + matrix = Mat4():identity():rotateZ(math.rad(45)), + }, + physics = { + mass = { + class = "sphere", + density = 1, + radius = 1, + }, + velocity = Vec3{ 0, 0, 0 }, + angularVelocity = Vec3{ 0, 0, 0 }, }, - shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/icosahedron.obj", index=1 }, -}) -entities:addComponent(id, "transform", { - matrix = Mat4():identity():rotateZ(math.rad(45)), -}) -entities:addComponent(id, "script", { - script = "baseRotationScript", }) local id2 = entities:createEntity() -entities:addComponent(id2, "renderMesh", { - shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/tetrahedron.obj", index=1 }, -}) -entities:addComponent(id2, "transform", { - parent=id, - matrix=Mat4():identity():translate(Vec3{0, 2, 0}), +entities:addComponents(id2, { + renderMesh = { + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + mesh = { filename="assets/tetrahedron.obj", index=1 }, + }, + transform = { + parent=id, + matrix=Mat4():identity():translate(Vec3{0, 2, 0}), + }, }) -- close window on ESCAPE key window:setKeyCallback(function(_, key, scancode, action) + dispatch(entities, "onKey", {key=key, scancode=scancode, action=action}) if action == glfw.PRESS then if key == glfw.KEY_ESCAPE then window:setShouldClose(true) @@ -100,7 +126,7 @@ end) window:setFramebufferSizeCallback(function(_, width, height) gl.Viewport(0, 0, width, height) vw, vh = width, height - sys.dispatch(entities, "onWindowResize", { width=width, height=height }) + dispatch(entities, "onWindowResize", { width=width, height=height }) end) package.loaded["cameraHandleResize"] = function(entities, id, data) |