testing 3d-to-2d conversion with a pygame draft script :)
[tennix.git] / doc / tennix-test.py
blob91d6afd43dd8a17c32431c62b7bfffeabf100e20
1 #!/usr/bin/python
3 import pygame
4 from pygame.constants import *
5 from math import cos
6 from math import pi
8 WIDTH = 640
9 HEIGHT = 480
10 H_FACT = 0.10
12 pygame.init()
13 pygame.display.set_mode( (WIDTH,HEIGHT))
14 surface = pygame.display.get_surface()
15 clock = pygame.time.Clock()
17 def convert_3d_2d( threed, h_factor):
18 #x = threed.x * (0.1 * threed.y + 0.9)
19 #y = (threed.y - threed.z*h_factor) * (0.15 * threed.y + 0.85)
20 a = 0.2
21 b = 0.6
22 x = threed.x * (a * threed.y + b)
23 y = (threed.y - threed.z*h_factor) * (a * threed.y + b)
25 return Coord2D( x, y)
28 class Coord3D(object):
29 def __init__( self, x, y, z = 0.0):
30 self.x = x
31 self.y = y
32 self.z = z
34 def bottom( self):
35 return Coord3D( self.x, self.y, 0.0)
37 def twod( self):
38 return convert_3d_2d( self, H_FACT)
40 class Coord2D(object):
41 def __init__( self, x, y):
42 self.x = x
43 self.y = y
46 tennis_area = [
47 ( Coord3D( -1, -1), Coord3D( -1, 1 ) ),
48 ( Coord3D( -1, -1), Coord3D( 1, -1) ),
49 ( Coord3D( -1, 1), Coord3D( 1, 1) ),
50 ( Coord3D( 1, -1), Coord3D( 1, 1) ),
51 ( Coord3D( -1, 0, 1), Coord3D( -1, 0, 0) ),
52 ( Coord3D( -1, 0, 1), Coord3D( 1, 0, 1) ),
53 ( Coord3D( 1, 0, 1), Coord3D( 1, 0, 0) ),
54 ( Coord3D( -1, 0, 0), Coord3D( 1, 0, 0) ),
57 def twod_to_real( twod):
58 x = twod.x * WIDTH/2.1 + WIDTH/2
59 y = twod.y * HEIGHT/2.1 + HEIGHT/2
60 return Coord2D( x, y)
63 hist = []
65 move_y = 0.015
66 move_x = 0.0025
68 x = 0
69 y = 0
70 i = 1
71 while i > 0:
72 clock.tick( 70)
73 evt = pygame.event.poll()
74 i += 1
76 if evt.type == KEYUP and evt.key == K_ESCAPE:
77 break
79 if evt.type == MOUSEMOTION:
80 x = (evt.pos[0]*2.0/WIDTH)-1.0
81 y = (evt.pos[1]*2.0/HEIGHT)-1.0
82 else:
83 x += move_x
84 y += move_y
85 if x > 1.0 or x < -1.0:
86 move_x *= -1
87 if y > 1.0 or y < -1.0:
88 move_y *= -1
90 surface.fill( (0,0,0))
91 for a in tennis_area:
92 twod_a = twod_to_real( a[0].twod())
93 twod_b = twod_to_real( a[1].twod())
94 pygame.draw.aaline( surface, (255,255,255), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
96 currd = Coord3D( x, y, 1.2+1.2*cos( y*2*pi ))
98 if i % 2 == 0:
99 hist.append( currd)
101 if len(hist) > 250:
102 hist = hist[-250:]
104 old = None
105 u = 0
106 for e in hist:
107 if old:
108 twod_a = twod_to_real( convert_3d_2d( old, H_FACT))
109 twod_b = twod_to_real( convert_3d_2d( e, H_FACT))
110 pygame.draw.aaline( surface, (0,u,0), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
111 twod_a = twod_to_real( convert_3d_2d( old.bottom(), H_FACT))
112 twod_b = twod_to_real( convert_3d_2d( e.bottom(), H_FACT))
113 pygame.draw.aaline( surface, (0,0,u), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
114 old = e
115 u += int(255/len(hist))
117 twod_a = twod_to_real( convert_3d_2d( Coord3D( 0.0, 0.0), H_FACT))
118 twod_b = twod_to_real( convert_3d_2d( currd, H_FACT))
119 pygame.draw.aaline( surface, (255,0,0), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
120 twod_a = twod_to_real( convert_3d_2d( Coord3D( 0.0, 0.0), H_FACT))
121 twod_b = twod_to_real( convert_3d_2d( Coord3D( x, y, 0), H_FACT))
122 pygame.draw.aaline( surface, (255,0,0), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
123 twod_a = twod_to_real( convert_3d_2d( currd, H_FACT))
124 twod_b = twod_to_real( convert_3d_2d( Coord3D( x, y, 0), H_FACT))
125 pygame.draw.aaline( surface, (255,0,0), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
127 pygame.display.flip()