-
Notifications
You must be signed in to change notification settings - Fork 2
/
star.py
64 lines (48 loc) · 2.56 KB
/
star.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Sun Mar 01 18:30:17 PST 2015
#
# Base star class which is the basic unit of composition for the Galaxy. In # the future should pull out the geomatric transformation functions into a
# base class, but this'll work for now.
#
import sys, math, pygame, random
from Vec3d import Vec3d
class star3D:
def __init__(self, v = Vec3d(0,0,0), color = (255,255,255), size=1 ):
self.v = v
self.color = color
self.size = size
def rotateX(self, angle):
""" Rotate the star around the X axis by the given angle in degrees. """
return star3D(self.v.rotated_around_x(angle), self.color, self.size)
def rotateXaroundViewer(self, viewer):
v = self.v - viewer.position
return star3D(v.rotated_around_x(viewer.direction.get_angle_around_x()), self.color,self.size)
def rotateYaroundViewer(self, viewer):
v = self.v - viewer.position
return star3D(v.rotated_around_y(viewer.direction.get_angle_around_y()), self.color,self.size)
def rotateZaroundViewer(self, viewer):
v = self.v - viewer.position
return star3D(v.rotated_around_y(viewer.direction.get_angle_around_z()), self.color,self.size)
def rotateY(self, angle):
""" Rotate the star around the Y axis by the given angle in degrees. """
return star3D(self.v.rotated_around_y(angle), self.color, self.size)
def rotateZ(self, angle):
""" Rotate the star around the Z axis by the given angle in degrees. """
return star3D(self.v.rotated_around_z(angle), self.color, self.size)
def project(self, win_width, win_height, galaxy_pos, fov, viewer):
""" Transforms this 3D point to 2D using a perspective projection. """
# The following chunc of code determines whether the star is
# outside of the viewers scope by simplistically determining if
# it is behind the viewer, in which case, it doesn't project the
# star
# Note the the viewer is viewer_d along the z axes and looking
# back (0,0,-1) therefore we use S+V to find the distance
dist = self.v - viewer.position # distance from star to viewer
if dist.z > 0:
return None
if ((viewer.position.z - self.v.z) + galaxy_pos.z) == 0:
factor = 0
else:
factor = fov / ((viewer.position.z - self.v.z) + galaxy_pos.z)
x = (self.v.x+galaxy_pos.x+viewer.position.x) * factor + win_width / 2
y = -(self.v.y+galaxy_pos.y+viewer.position.y) * factor + win_height / 2
return star3D(Vec3d(x, y, 0), self.color, self.size)