annotate pytouhou/utils/matrix.py @ 25:cc864aadc733

Preload enemy and background textures
author Thibaut Girka <thib@sitedethib.com>
date Fri, 12 Aug 2011 19:25:08 +0200
parents 787d2eb13c2d
children f405b947624d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 #TODO: find/learn to use a proper lib
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 from math import sin, cos
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 class Matrix(object):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 def __init__(self):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 self.data = [[0] * 4 for i in xrange(4)]
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 def mult(self, other_matrix):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 result = Matrix()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 for i in xrange(4):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 for j in xrange(4):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14 result.data[i][j] = sum(self.data[i][a] * other_matrix.data[a][j] for a in xrange(4))
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 return result
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 def get_translation_matrix(cls, x, y, z):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 matrix = cls()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 matrix.data[0][0] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 matrix.data[1][1] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 matrix.data[2][2] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 matrix.data[3][3] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 matrix.data[0][3] = x
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 matrix.data[1][3] = y
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 matrix.data[2][3] = z
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 return matrix
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 def get_scaling_matrix(cls, x, y, z):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 matrix = cls()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 matrix.data[0][0] = x
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 matrix.data[1][1] = y
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 matrix.data[2][2] = z
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 matrix.data[3][3] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 return matrix
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 def get_rotation_matrix(cls, angle, axis):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 """Only handles axis = x, y or z."""
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 matrix = cls()
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 matrix.data[3][3] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 if axis == 'x':
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 matrix.data[0][0] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 matrix.data[1][1] = cos(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 matrix.data[1][2] = -sin(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 matrix.data[2][1] = sin(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 matrix.data[2][2] = cos(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 elif axis == 'y':
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 matrix.data[0][0] = cos(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 matrix.data[0][2] = sin(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 matrix.data[2][0] = -sin(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 matrix.data[2][2] = cos(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 matrix.data[1][1] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 elif axis == 'z':
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 matrix.data[0][0] = cos(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 matrix.data[0][1] = -sin(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 matrix.data[1][0] = sin(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 matrix.data[1][1] = cos(angle)
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 matrix.data[2][2] = 1
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 else:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 raise Exception
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 return matrix