Created
December 31, 2014 14:37
-
-
Save dermotbalson/c4f1f90919045e4cf596 to your computer and use it in GitHub Desktop.
3D screen boundaries
This file contains 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
function setup() | |
displayMode(FULLSCREEN) | |
ScreenFraction=0.7 --reduce the screen size artificially so we can see it works | |
perspective() | |
camera(0,0,0,0,0,-1) | |
xFactor=-1/projectionMatrix()[1]*ScreenFraction | |
yFactor=-1/projectionMatrix()[6]*ScreenFraction | |
--set up asteroids | |
asteroids={} | |
for i=1,25 do | |
local a={} | |
local z=math.random(-1000,-900) | |
a.radius=math.random(3,10) | |
a.pos=vec3(math.random(-xFactor*z+a.radius,xFactor*z-a.radius), | |
math.random(-yFactor*z+a.radius,yFactor*z-a.radius), | |
z) | |
a.velocity=vec3(0,0,2) --moving toward us | |
a.colour=color(math.random(0,255),math.random(0,255),math.random(0,255)) | |
table.insert(asteroids,a) | |
end | |
end | |
function draw() | |
background(0) | |
perspective() | |
camera(0,0,0,0,0,-1) | |
fill(255) | |
local counter=0 | |
for i,a in pairs(asteroids) do | |
counter=counter+1 | |
pushMatrix() | |
translate(a.pos.x,a.pos.y,a.pos.z) | |
fill(a.colour) | |
ellipse(0,0,a.radius*2) | |
popMatrix() | |
a.pos=a.pos+a.velocity | |
if math.abs(a.pos.x)-a.radius>a.pos.z*xFactor or math.abs(a.pos.y)-a.radius>a.pos.z*yFactor then | |
table.remove(asteroids,i) | |
end | |
end | |
--all this below is just to draw a rectangle showing the reduced screen boundary | |
ortho() | |
viewMatrix(matrix()) | |
pushStyle() | |
noFill() | |
stroke(255) | |
strokeWidth(2) | |
rectMode(CENTER) | |
rect(WIDTH/2,HEIGHT/2,WIDTH*ScreenFraction,HEIGHT*ScreenFraction) | |
popStyle() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment