changeset 645:291ba2946c73

* Added the ability to normalize a 2D and 3D point. * You can now rotate a 2D point around the origin or a specific point. * Modified the shooter demo to use the new point functionality. * Removed the fife_math extension.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 12 Oct 2010 18:58:47 +0000
parents b84dbc4665b0
children 07b1cf8e92b5
files demos/shooter/scripts/common/baseobject.py demos/shooter/scripts/ships/player.py demos/shooter/scripts/weapons.py engine/core/util/structures/point.h engine/core/util/structures/utilstructures.i engine/python/fife/extensions/__init__.py engine/python/fife/extensions/fife_math.py
diffstat 7 files changed, 128 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/scripts/common/baseobject.py	Mon Oct 11 22:20:00 2010 +0000
+++ b/demos/shooter/scripts/common/baseobject.py	Tue Oct 12 18:58:47 2010 +0000
@@ -22,7 +22,6 @@
 # ####################################################################
 
 from fife import fife
-from fife.extensions.fife_math import normalize
 from fife.fife import FloatRect as Rect
 
 
@@ -126,7 +125,8 @@
 		self._velocity.y += (vector.y * (self._scene.timedelta/1000.0))/self._yscale
 		
 		if self._velocity.length() > self._maxvelocity:
-			norm = normalize(self._velocity)
+			norm = fife.DoublePoint(self._velocity)
+			norm.normalize()
 			self._velocity.x = norm.x * self._maxvelocity
 			self._velocity.y = norm.y * self._maxvelocity
 		
@@ -144,7 +144,9 @@
 			return
 		
 		#first normalize to get a unit vector of the direction we are traveling
-		norm = normalize(self._velocity)
+		norm = fife.DoublePoint(self._velocity)
+		norm.normalize()
+		
 		if norm.length() == 0:
 			self._velocity.x = 0
 			self._velocity.y = 0
--- a/demos/shooter/scripts/ships/player.py	Mon Oct 11 22:20:00 2010 +0000
+++ b/demos/shooter/scripts/ships/player.py	Tue Oct 12 18:58:47 2010 +0000
@@ -24,7 +24,6 @@
 from fife import fife
 from scripts.common.baseobject import *
 from scripts.ships.shipbase import *
-from fife.extensions.fife_math import *
 from scripts.weapons import *
 
 
--- a/demos/shooter/scripts/weapons.py	Mon Oct 11 22:20:00 2010 +0000
+++ b/demos/shooter/scripts/weapons.py	Tue Oct 12 18:58:47 2010 +0000
@@ -23,7 +23,6 @@
 
 from fife import fife
 from scripts.common.baseobject import *
