-
-
Save Ivoah/cd6e2f99c6d2e304b7bc to your computer and use it in GitHub Desktop.
3d stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--3demo | |
--simple 3d demo | |
cube = {{{-1,-1,-1}, --points | |
{-1,-1,1}, | |
{1,-1,1}, | |
{1,-1,-1}, | |
{-1,1,-1}, | |
{-1,1,1}, | |
{1,1,1}, | |
{1,1,-1}, | |
--[[{-0.5,-0.5,-0.5}, --inside | |
{-0.5,-0.5,0.5}, | |
{0.5,-0.5,0.5}, | |
{0.5,-0.5,-0.5}, | |
{-0.5,0.5,-0.5}, | |
{-0.5,0.5,0.5}, | |
{0.5,0.5,0.5}, | |
{0.5,0.5,-0.5}]]}, | |
{{1,2}, --lines | |
{2,3}, | |
{3,4}, | |
{4,1}, | |
{5,6}, | |
{6,7}, | |
{7,8}, | |
{8,5}, | |
{1,5}, | |
{2,6}, | |
{3,7}, | |
{4,8}, | |
--[[{8+1,8+2}, --inside | |
{8+2,8+3}, | |
{8+3,8+4}, | |
{8+4,8+1}, | |
{8+5,8+6}, | |
{8+6,8+7}, | |
{8+7,8+8}, | |
{8+8,8+5}, | |
{8+1,8+5}, | |
{8+2,8+6}, | |
{8+3,8+7}, | |
{8+4,8+8}, | |
{1,9},-- | |
{2,10}, | |
{3,11}, | |
{4,12}, | |
{5,13}, | |
{6,14}, | |
{7,15}, | |
{8,16}]]}} | |
tri = {{{}, | |
{}, | |
{}, | |
{}} | |
{{1,2}, | |
{2,3}, | |
{3,1}, | |
{1,4}, | |
{2,4}, | |
{3,4}}} | |
function _init() | |
cam = {0,0,-2.5} | |
mult = 64 | |
a = flr(rnd(3))+1 | |
t = flr(rnd(50))+25 | |
--s1 = cube | |
--s2 = cube | |
map={{cube,cube,cube}, | |
{cube,nil, cube}, | |
{cube,cube,cube}, | |
{cube,nil, cube}, | |
{cube,cube,cube}} | |
i=0 | |
for m in all(map) do | |
j=0 | |
i += 1 | |
for v in all(m) do | |
j += 1 | |
if v then | |
map[i][j] = translate_shape(v,{3-j*2,5-i*2,0}) | |
end | |
end | |
end | |
end | |
function _update() | |
if btn(0) then cam[1] -= 0.1 end | |
if btn(1) then cam[1] += 0.1 end | |
if btn(2) then cam[2] += 0.1 end | |
if btn(3) then cam[2] -= 0.1 end | |
--if btn(0) then s1=rotate_shape(s1,2,0.01) end | |
--if btn(1) then s1=rotate_shape(s1,2,-0.01) end | |
--if btn(2) then s1=rotate_shape(s1,1,0.01) end | |
--if btn(3) then s1=rotate_shape(s1,1,-0.01) end | |
if btn(4) then cam[3] -= 0.1 end | |
if btn(5) then cam[3] += 0.1 end | |
t -= 1 | |
if t <= 0 then | |
t = flr(rnd(50))+25 | |
a = flr(rnd(3))+1 | |
end | |
i=0 | |
for m in all(map) do | |
j=0 | |
i += 1 | |
for v in all(m) do | |
j += 1 | |
if v then | |
map[i][j] = rotate_shape(v,a,0.01) | |
end | |
end | |
end | |
--s1 = rotate_shape(s1,a,0.01) | |
--s2 = rotate_shape(s2,a,0.03) | |
end | |
function _draw() | |
cls() | |
print("t="..t,0,6*0) | |
print("x="..cam[1],0,6*1) | |
print("y="..cam[2],0,6*2) | |
print("z="..cam[3],0,6*3) | |
--draw_shape(s1) | |
for m in all(map) do | |
for v in all(m) do | |
if v then | |
draw_shape(v) | |
end | |
end | |
end | |
--draw_shape(translate_shape(s2,{0,2,0})) | |
--draw_point({0,0,3},12) | |
end | |
function draw_shape(s,c) | |
for l in all(s[2]) do | |
draw_line(s[1][l[1]], s[1][l[2]], c) | |
end | |
end | |
function draw_line(p1,p2,c) | |
x0, y0 = project(p1) | |
x1, y1 = project(p2) | |
line(x0, y0, x1, y1, c or 11) | |
end | |
function draw_point(p,c) | |
x, y = project(p) | |
pset(x, y, c or 11) | |
end | |
function project(p) | |
x = (p[1]-cam[1])*mult/(p[3]-cam[3]) + 127/2 | |
y = -(p[2]-cam[2])*mult/(p[3]-cam[3]) + 127/2 | |
return x, y | |
end | |
function translate_shape(s,t) | |
ns = {{},s[2]} | |
for p in all(s[1]) do | |
add(ns[1],{p[1]+t[1],p[2]+t[2],p[3]+t[3]}) | |
end | |
return ns | |
end | |
function rotate_shape(s,a,r) | |
ns = {{},s[2]} | |
for p in all(s[1]) do | |
add(ns[1], rotate_point(p,a,r)) | |
end | |
return ns | |
end | |
function rotate_point(p,a,r) | |
if a==1 then | |
x,y,z = 3,2,1 | |
elseif a==2 then | |
x,y,z = 1,3,2 | |
elseif a==3 then | |
x,y,z = 1,2,3 | |
end | |
_x = cos(r)*(p[x]) - sin(r) * (p[y]) | |
_y = sin(r)*(p[x]) + cos(r) * (p[y]) | |
np = {} | |
np[x] = _x | |
np[y] = _y | |
np[z] = p[z] | |
return np | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment