Compare commits

...

3 Commits

3 changed files with 46 additions and 14 deletions

View File

@ -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

View File

@ -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

View File

@ -220,14 +220,30 @@ function Vector3:rotated(axis, angle)
return vaxis
end
---@diagnostic disable-next-line: undefined-field
vaxis = vaxis:normalized()
local cosa = math.cos(angle)
local sina = math.sin(angle)
-- __mul is only defined for the left operand (table), numbers don't get metatables.
-- as such, the ordering of operations here is specific
local v = (self * cosa) + (vaxis * ((1 - cosa) * self:dot(vaxis))) + (vaxis:cross(self) * sina)
return Vector3(v)
return (self * cosa) + (vaxis * ((1 - cosa) * self:dot(vaxis))) + (vaxis:cross(self) * sina)
end
---In-place version of rotated.
---@param axis Vector3
---@param angle number
---@return Vector3
function Vector3:rotate(axis, angle)
local cosa = math.cos(angle)
local sina = math.sin(angle)
local dot = self:dot(axis)
local cross = axis:cross(self)
self.x = self.x * cosa + axis.x * ((1 - cosa) * dot) + cross.x * sina
self.y = self.y * cosa + axis.y * ((1 - cosa) * dot) + cross.y * sina
self.z = self.z * cosa + axis.z * ((1 - cosa) * dot) + cross.z * sina
return self
end
---Returns a copy of this vector.
@ -293,12 +309,18 @@ function Vector3:horizontal()
end
---- CONSTANTS
---@type Vector3
Vector3.UP = Vector3(0, 1, 0)
Vector3.DOWN = -Vector3.UP
---@type Vector3
Vector3.DOWN = Vector3(0, -1, 0)
---@type Vector3
Vector3.FORWARD = Vector3(0, 0, -1)
Vector3.BACK = -Vector3.FORWARD
---@type Vector3
Vector3.BACK = Vector3(0, 0, 1)
---@type Vector3
Vector3.RIGHT = Vector3(1, 0, 0)
Vector3.LEFT = -Vector3.RIGHT
---@type Vector3
Vector3.LEFT = Vector3(-1, 0, 0)
-------------------
return Vector3