From 342c98e9154a736e608380ce05d8b0b82ff45893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sun, 23 Feb 2025 17:08:20 +0300 Subject: [PATCH] add rotation field to obj --- data/scripts/classes/obj.lua | 16 ++++++++++++---- data/scripts/game.lua | 10 ++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/data/scripts/classes/obj.lua b/data/scripts/classes/obj.lua index 311babd..197d134 100644 --- a/data/scripts/classes/obj.lua +++ b/data/scripts/classes/obj.lua @@ -3,6 +3,7 @@ ---@field texture string ---@field texture_size integer ---@field position Vector3 +---@field rotation Vector3 ---@field triangles table local Obj = {} @@ -50,7 +51,7 @@ function Obj.create(model, texture, position) texture = texture.file, texture_size = texture.size, position = pos:copy(), - + rotation = Vector3(), triangles = {} } obj.position = obj.position:copy() @@ -112,12 +113,19 @@ function Obj.create(model, texture, position) return obj end +local function rotate_vertex(obj, v) + local vec = Vector3(v) + --- YXZ (yaw pitch roll) minimizes gimbal lock + -- return vec:rotate(Vector3.UP, obj.rotation.y):rotate(Vector3.LEFT, obj.rotation.x):rotate(Vector3.FORWARD, obj.rotation.z) + return vec:rotate(Vector3.UP, obj.rotation.y):rotate(Vector3.LEFT, obj.rotation.x):rotate(Vector3.FORWARD, obj.rotation.z) +end + function Obj:draw() for _, triangle in ipairs(self.triangles) do local newt = util.shallow_copy(triangle) - newt.v0 = Vector3(triangle.v0) + self.position - newt.v1 = Vector3(triangle.v1) + self.position - newt.v2 = Vector3(triangle.v2) + self.position + newt.v0 = rotate_vertex(self, triangle.v0) + self.position + newt.v1 = rotate_vertex(self, triangle.v1) + self.position + newt.v2 = rotate_vertex(self, triangle.v2) + self.position draw_triangle(newt) end end diff --git a/data/scripts/game.lua b/data/scripts/game.lua index 6454492..3ea7d45 100644 --- a/data/scripts/game.lua +++ b/data/scripts/game.lua @@ -5,7 +5,7 @@ local List = require "types.list" local Obj = require "classes.obj" -local cube = Obj.create("models/unit_cube.obj", {file = "images/measure002a.png", size = 512}, Vector3(0, 1, 0)) +local cube = Obj.create("models/unit_cube.obj", {file = "images/measure002a.png", size = 512}, Vector3(0, 1, 1)) local Feed = require "classes.feed" ---@type List @@ -44,7 +44,7 @@ end function game_tick() -- ctx.initialization_needed is true first frame and every time dynamic reload is performed if ctx.initialization_needed then - audio_play{audio = "music/bg1.xm", loops = true, channel = "music"} + -- audio_play{audio = "music/bg1.xm", loops = true, channel = "music"} player.ThrowPressed:connect(create_feed) -- spawn some ducks @@ -86,7 +86,9 @@ function game_tick() texture_region = { x = 0, y = 0, w = 512, h = 512 }, } draw_quad(util.merge(q, params)) - cube.position.x = math.sin(ctx.frame_number * 0.01) - cube.position.z = math.cos(ctx.frame_number * 0.01) + cube.position.y = math.sin(ctx.frame_number * 0.05) + -- cube.position.z = math.cos(ctx.frame_number * 0.01) + cube.rotation.x = cube.rotation.x + 0.01 + cube.rotation.z = cube.rotation.z + 0.01 cube:draw() end