Oh, hey. This seems to work...
Create a 3D Object: (Points only right now, triangles later)
Code: Select all
obj3d = {}
obj3d[1] = {
x = 200,
y = 100,
z = 0,
rot = {
xy = 0,
xz = 0,
yz = 0
},
pnt = {
[1] = {-30, -30, -30},
[2] = { 30, -30, -30},
[3] = { 30, 30, -30},
[4] = {-30, 30, -30},
[5] = {-30, -30, 30},
[6] = { 30, -30, 30},
[7] = { 30, 30, 30},
[8] = {-30, 30, 30}
}
}
The rotate 3d point function:
Code: Select all
function rotate_point_3d( cx, cy, cz, ox, oy, oz, angle_xy, angle_xz, angle_yz )
local rot_pnt = { x, y, z }
rot_pnt.x = ox * math.cos(math.rad(-angle_xy)) - oy * math.sin(math.rad(-angle_xy))
rot_pnt.y = ox * math.sin(math.rad(-angle_xy)) + oy * math.cos(math.rad(-angle_xy))
ox = rot_pnt.x
rot_pnt.x = ox * math.cos(math.rad(-angle_xz)) - oz * math.sin(math.rad(-angle_xz))
rot_pnt.z = ox * math.sin(math.rad(-angle_xz)) + oz * math.cos(math.rad(-angle_xz))
oy = rot_pnt.y
oz = rot_pnt.z
rot_pnt.y = oy * math.cos(math.rad(-angle_yz)) - oz * math.sin(math.rad(-angle_yz))
rot_pnt.z = oy * math.sin(math.rad(-angle_yz)) + oz * math.cos(math.rad(-angle_yz))
return rot_pnt.x + cx, rot_pnt.y + cy, rot_pnt.z + cz
end
Rotate and draw the points:
Code: Select all
function draw_object_3d(obj)
local x, y, z = {}, {}, {}
for i, p in pairs(obj3d[obj].pnt) do
obj3d[obj].rot.xy = obj3d[obj].rot.xy + dt * 20
obj3d[obj].rot.xz = obj3d[obj].rot.xz + dt * 20
x[i], y[i], z[i] = rotate_point_3d(
obj3d[obj].x, obj3d[obj].y, obj3d[obj].z,
p[1], p[2], p[3],
obj3d[obj].rot.xy, obj3d[obj].rot.xz, obj3d[obj].rot.yz
)
love.graphics.circle("fill", x[i], y[i], 2)
end
end
Of course this does nothing for sorting and handling large amounts. So I have to do some tweaking and thinking.
Edit: Eh. Experiment was a slight failure. Will try again maybe sometime in the future.