-from fife.extensions.fife_math import normalize, rotatePoint
 
 class Projectile(SpaceObject):
 	"""
@@ -171,7 +170,8 @@
 
 		
 	def fire(self, direction):
-		velocity = normalize(direction)
+		velocity = fife.DoublePoint(direction)
+		velocity.normalize()
 		velocity.x = velocity.x * self._projectileVelocity
 		velocity.y = velocity.y * self._projectileVelocity
 	
@@ -193,7 +193,8 @@
 		self._soundclip = scene.soundmanager.createSoundEmitter("sounds/fireball.ogg")		
 
 	def fire(self, direction):
-		velocity = normalize(direction)
+		velocity = fife.DoublePoint(direction)
+		velocity.normalize()
 		velocity.x = velocity.x * self._projectileVelocity
 		velocity.y = velocity.y * self._projectileVelocity
 	
@@ -220,7 +221,8 @@
 
 		
 	def fire(self, direction):
-		velocity = normalize(direction)
+		velocity = fife.DoublePoint(direction)
+		velocity.normalize()
 		velocity.x = velocity.x * self._projectileVelocity
 		velocity.y = velocity.y * self._projectileVelocity
 	
@@ -252,19 +254,28 @@
 	def fire(self, direction):
 	
 		if (self._scene.time - self._lastfired) > self._firerate:
-			velocity = normalize(direction)
+			velocity = fife.DoublePoint(direction)
+			velocity.normalize()
 			velocity.x = velocity.x * self._projectileVelocity
 			velocity.y = velocity.y * self._projectileVelocity
 
 			origin = fife.DoublePoint(0,0)
+		
+			p1 = fife.DoublePoint(velocity)
+			p2 = fife.DoublePoint(velocity)
+			p3 = fife.DoublePoint(velocity)
+			p4 = fife.DoublePoint(velocity)
+			p5 = fife.DoublePoint(velocity)
+			p6 = fife.DoublePoint(velocity)
+			p7 = fife.DoublePoint(velocity)
 			
-			p1 = rotatePoint(origin, velocity, -30)
-			p2 = rotatePoint(origin, velocity, -20)
-			p3 = rotatePoint(origin, velocity, -10)
-			p4 = rotatePoint(origin, velocity, 0)
-			p5 = rotatePoint(origin, velocity, 10)
-			p6 = rotatePoint(origin, velocity, 20)
-			p7 = rotatePoint(origin, velocity, 30)
+			p1.rotate(origin, -30)
+			p2.rotate(origin, -20)
+			p3.rotate(origin, -10)
+			p4.rotate(origin, 0)
+			p5.rotate(origin, 10)
+			p6.rotate(origin, 20)
+			p7.rotate(origin, 30)
 			
 			pjctl1 = Projectile(self._scene, self._ship, "fireball", 6000 )
 			pjctl1.run(p1, self._ship.location)
@@ -309,17 +320,24 @@
 	def fire(self, direction):
 	
 		if (self._scene.time - self._lastfired) > self._firerate:
-			velocity = normalize(direction)
+			velocity = fife.DoublePoint(direction)
+			velocity.normalize()
 			velocity.x = velocity.x * self._projectileVelocity
 			velocity.y = velocity.y * self._projectileVelocity
 
 			origin = fife.DoublePoint(0,0)
 			
-			p2 = rotatePoint(origin, velocity, -10)
-			p3 = rotatePoint(origin, velocity, -5)
-			p4 = rotatePoint(origin, velocity, 0)
-			p5 = rotatePoint(origin, velocity, 5)
-			p6 = rotatePoint(origin, velocity, 10)
+			p2 = fife.DoublePoint(velocity)
+			p3 = fife.DoublePoint(velocity)
+			p4 = fife.DoublePoint(velocity)
+			p5 = fife.DoublePoint(velocity)
+			p6 = fife.DoublePoint(velocity)
+			
+			p2.rotate(origin, -10)
+			p3.rotate(origin, -5)
+			p4.rotate(origin, 0)
+			p5.rotate(origin, 5)
+			p6.rotate(origin, 10)
 			
 			pjctl2 = Projectile(self._scene, self._ship, "bullet1", 3000 )
 			pjctl2.run(p2, self._ship.location)
--- a/engine/core/util/structures/point.h	Mon Oct 11 22:20:00 2010 +0000
+++ b/engine/core/util/structures/point.h	Tue Oct 12 18:58:47 2010 +0000
@@ -60,6 +60,11 @@
 		explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) {
 		}
 
+		/** Copy Constructor
+		 */
+		PointType2D(const PointType2D<T>& rhs): x(rhs.x), y(rhs.y) {
+		}
+
 		/** Vector addition
 		 */
 		PointType2D<T> operator+(const PointType2D<T>& p) const {
@@ -120,7 +125,53 @@
 			return static_cast<T>(sqrt(sq));
 		}
 
-		inline T& operator[] (int ind) { 
+		/** Normalizes the point
+		 */
+		void normalize() {
+			T invLength = 1.0/length();
+
+			//TODO: get rid of this static cast
+			if (invLength > static_cast<T>(DBL_ZERO_TOLERANCE)) {
+				x = x * invLength;
+				y = y * invLength;
+			}
+			else {
+				x = 0;
+				y = 0;
+			}
+		}
+
+		/** Rotates the point around the origin
+		 */
+		void rotate(T angle){
+			//TODO: get rid of this static cast
+			T theta = (angle * static_cast<T>(DBL_PI))/180;
+			T costheta = cos(theta);
+			T sintheta = sin(theta);
+
+			T nx = x;
+			T ny = y;
+
+			x = costheta * nx - sintheta * ny;
+			y = sintheta * nx + costheta * ny;
+		}
+
+		/** Rotates the point around an origin
+		 */
+		void rotate(const PointType2D<T>& origin, T angle){
+			//TODO: get rid of this static cast
+			T theta = (angle * static_cast<T>(DBL_PI))/180;
+			T costheta = cos(theta);
+			T sintheta = sin(theta);
+
+			T nx = x - origin.x;
+			T ny = y - origin.y;
+
+			x = costheta * nx - sintheta * ny;
+			y = sintheta * nx + costheta * ny;
+		}
+
+		inline T& operator[] (int ind) {
 			assert(ind > -1 && ind < 2);
 			return val[ind];
 		}
@@ -157,6 +208,11 @@
 		explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) {
 		}
 
+		/** Copy Constructor
+		 */
+		PointType3D(const PointType3D<T>& rhs): x(rhs.x), y(rhs.y), z(rhs.z) {
+		}
+
 		/** Vector addition
 		 */
 		PointType3D<T> operator+(const PointType3D<T>& p) const {
@@ -219,9 +275,27 @@
 			return static_cast<T>(sqrt(sq));
 		}
 
-		inline T& operator[] (int ind) { 
-			assert(ind > -1 && ind < 3); 
-			return val[ind]; 
+		/** Normalizes the point
+		 */
+		void normalize() {
+			T invLength = 1.0/length();
+
+			//TODO: get rid of this static cast
+			if (invLength > static_cast<T>(DBL_ZERO_TOLERANCE)) {
+				x = x * invLength;
+				y = y * invLength;
+				z = z * invLength;
+			}
+			else {
+				x = 0;
+				y = 0;
+				z = 0;
+			}
+		}
+
+		inline T& operator[] (int ind) {
+			assert(ind > -1 && ind < 3);
+			return val[ind];
 		}
 	};
 
--- a/engine/core/util/structures/utilstructures.i	Mon Oct 11 22:20:00 2010 +0000
+++ b/engine/core/util/structures/utilstructures.i	Tue Oct 12 18:58:47 2010 +0000
@@ -1,6 +1,6 @@
 /***************************************************************************
- *   Copyright (C) 2005-2008 by the FIFE team                              *
- *   http://www.fifengine.de                                               *
+ *   Copyright (C) 2005-2010 by the FIFE team                              *
+ *   http://www.fifengine.net                                              *
  *   This file is part of FIFE.                                            *
  *                                                                         *
  *   FIFE is free software; you can redistribute it and/or                 *
@@ -31,6 +31,7 @@
 		T x;
 		T y;
 		explicit PointType2D(T _x = 0, T _y = 0);
+		PointType2D(const PointType2D<T>& rhs);
 		PointType2D<T> operator+(const PointType2D<T>& p) const;
 		PointType2D<T> operator-(const PointType2D<T>& p) const;
 		PointType2D<T>& operator+=(const PointType2D<T>& p);
@@ -40,6 +41,9 @@
 		bool operator==(const PointType2D<T>& p) const;
 		bool operator!=(const PointType2D<T>& p) const;
 		T length() const;
+		void normalize();
+		void rotate(T angle);
+		void rotate(const PointType2D<T>& origin, T angle);
 	};
 
 	template<typename T>
@@ -60,6 +64,7 @@
 		T y;
 		T z;
 		explicit PointType3D(T _x = 0, T _y = 0, T _z = 0);
+		PointType3D(const PointType3D<T>& rhs);
 		PointType3D<T> operator+(const PointType3D<T>& p) const;
 		PointType3D<T> operator-(const PointType3D<T>& p) const;
 		PointType3D<T>& operator+=(const PointType3D<T>& p);
@@ -69,6 +74,7 @@
 		bool operator==(const PointType3D<T>& p) const;
 		bool operator!=(const PointType3D<T>& p) const;
 		T length() const;
+		void normalize();
 	};
 
 	template<typename T>
--- a/engine/python/fife/extensions/__init__.py	Mon Oct 11 22:20:00 2010 +0000
+++ b/engine/python/fife/extensions/__init__.py	Tue Oct 12 18:58:47 2010 +0000
@@ -11,6 +11,5 @@
 	'pythonize',
 	'savers',
 	'loaders',
-	'soundmanager',
-	'fife_math'
+	'soundmanager'
 ]
--- a/engine/python/fife/extensions/fife_math.py	Mon Oct 11 22:20:00 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,84 +0,0 @@
-# -*- coding: utf-8 -*-
-
-# ####################################################################
-#  Copyright (C) 2005-2010 by the FIFE team
-#  http://www.fifengine.net
-#  This file is part of FIFE.
-#
-#  FIFE is free software; you can redistribute it and/or
-#  modify it under the terms of the GNU Lesser General Public
-#  License as published by the Free Software Foundation; either
-#  version 2.1 of the License, or (at your option) any later version.
-#
-#  This library is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#  Lesser General Public License for more details.
-#
-#  You should have received a copy of the GNU Lesser General Public
-#  License along with this library; if not, write to the
-#  Free Software Foundation, Inc.,
-#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-# ####################################################################
-
-"""
-Math Library
-==================================
-
-This is a collection of useful 2D math functions/classes.
-"""
-
-from fife import fife
-import math
-
-def normalize(vector):
-	"""
-	Helper function to normalize a 2D vector
-	
-	@param vector: a L{fife.DoublePoint} to be normalized
-	@type vector: L{fife.DoublePoint}
-	
-	@return: A normalized L{fife.DoublePoint}
-	"""
-	norm = fife.DoublePoint(0,0) 
-		
-	invLength = 1.0/vector.length()
-	if invLength > 1e-06:
-		norm.x = vector.x * invLength;
-		norm.y = vector.y * invLength;
-	else:
-		norm.x = 0
-		norm.y = 0
-	
-	return norm
-	
-def rotatePoint(origin, point, angle):
-	"""
-	Rotates a point around the specified origin.
-	
-	@param origin: A point specifying the origin.
-	@type origin: L{fife.DoublePoint}
-	@param point: The point to be rotated.
-	@type point: L{fife.DoublePoint}
-	@param angle: The angle in which to rotate the point.
-	@type angle: C{int} or C{float}
-	
-	@return: The rotated point.
-	@rtype: L{fife.DoublePoint}
-	"""
-	newp = fife.DoublePoint(0,0)
-	
-	theta = (angle * math.pi)/180
-	
-	costheta = math.cos(theta)
-	sintheta = math.sin(theta)
-	
-	x = point.x - origin.x
-	y = point.y - origin.y
-	
-	newp.x = costheta * x - sintheta * y
-	newp.y = sintheta * x + costheta * y
-	
-	return newp
-
-__all__ = ['normalize','rotatePoint']