Created
July 25, 2012 03:12
-
-
Save vgel/3174158 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
from math import * | |
import random, time, pygame | |
x=0 | |
y=1 | |
z=2 | |
def t3d_2d(a,c,t,e): | |
dx = cos(t[y]) * (sin(t[z]) * (a[y] - c[y]) + cos(t[z]) * (a[x] - c[x])) - sin(t[y]) * (a[z] - c[z]) | |
dy = sin(t[z]) * (cos(t[y]) * (a[z] - c[z]) + sin(t[y]) * (sin(t[z]) * (a[y] - c[y]) + cos(t[z]) * (a[x] - c[x]))) + cos(t[x]) * (cos(t[z]) * (a[y] - c[y]) - sin(t[z]) * (a[x] - c[x])) | |
dz = cos(t[x]) * (cos(t[y]) * (a[z] - c[z]) + sin(t[y]) * (sin(t[z]) * (a[y] - c[y]) + cos(t[z]) * (a[x] - c[x]))) - sin(t[x]) * (cos(t[z]) * (a[y] - c[y]) - sin(t[z]) * (a[x] - c[x])) | |
f=(e[z]/dz) | |
bx = (dx - e[x])/f | |
by = (dy - e[y])/f | |
return (bx,by) | |
def dist(a, b): | |
return sqrt((a[x] - b[x])**2 + (a[y] - b[y])**2 + (a[z]-b[z])**2) | |
def loop(dis,z,c,t,e): | |
pygame.draw.rect(dis, (0,0,0), pygame.Rect(0, 0, dis.get_width(), dis.get_height())) | |
for x in z: | |
m=t3d_2d(x,c,t,e) | |
pygame.draw.rect(dis, x[3], pygame.Rect(m[0] + dis.get_width() / 2, m[1] + dis.get_height() / 2, dist(c,x)/10,dist(c,x)/10)) | |
pygame.display.flip() | |
def keys(current, ai, ad, bi, bd, ci, cd): | |
new = current | |
if pygame.key.get_pressed()[ai]: | |
new = (current[0], current[1] + 1, current[2]) | |
if pygame.key.get_pressed()[ad]: | |
new = (current[0], current[1] - 1, current[2]) | |
if pygame.key.get_pressed()[bi]: | |
new = (current[0] - 1, current[1], current[2]) | |
if pygame.key.get_pressed()[bd]: | |
new = (current[0] + 1, current[1], current[2]) | |
if pygame.key.get_pressed()[ci]: | |
new = (current[0], current[1], current[2] - 1) | |
if pygame.key.get_pressed()[cd]: | |
new = (current[0], current[1], current[2] + 1) | |
return new | |
def run(): | |
tick=1 | |
l = [] | |
for i in xrange(100): | |
l.append((float(random.randint(-20,20)),float(random.randint(-20,20)),float(random.randint(-20,20)), (random.randint(0,255),random.randint(0,255),random.randint(0,255)))) | |
pygame.init() | |
d = pygame.display.set_mode((100,100)) | |
camera = (.1,.1,.1) | |
theta = (.0,.0,.0) | |
viewer = (.1,.1,30.1) | |
while 1: | |
pygame.event.pump() | |
camera = keys(camera, pygame.K_d, pygame.K_a, pygame.K_w, pygame.K_s, pygame.K_q, pygame.K_e) | |
theta = keys(theta, pygame.K_h, pygame.K_f, pygame.K_t, pygame.K_g, pygame.K_r, pygame.K_y) | |
viewer = keys(viewer, pygame.K_l, pygame.K_j, pygame.K_i, pygame.K_k, pygame.K_u, pygame.K_o) | |
print camera,theta,viewer | |
loop(d, l, camera, theta, viewer) | |
tick+=1 | |
time.sleep((1000.0/10)/1000) | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment