Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Use this function to perform your initial setup
- function setup()
- iparameter("VanishPointx",0,WIDTH,WIDTH/2)
- iparameter("VanishPointy",0,WIDTH,WIDTH/2)
- iparameter("VanishingPoint",0,1,0)
- iparameter("Horizon",0,1,0)
- iparameter("Midline",0,1,0)
- iparameter("HitBox",0,100,0)
- iparameter("Sticker",0,1,0)
- iparameter("Mobs",0,1,0)
- iparameter("zSpeed",0,30,0)
- iparameter("Mobtype",0,1,0)
- Mobz = {}
- print("Move the sliders to make something appear")
- end
- -- This function gets called once every frame
- function draw()
- vp = vec2(VanishPointx,VanishPointy) --vanishing point
- background(0,0,0,0)
- stroke(0, 255, 39, 255)
- strokeWidth(3)
- fill(3, 253, 15, 255)
- if HitBox > 0 then
- ShowHitBox(HitBox)
- end
- if Midline == 1 then
- line(vp.x,0,vp.x,HEIGHT)
- end
- if Horizon == 1 then
- line(0,vp.y,WIDTH,vp.y)
- end
- if VanishingPoint == 1 then
- ellipse(vp.x,vp.y,5,5)
- end
- if Mobs == 0 then
- for k, v in pairs(Mobz) do
- table.remove(Mobz,k)
- end
- else
- nMobz = table.maxn(Mobz)
- for i = 1,10-nMobz do
- x=math.random()*WIDTH
- y=math.random()*HEIGHT
- z=math.random()*WIDTH*10
- d=256
- table.insert(Mobz,nMobz+1,{x=x,y=y,z=z,d=d})
- end
- for k, v in pairs(Mobz) do
- zd = l3Dto2d(Mobz[k].d,Mobz[k].z)
- e = c3Dto2d(vec3(Mobz[k].x,Mobz[k].y,Mobz[k].z))
- if Mobtype == 0 then
- ellipse(e.x,e.y,zd)
- else
- sprite("SpaceCute:Planet",e.x,e.y,zd)
- end
- end
- end
- if zSpeed > 0 and table.maxn(Mobz) > 0 then
- for k, v in pairs(Mobz) do
- Mobz[k].z = Mobz[k].z - zSpeed
- if Mobz[k].z < 0 then
- table.remove(Mobz,k)
- end
- end
- end
- if Sticker == 1 then --draw something full size
- --like a sticker on the window
- ellipse(100,100,100)
- --sprite("SpaceCute:Planet",300,300,256)
- end
- end
- function l3Dto2d(d,z)
- if z == 0 then
- --depth is zero, we're on the screen
- return (d)
- elseif z < 0 then
- --depth is negative
- --look out it's behind you!
- --now crash and burn them for passing this to us
- return (nil)
- --maybe after some thought this could be handled
- else --if z > 0
- --depth is positive
- --it's ahead of us (doesn't mean we can see it)
- dz = d * WIDTH / ( WIDTH + z)
- return(dz)
- end -- z depth
- end
- function c3Dto2d(p)
- --print(p)
- --convert 3D to 2D
- --accepts p a vec3 -- x,y,z
- --returns a vec2 --x,y
- -- p.x + (vp.x / 2)/WIDTH/p.z
- -- p.x - ((WIDTH - vp.x) /2)/WIDTH/p.z
- local x2, y2
- if p.z == 0 then
- --depth is zero, we're on the screen
- return (vec2(p.x,p.y))
- elseif p.z < 0 then
- --depth is negative
- --look out it's behind you!
- --now crash and burn them for passing this to us
- return vec2(nil,nil)
- --maybe after some thought this could be handled
- else --if p.z > 0
- --depth is positive
- --it's ahead of us (doesn't mean we can see it)
- x2 = p.x + ((vp.x - p.x)* (p.z/WIDTH) / ((p.z/WIDTH)+1) )
- y2 = p.y + ((vp.y - p.y)* (p.z/WIDTH) / ((p.z/WIDTH)+1) )
- --print(x2,y2)
- return(vec2(x2,y2))
- end -- z depth
- end
- function ShowHitBox(boxes)
- for i = 1,boxes do
- --the hit box is a box in front of the viewer
- --objects in the hit box will hit the screen
- sll = vec3(0,0,(i-1)*WIDTH) -- screen lower left point
- sul = vec3(0,HEIGHT,(i-1)*WIDTH) -- screen upper left point
- slr = vec3(WIDTH,0,(i-1)*WIDTH) -- screen lower right point
- sur = vec3(WIDTH,HEIGHT,(i-1)*WIDTH) -- screen upper right point
- -- depth as in the depth of the box
- -- the box depth is set to it's width
- dll = vec3(0,0,i*WIDTH) -- depth lower left point
- dul = vec3(0,HEIGHT,i*WIDTH) -- depth upper left point
- dlr = vec3(WIDTH,0,i*WIDTH) -- depth screen lower right
- dur = vec3(WIDTH,HEIGHT,i*WIDTH) -- depth screen upper right
- sll2 = c3Dto2d(sll)
- sul2 = c3Dto2d(sul)
- slr2 = c3Dto2d(slr)
- sur2 = c3Dto2d(sur)
- dll2 = c3Dto2d(dll)
- dul2 = c3Dto2d(dul)
- dlr2 = c3Dto2d(dlr)
- dur2 = c3Dto2d(dur)
- DrawBox(sll2,sul2,slr2,sur2,dll2,dul2,dlr2,dur2)
- end
- end
- function DrawBox(sll2,sul2,slr2,sur2,dll2,dul2,dlr2,dur2)
- --back
- line(dll2.x,dll2.y,dul2.x,dul2.y)
- line(dul2.x,dul2.y,dur2.x,dur2.y)
- line(dur2.x,dur2.y,dlr2.x,dlr2.y)
- line(dlr2.x,dlr2.y,dll2.x,dll2.y)
- --sides
- line(sll2.x,sll2.y,dll2.x,dll2.y)
- line(sul2.x,sul2.y,dul2.x,dul2.y)
- line(sur2.x,sur2.y,dur2.x,dur2.y)
- line(slr2.x,slr2.y,dlr2.x,dlr2.y)
- --front
- line(sll2.x,sll2.y,sul2.x,sul2.y)
- line(sul2.x,sul2.y,sur2.x,sur2.y)
- line(sur2.x,sur2.y,slr2.x,slr2.y)
- line(slr2.x,slr2.y,sll2.x,sll2.y)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement