3D rotation

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

3D rotation

Post by adge »

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?

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



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!
cval
Citizen
Posts: 58
Joined: Sun Apr 20, 2014 2:15 pm
Location: Ukraine

Re: 3D rotation

Post by cval »

I think in your transformation code you can move your cube to origin (0,0,0), then rotate it around desired axis and then move back to the point where it was.
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: 3D rotation

Post by adge »

Well this sound ugly but it works! Thanks man!
User avatar
Guard13007
Party member
Posts: 132
Joined: Sat Oct 25, 2014 3:42 am
Location: Internet, USA
Contact:

Re: 3D rotation

Post by Guard13007 »

adge wrote:Well this sound ugly but it works! Thanks man!
I could be wrong, but you do the exact same thing in 2D. I never found a solution that didn't involve moving to the origin first.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 44 guests