add rotation field to obj
This commit is contained in:
parent
1e2a162800
commit
342c98e915
@ -3,6 +3,7 @@
|
|||||||
---@field texture string
|
---@field texture string
|
||||||
---@field texture_size integer
|
---@field texture_size integer
|
||||||
---@field position Vector3
|
---@field position Vector3
|
||||||
|
---@field rotation Vector3
|
||||||
---@field triangles table
|
---@field triangles table
|
||||||
local Obj = {}
|
local Obj = {}
|
||||||
|
|
||||||
@ -50,7 +51,7 @@ function Obj.create(model, texture, position)
|
|||||||
texture = texture.file,
|
texture = texture.file,
|
||||||
texture_size = texture.size,
|
texture_size = texture.size,
|
||||||
position = pos:copy(),
|
position = pos:copy(),
|
||||||
|
rotation = Vector3(),
|
||||||
triangles = {}
|
triangles = {}
|
||||||
}
|
}
|
||||||
obj.position = obj.position:copy()
|
obj.position = obj.position:copy()
|
||||||
@ -112,12 +113,19 @@ function Obj.create(model, texture, position)
|
|||||||
return obj
|
return obj
|
||||||
end
|
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()
|
function Obj:draw()
|
||||||
for _, triangle in ipairs(self.triangles) do
|
for _, triangle in ipairs(self.triangles) do
|
||||||
local newt = util.shallow_copy(triangle)
|
local newt = util.shallow_copy(triangle)
|
||||||
newt.v0 = Vector3(triangle.v0) + self.position
|
newt.v0 = rotate_vertex(self, triangle.v0) + self.position
|
||||||
newt.v1 = Vector3(triangle.v1) + self.position
|
newt.v1 = rotate_vertex(self, triangle.v1) + self.position
|
||||||
newt.v2 = Vector3(triangle.v2) + self.position
|
newt.v2 = rotate_vertex(self, triangle.v2) + self.position
|
||||||
draw_triangle(newt)
|
draw_triangle(newt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,7 +5,7 @@ local List = require "types.list"
|
|||||||
|
|
||||||
local Obj = require "classes.obj"
|
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"
|
local Feed = require "classes.feed"
|
||||||
---@type List
|
---@type List
|
||||||
@ -44,7 +44,7 @@ end
|
|||||||
function game_tick()
|
function game_tick()
|
||||||
-- ctx.initialization_needed is true first frame and every time dynamic reload is performed
|
-- ctx.initialization_needed is true first frame and every time dynamic reload is performed
|
||||||
if ctx.initialization_needed then
|
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)
|
player.ThrowPressed:connect(create_feed)
|
||||||
|
|
||||||
-- spawn some ducks
|
-- spawn some ducks
|
||||||
@ -86,7 +86,9 @@ function game_tick()
|
|||||||
texture_region = { x = 0, y = 0, w = 512, h = 512 },
|
texture_region = { x = 0, y = 0, w = 512, h = 512 },
|
||||||
}
|
}
|
||||||
draw_quad(util.merge(q, params))
|
draw_quad(util.merge(q, params))
|
||||||
cube.position.x = math.sin(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.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()
|
cube:draw()
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user