3D Rotating GUI code problem..
Posted: Sun Jul 24, 2011 2:28 pm
Hey people, I got a problem with the 'quad' shape drawing function..
I took the working Java version from here: http://www.dreamincode.net/code/snippet319.htm
I tried the Java code in the Eclipse Java engine, it works fine there. It's basically a 2D shape with 4 vertices rotating so it looks 3D.
Now I was converting the Java code into Lua and make it work with Löve, but I'm having problems..
workspace is a table like:
Now the problem is I have no clue how to use math.cos and math.sin, so it's glitching badly at the moment.
The shape's vertices fly on the screen in all directions, and the shape shrinks until all the vertices are on a pile!
So, it's a mess as you can see.
Is there any way to fix this?
Thanks for reading.
I took the working Java version from here: http://www.dreamincode.net/code/snippet319.htm
I tried the Java code in the Eclipse Java engine, it works fine there. It's basically a 2D shape with 4 vertices rotating so it looks 3D.
Now I was converting the Java code into Lua and make it work with Löve, but I'm having problems..
workspace is a table like:
Code: Select all
workspace={Size={x,y},Position={x,y,z},Vertices={v1,v2,v3,v4},Angle={x,y}} -- Etc.
The shape's vertices fly on the screen in all directions, and the shape shrinks until all the vertices are on a pile!
So, it's a mess as you can see.
Is there any way to fix this?
Thanks for reading.
Code: Select all
for i=1,#workspace do
x_verts = {workspace[i].Vertices.v1.x,workspace[i].Vertices.v2.x,workspace[i].Vertices.v4.x,workspace[i].Vertices.v3.x}; -- Four vertices for a cube
y_verts = {workspace[i].Vertices.v1.y,workspace[i].Vertices.v2.y,workspace[i].Vertices.v4.y,workspace[i].Vertices.v3.y};
shape_save = {} --{-48,48,48,-48,48,48,-48,-48}; -- Original shape example
rotation_pos_x = workspace[i].Angle.x;
rotation_pos_y = workspace[i].Angle.y;
chroma_pos = 64;
xpos,ypos=workspace[i].Position.x,workspace[i].Position.y
-- Reset to original shape
for index=1,4 do
shape_save[index] = x_verts[index]; -- Copy X's
shape_save[index+4] = y_verts[index]; -- Copy Y's
end
-- Rotate all the points like so...
temp_x,temp_y=0,0
for index=1,4 do
-- Rotate the square normally over the y axis in the xyz plane
temp_x = x_verts[index]*math.cos(rotation_pos_x)-y_verts[index]*math.sin(rotation_pos_y);
temp_y = x_verts[index]*math.sin(rotation_pos_x)+y_verts[index]*math.cos(rotation_pos_y);
x_verts[index] = temp_x;
y_verts[index] = temp_y;
-- And the square rotates to look 3D 2x along the x axis in the xyz plane
y_verts[index] = y_verts[index]*math.sin(rotation_pos_y/2);
end
-- Add a degree of rotation
rotation_pos_x = rotation_pos_x+0.0001;
rotation_pos_y = rotation_pos_y+0.0001;
chroma_pos=chroma_pos+1
if (chroma_pos > 255) then
chroma_pos = 64; -- Reset to flash color
tempcolor = Color(0,0,chroma_pos);
--[[set cube's/background color here]]
end
for index=1,4 do
if (index < 3) then
love.graphics.line(x_verts[index]+xpos,y_verts[index]+ypos,x_verts[index+1]+xpos,y_verts[index+1]+ypos);
else
love.graphics.line(x_verts[index]+xpos,y_verts[index]+ypos,x_verts[1]+xpos,y_verts[1]+ypos);
end
end
workspace[i].Vertices.v1.x,workspace[i].Vertices.v2.x,workspace[i].Vertices.v4.x,workspace[i].Vertices.v3.x=x_verts[1],x_verts[2],x_verts[3],x_verts[4]
workspace[i].Vertices.v1.y,workspace[i].Vertices.v2.y,workspace[i].Vertices.v4.y,workspace[i].Vertices.v3.y=y_verts[1],y_verts[2],y_verts[3],y_verts[4]
workspace[i].Angle.x=rotation_pos_x
workspace[i].Angle.y=rotation_pos_y
love.graphics.quad("line", workspace[i].Vertices.v1.x,workspace[i].Vertices.v1.y,workspace[i].Vertices.v2.x,workspace[i].Vertices.v2.y,workspace[i].Vertices.v3.x,workspace[i].Vertices.v3.y,workspace[i].Vertices.v4.x,workspace[i].Vertices.v4.y)
end