comparison engine/python/fife/extensions/fife_math.py @ 492:16ceb3228324

Moved the SoundManager and the 2D math function (helpers) from the shooter demo to the fife extensions. Fixed fife_timer as it didn't work in the first place. Modified the SoundManager to use the now working fife_timer and removed the Timer class from the shooter demo entirely
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 07 May 2010 21:07:27 +0000
parents
children e241d7553496
comparison
equal deleted inserted replaced
491:c4168eb47a44 492:16ceb3228324
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2010 by the FIFE team
5 # http://www.fifengine.net
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 from fife import fife
25 import math
26
27 """
28 Math Library
29 ==================================
30
31 This is a collection of useful 2D math functions/classes.
32 """
33
34
35 def normalize(vector):
36 """
37 Helper function to normalize a 2D vector
38
39 @param vector a L{fife.DoublePoint} to be normalized
40
41 @return A normalized L{fife.DoublePoint}
42 """
43 norm = fife.DoublePoint(0,0)
44
45 invLength = 1.0/vector.length()
46 if invLength > 1e-06:
47 norm.x = vector.x * invLength;
48 norm.y = vector.y * invLength;
49 else:
50 norm.x = 0
51 norm.y = 0
52
53 return norm
54
55 class Rect(object):
56 """
57 Rect
58
59 A class used to specify the bounding box of objects. For use
60 with collision detection. This was written in python because
61 FIFE does not provide a Rect class that can use floating point
62 values.
63 """
64 def __init__(self, x = 0, y = 0, w = 0, h = 0):
65 """
66 @param x The x coordinate
67 @param y The y coordinate
68 @param w The width
69 @param h The height
70 """
71 self._x = x
72 self._y = y
73 self._w = w
74 self._h = h
75
76 def intersects(self, rect):
77 """
78 Tests for intersection of rect.
79
80 @param rect the Rect to perform the test against
81 @return True if the rectancles intersect, False if not.
82 """
83 _x = self._x - rect.x;
84 _y = self._y - rect.y;
85 _w = self._w;
86 _h = self._h;
87
88 if _x < 0:
89 _w += _x
90 _x = 0
91
92 if _y < 0:
93 _h += _y
94 _y = 0
95
96 if _x + _w > rect.w:
97 _w = rect.w - _x
98
99 if _y + _h > rect.h:
100 _h = rect.h - _y
101
102 if _w <= 0 or _h <= 0:
103 return False
104
105 return True
106
107 def _setX(self, x):
108 self._x = x
109
110 def _getX(self):
111 return self._x
112
113 def _setY(self, y):
114 self._y = y
115
116 def _getY(self):
117 return self._y
118
119 def _setW(self, w):
120 self._w = w
121
122 def _getW(self):
123 return self._w
124
125 def _setH(self, h):
126 self._h = h
127
128 def _getH(self):
129 return self._h
130
131 x = property(_getX, _setX)
132 y = property(_getY, _setY)
133 w = property(_getW, _setW)
134 h = property(_getH, _setH)
135
136 def rotatePoint(origin, point, angle):
137 """
138 Rotates a point around the specified origin.
139
140 @param origin A L{fife.DoublePoint} specifying the origin.
141 @param point A L{fife.DoublePoint} to be rotated.
142 @param angle The angle in which to rotate the point.
143
144 @return A L{fife.DoublePoint} representing the rotated point.
145 """
146 newp = fife.DoublePoint(0,0)
147
148 theta = (angle * math.pi)/180
149
150 costheta = math.cos(theta)
151 sintheta = math.sin(theta)
152
153 x = point.x - origin.x
154 y = point.y - origin.y
155
156 newp.x = costheta * x - sintheta * y
157 newp.y = sintheta * x + costheta * y
158
159 return newp
160
161 __all__ = ['normalize','Rect','rotatePoint']