Created
October 19, 2016 14:27
-
-
Save KeyMaster-/6f576ce9e7169dde963813c3e090ce5d to your computer and use it in GitHub Desktop.
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
import luxe.GameConfig; | |
import luxe.Input; | |
import luxe.Vector; | |
class Main extends luxe.Game { | |
//--- The actual important stuff -- | |
var verts:Array<Vector>; //Holds the '3D model' vertices, i.e. actual 3D coordinates | |
//The setup function, this runs once at the start of the game | |
override function ready() { | |
verts = []; | |
var phi = (1 + Math.sqrt(5)) / 2; | |
var face_angle = 2 * Math.atan(phi) + Math.PI; | |
var pentagon_height = Math.sqrt(5 + 2 * Math.sqrt(5)) / 2; | |
var lr_height = Math.sin(face_angle) * pentagon_height; | |
var dodec_half_height = (phi * phi * phi) / (2 * (Math.sqrt(phi * phi + 1))); | |
var bottom_radius = Math.sqrt(50 + 10 * Math.sqrt(5)) / 10; | |
var radius_add = Math.cos(face_angle) * pentagon_height; | |
make_pentagon(bottom_radius, dodec_half_height); | |
make_pentagon(bottom_radius + radius_add, -dodec_half_height - lr_height); | |
make_pentagon(bottom_radius + radius_add, dodec_half_height + lr_height, Math.PI / 5); | |
make_pentagon(bottom_radius, -dodec_half_height, Math.PI / 5); | |
} //ready | |
function make_pentagon(radius:Float, y:Float, angle_offset:Float = 0) { | |
for(i in 0...5) { | |
var angle = i * Math.PI * 2 / 5; | |
angle += angle_offset; | |
var x = Math.cos(angle) * radius; | |
var z = Math.sin(angle) * radius; | |
verts.push(new Vector(x, y, z)); | |
} | |
} | |
//Our accumulated running time, for the rotation | |
var time:Float = 0; | |
override function update(delta:Float) { | |
time += delta; | |
var transformed = []; //Our vertices, transformed to screen space | |
for(i in 0...verts.length) { | |
var transformed_vert = verts[i].clone(); | |
transformed_vert.multiplyScalar(0.7); | |
var angle = time * 0.25 * Math.PI; | |
var temp_x = transformed_vert.x; | |
//The 2D rotation matrix around the y-axis (i.e. along the x-z plane) | |
transformed_vert.x = Math.cos(angle) * transformed_vert.x - Math.sin(angle) * transformed_vert.z; | |
transformed_vert.z = Math.sin(angle) * temp_x + Math.cos(angle) * transformed_vert.z; | |
transformed_vert.z += 2; //Push the whole model away from our "camera" at 0,0, into the screen | |
//This is the perspective projection. | |
//Objects far away have a higher z coordinate, and thus get scaled towards the center of the projection (right now, that's the 0,0 point) | |
transformed_vert.x /= transformed_vert.z; | |
transformed_vert.y /= transformed_vert.z; | |
// transformed_vert.y *= 9; | |
transformed_vert.multiplyScalar(Luxe.screen.w / 2); //Scale up the resulting projected position to be actually visible on screen. This value is kinda arbitrary | |
transformed_vert.add(Luxe.screen.mid); //Currently, our model is centered around 0,0. This moves it to the center of the screen. | |
transformed_vert.z = 0; //This is in screenspace, so there should be no z. (Necessary for my rendering, if you just pass x/y directly z doesn't matter) | |
transformed.push(transformed_vert); | |
} | |
// for(i in 0...transformed.length) { | |
// for(j in i+1...transformed.length) { | |
// draw_line(transformed[i], transformed[j]); | |
// } | |
// } | |
for(i in 0...5) { | |
draw_line(transformed[i], transformed[(i + 1) % 5]); | |
draw_line(transformed[i], transformed[5 + i]); | |
draw_line(transformed[5 + i], transformed[10 + i]); | |
draw_line(transformed[5 + i], transformed[10 + (i - 1 + 5) % 5]); | |
draw_line(transformed[15 + i], transformed[15 + (i + 1) % 5]); | |
draw_line(transformed[15 + i], transformed[10 + i]); | |
// draw_line(transformed[5 + i], transformed[5 + (i + 1) % 5]); | |
// draw_line(transformed[10 + i], transformed[10 + (i + 1) % 5]); | |
} | |
} //update | |
function draw_line(p0:Vector, p1:Vector) { | |
Luxe.draw.line({ | |
p0:p0, | |
p1:p1, | |
immediate:true | |
}); | |
} | |
//--- Luxe specific setup. --- | |
override function config(config:GameConfig) { | |
config.window.title = 'luxe game'; | |
config.window.width = 640; | |
config.window.height = 640; | |
config.window.fullscreen = false; | |
return config; | |
} //config | |
override function onkeyup(event:KeyEvent) { | |
if(event.keycode == Key.escape) { | |
Luxe.shutdown(); | |
} | |
} //onkeyup | |
} //Main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment