annotate pytouhou/utils/matrix.py @ 41:93c8dc2de923

Add (hopefully) "texture shifting" in animations
author Thibaut Girka <thib@sitedethib.com>
date Mon, 15 Aug 2011 00:41:51 +0200
parents 55973a3f1222
children c435e83a8e70
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):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
6 def __init__(self, data=None):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
7 self.data = data or [[0] * 4 for i in xrange(4)]
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):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
11 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
12 d2 = other_matrix.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
13 return Matrix([[sum(d1[i][a] * d2[a][j] for a in xrange(4)) for j in xrange(4)] for i in xrange(4)])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
14
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
15
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
16 def flip(self):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
17 self.data[0][:] = (-x for x in self.data[0])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
18
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
19
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
20 def scale(self, x, y, z):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
21 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
22 d1[0][:] = (a * x for a in d1[0])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
23 d1[1][:] = (a * y for a in d1[1])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
24 d1[2][:] = (a * z for a in d1[2])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
25
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
26
31
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
27 def scale2d(self, x, y):
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
28 d1 = self.data
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
29 d1[0][:] = (a * x for a in d1[0])
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
30 d1[1][:] = (a * y for a in d1[1])
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
31
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
32
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
33 def translate(self, x, y, z):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
34 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
35 a, b, c = (v * m for v, m in zip(d1[3][:3], (x, y, z)))
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
36 d1[0][:] = (v + a for v in d1[0])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
37 d1[1][:] = (v + b for v in d1[1])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
38 d1[2][:] = (v + c for v in d1[2])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
39
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
40
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
41 def rotate_x(self, angle):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
42 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
43 cos_a = cos(angle)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
44 sin_a = sin(angle)
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 28
diff changeset
45 d1[1][:], d1[2][:] = ([cos_a * d1[1][i] - sin_a * d1[2][i] for i in range(4)],
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 28
diff changeset
46 [sin_a * d1[1][i] + cos_a * d1[2][i] for i in range(4)])
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
47
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
48
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
49 def rotate_y(self, angle):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
50 #TODO: check
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
51 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
52 cos_a = cos(angle)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
53 sin_a = sin(angle)
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 28
diff changeset
54 d1[0][:], d1[2][:] = ([cos_a * d1[0][i] - sin_a * d1[2][i] for i in range(4)],
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 28
diff changeset
55 [sin_a * d1[0][i] + cos_a * d1[2][i] for i in range(4)])
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
56
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
57
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
58 def rotate_z(self, angle):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
59 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
60 cos_a = cos(angle)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
61 sin_a = sin(angle)
30
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 28
diff changeset
62 d1[0][:], d1[1][:] = ([cos_a * d1[0][i] - sin_a * d1[1][i] for i in range(4)],
e3ba2fa966f6 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 28
diff changeset
63 [sin_a * d1[0][i] + cos_a * d1[1][i] for i in range(4)])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 def get_translation_matrix(cls, x, y, z):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
68 return cls([[1., 0., 0., x],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
69 [0., 1., 0., y],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
70 [0., 0., 1., z],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
71 [0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75 def get_scaling_matrix(cls, x, y, z):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
76 return cls([[x, 0., 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
77 [0., y, 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
78 [0., 0., z, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
79 [0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 def get_rotation_matrix(cls, angle, axis):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 """Only handles axis = x, y or z."""
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
85 cos_a = cos(angle)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
86 sin_a = sin(angle)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 if axis == 'x':
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
88 return Matrix([[ 1., 0., 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
89 [ 0., cos_a, -sin_a, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
90 [ 0., sin_a, cos_a, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
91 [ 0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 elif axis == 'y':
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
93 return Matrix([[ cos_a, 0., sin_a, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
94 [ 0., 1., 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
95 [-sin_a, 0., cos_a, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
96 [ 0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97 elif axis == 'z':
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
98 return Matrix([[ cos_a, -sin_a, 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
99 [ sin_a, cos_a, 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
100 [ 0., 0., 1., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
101 [ 0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
102 else:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
103 raise Exception
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
104