3D rotation
Posted: Wed Apr 27, 2016 5:20 pm
Hey there!
I'm trying to get a basic rotation working. I have a 3D cube and I want to rotate it around the z axis of the cubes middle point. However it seems to rotate around the the origin point of the canvas. Got a lil frustrated so I'm asking for help. Whats wrong with these formulas?
Just copy everything and paste it to a .lua file and run it with love.
The code I need some help with is all located in the rotateZ3D function located at the bottom.
Thank you!
I'm trying to get a basic rotation working. I have a 3D cube and I want to rotate it around the z axis of the cubes middle point. However it seems to rotate around the the origin point of the canvas. Got a lil frustrated so I'm asking for help. Whats wrong with these formulas?
Code: Select all
node0 = { 100, 200, 100}
node1 = { 100, 200, 200}
node2 = { 100, 100, 100}
node3 = { 100, 100, 200}
node4 = { 200, 200, 100}
node5 = { 200, 200, 200}
node6 = { 200, 100, 100}
node7 = { 200, 100, 200}
nodes = {node0, node1, node2, node3, node4, node5, node6, node7}
edge0 = {0, 1}
edge1 = {1, 3}
edge2 = {3, 2}
edge3 = {2, 0}
edge4 = {4, 5}
edge5 = {5, 7}
edge6 = {7, 6}
edge7 = {6, 4}
edge8 = {0, 4}
edge9 = {1, 5}
edge10 = {2, 6}
edge11 = {3, 7}
edges = {edge0, edge1, edge2, edge3, edge4, edge5, edge6, edge7, edge8, edge9, edge10, edge11}
function love.load()
end
function love.update(dt)
if love.keyboard.isDown('1') then
rotateZ3D(1)
end
end
function love.draw()
for i = 1, 8, 1 do
node = nodes[i]
love.graphics.points(node[1], node[2])
end
for n = 1, 12, 1 do
n0 = edges[n][1]
n1 = edges[n][2]
node00 = nodes[n0+1]
node11 = nodes[n1+1]
love.graphics.line(node00[1], node00[2], node11[1], node11[2])
print(n0)
end
end
function rotateZ3D(alpha)
sin = math.sin(alpha)
cos = math.cos(alpha)
for a = 1, 8, 1 do
node = nodes[a]
x = node[1]
y = node[2]
node[1] = (x * cos - y * sin)
node[2] = (y * cos + x * sin)
end
end
The code I need some help with is all located in the rotateZ3D function located at the bottom.
Thank you!