From 40c634a1650b2b666f92d1eafa3271dbe5a33b69 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 8 May 2023 01:38:39 -0500 Subject: add simple character movement --- main.lua | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index 4372a03..62f0422 100644 --- a/main.lua +++ b/main.lua @@ -74,6 +74,18 @@ function setupEntities() class = "plane", }, }) + + local planeMesh = entities:createEntityWithComponents{ + transform = { + parent = plane, + matrix = Mat4():identity():rotateX(0.5*math.pi):scale(Vec3{20,20,20}), + }, + renderMesh = { + textures = { ourTexture={ filename="assets/green+grass-1024x1024.jpg" } }, + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + mesh = { filename="builtin.quad", index=1 }, + }, + } @@ -170,12 +182,17 @@ function setupEntities() yaw = 0, }, onCursorPos = { script = "cameraCursorPos" }, + script = { script = "capsuleMove" }, } package.loaded["cameraCursorPos"] = (function() - local prevx, prevy = 0, 0 + local prevx, prevy return function(entities, id, data) + if not prevx then + prevx, prevy = data.xpos, data.ypos + end local dx = data.xpos - prevx local dy = data.ypos - prevy + print(dx,dy) prevx, prevy = data.xpos, data.ypos local transform = entities:getComponent(id, "transform") @@ -192,6 +209,43 @@ function setupEntities() end end)() + package.loaded["capsuleMove"] = function(entities, id, dt) + local pressed = function(key) + return glfw.GetKey(window.win, key) == glfw.PRESS + end + + local py = entities:getComponent(id, "pitchyaw") + local yaw = math.rad(py.yaw) + local forward = Vec3{-math.sin(yaw), 0, -math.cos(yaw)} + local left = Vec3{-math.cos(yaw), 0, math.sin(yaw)} + + print(forward, left) + + local capsule = entities:getComponent(capsule, "physics") + + local vel = Vec3{0,0,0} + + if pressed(glfw.KEY_W) then + vel = vel + forward + end + if pressed(glfw.KEY_A) then + vel = vel + left + end + if pressed(glfw.KEY_S) then + vel = vel - forward + end + if pressed(glfw.KEY_D) then + vel = vel - left + end + + vel = 10000 * vel:normalize() + + x, y, z = ode.BodyGetLinearVel(capsule._body) + if Vec3{x,y,z}:norm2() < 500 then + ode.BodyAddForce(capsule._body, vel[1], vel[2], vel[3]) + end + end + local capcam = entities:createEntityWithComponents{ camera = { -- cgit v1.2.1