# HG changeset patch # User terom # Date 1229351078 0 # Node ID 39e59dd36b6ee4a8e93266a5ed82b728c55ae80a # Parent 9b682981e060362d28412b7d60a985e0746f78dc clean up Vector a bit, remove unused Terrain -> direction function diff -r 9b682981e060 -r 39e59dd36b6e build/mkcmake.sh --- a/build/mkcmake.sh Tue Dec 09 04:49:33 2008 +0000 +++ b/build/mkcmake.sh Mon Dec 15 14:24:38 2008 +0000 @@ -12,7 +12,7 @@ INSTALL_PREFIX="$2" fi -echo "BUILD_TYPE=${BUILD_TYPE}" +echo "-- Using BUILD_TYPE=${BUILD_TYPE}, INSTALL_PREFIX=${INSTALL_PREFIX}" cmake ../ \ -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ diff -r 9b682981e060 -r 39e59dd36b6e src/PhysicsObject.cc --- a/src/PhysicsObject.cc Tue Dec 09 04:49:33 2008 +0000 +++ b/src/PhysicsObject.cc Mon Dec 15 14:24:38 2008 +0000 @@ -35,10 +35,13 @@ Vector PhysicsObject::walk_one_step (float partial, bool right) { // which way we are walking float deltaX = right ? partial : -partial; + Vector reached = this->position; - if(reached.roundToInt() == (reached+Vector(deltaX, 0)).roundToInt()) { - return reached+Vector(deltaX, 0); + + if (reached.roundToInt() == (reached + Vector(deltaX, 0)).roundToInt()) { + return reached + Vector(deltaX, 0); } + // Is there upward ramp if(!possibleLocation(position+Vector(deltaX, 0))) { // Yes. Then we check n pixels up @@ -211,13 +214,14 @@ collisionPoint = reached+shape[i]; if (inAir) - this->bounce(world.getNormal(reached+shape[i], reached-unitVector+shape[i])); + this->bounce(world.getNormal(reached + shape[i], reached - unitVector + shape[i])); reached = reached - unitVector; // Return to last point collided = true; - + + // snap velocity to zero once it's below a threshold if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED) - this->velocity = Vector(0,0); + this->velocity = Vector(0, 0); break; } diff -r 9b682981e060 -r 39e59dd36b6e src/Terrain.cc --- a/src/Terrain.cc Tue Dec 09 04:49:33 2008 +0000 +++ b/src/Terrain.cc Mon Dec 15 14:24:38 2008 +0000 @@ -316,7 +316,8 @@ */ static int getDirectionIndex (Vector direction) { Vector dir = direction.roundToInt(); - if(dir.x == 0 && dir.y == -1) { + + if (dir.x == 0 && dir.y == -1) { return 0; } else if (dir.x == 1 && dir.y == -1) { return 1; @@ -374,19 +375,6 @@ return normal; } -// XXX: weird vectors -Vector direction (const Vector &v) { - Vector tmp(v); - - if (tmp.length() > 0) - tmp /= tmp.length(); - - tmp.x = (uint16_t)(tmp.x); - tmp.y = (uint16_t)(tmp.y); - - return tmp; -} - // TODO: This could better :) // TODO: And this need some cleaning :) void Terrain::generateTerrain (int seed) { diff -r 9b682981e060 -r 39e59dd36b6e src/Terrain.hh --- a/src/Terrain.hh Tue Dec 09 04:49:33 2008 +0000 +++ b/src/Terrain.hh Mon Dec 15 14:24:38 2008 +0000 @@ -185,6 +185,4 @@ std::vector > getTerrain() const; }; -Vector direction (const Vector &v); - #endif diff -r 9b682981e060 -r 39e59dd36b6e src/Types.hh --- a/src/Types.hh Tue Dec 09 04:49:33 2008 +0000 +++ b/src/Types.hh Mon Dec 15 14:24:38 2008 +0000 @@ -12,7 +12,7 @@ /* * Standard vector used for Physics stuff */ -typedef _Vector Vector; +typedef VectorType Vector; /** * A player's health is measured in... @@ -27,7 +27,7 @@ /** * A Terrain/Pixel coordinate as a PixelDimension Vector */ -typedef _Vector PixelCoordinate; +typedef VectorType PixelCoordinate; /** * A time interval, measured in real milliseconds diff -r 9b682981e060 -r 39e59dd36b6e src/Vector.hh --- a/src/Vector.hh Tue Dec 09 04:49:33 2008 +0000 +++ b/src/Vector.hh Mon Dec 15 14:24:38 2008 +0000 @@ -7,106 +7,171 @@ /** * A 2D Vector class. Implements standard vector operations. */ -template -class _Vector { +template class VectorType { public: + /** Horizontal component */ T x; + /** Vertical component */ T y; /** - * Default constructor. + * Default constructor. Values are zero */ - _Vector() : x(0), y(0) {} + VectorType() : + x(0), y(0) + { + + } /** - * Constuctor. + * Scalar constuctor. * * @param x Initial x-coordinate * @param y Initial y-coordinate */ - _Vector(T x, T y) : x(x), y(y) {} + VectorType (const T &x, const T &y) : + x(x), y(y) + { + + } /** * Copy constructor. * * @param v Vector to be copied. */ - _Vector(const _Vector &v) : x(v.x), y(v.y) {} - - // Operator declarations - void operator=(const _Vector &v) { - this->x = v.x; - this->y = v.y; - } - _Vector operator+(const _Vector &v) const { - return _Vector(this->x+v.x, this->y+v.y); - } - _Vector operator-(const _Vector &v) const { - return _Vector(this->x-v.x, this->y-v.y); - } - _Vector operator- (void) const { - return _Vector(-this->x, -this->y); - } - _Vector operator*(const T &scalar) const { - return _Vector(this->x*scalar, this->y*scalar); - } - T operator*(const _Vector &v) const { - return (this->x*v.x + this->y*v.y); - } - _Vector operator/ (const T &d) const { - return _Vector(this->x / d, this->y / d); - } - void operator+=(const _Vector &v) { - *this = *this + v; - } - void operator-=(const _Vector &v) { - *this = *this - v; - } - void operator*=(const T &scalar) { - *this = *this * scalar; - } - void operator/=(const T &scalar) { - *this = *this / scalar; + VectorType (const VectorType &v) : + x(v.x), + y(v.y) + { + } - // Other operations - T length() const { + /** + * @name Standard operators + * + * @{ + */ + VectorType& operator= (const VectorType &v) { + x = v.x; + y = v.y; + + return *this; + } + + VectorType operator+ (const VectorType &v) const { + return VectorType(x + v.x, y + v.y); + } + + VectorType operator- (const VectorType &v) const { + return VectorType(x - v.x, y - v.y); + } + + /** Unary minus (v + -v = 0) */ + VectorType operator- (void) const { + return VectorType(-x, -y); + } + + /** Scalar multiplication */ + VectorType operator* (const T &scalar) const { + return VectorType(x * scalar, y * scalar); + } + + /** Scalar division */ + VectorType operator/ (const T &scalar) const { + return VectorType(x / scalar, y / scalar); + } + + /** Dot product */ + T operator* (const VectorType &v) const { + return (x * v.x + y * v.y); + } + + void operator+= (const VectorType &v) { + x += v.x; + y += v.y; + } + + void operator-= (const VectorType &v) { + x -= v.x; + y -= v.y; + } + + void operator*= (const T &scalar) { + x *= scalar; + y *= scalar; + } + + void operator/= (const T &scalar) { + x /= scalar; + y /= scalar; + } + + /** + * XXX: This needs to do some rounding for float-vectors + */ + bool operator== (const VectorType &other) const { + return (x == other.x) && (y == other.y); + } + + /** + * XXX: This needs to do some rounding for float-vectors + */ + bool operator!= (const VectorType &other) const { + return (x != other.x) || (y != other.y); + } + + // @} + + /** + * Vector scalar length + */ + T length (void) const { return sqrt(sqrLength()); } - T sqrLength() const { - return (this->x * this->x) + (this->y * this->y); + + /** + * Vector scalar length, squared + */ + T sqrLength (void) const { + return (x * x) + (y * y); } - _Vector roundToInt() const { - return _Vector((int)(x), (int)(y)); + + /** + * XXX: should be replaced with a working operator== implementation + * + * This doesn't actually *round*, it *truncates* + */ + VectorType roundToInt (void) const { + return VectorType((int)(x), (int)(y)); + } + + /** + * Test for a zero-length vector. + * + * XXX: this is currently only used when testing for Vector(0, 0), probably breaks otherwise + */ + bool zero (void) const { + return x == 0 && y == 0; } - // test Vectors as booleans - // XXX: comparing floats against zero... is a bad idea? - bool zero(void) const { - return x == 0 && y == 0; - } }; -// Unary operators -template -_Vector operator*(const T &scalar, const _Vector v) { +/** + * @name Postfix operators + * Operators with the vector as the second argument :) + * + * @{ + */ +template VectorType operator* (const T &scalar, const VectorType &v) { return (v * scalar); } -// Comparison operators -template -bool operator==(const _Vector &v1, const _Vector &v2) { - return ((v1.x == v2.x) && (v1.y == v2.y)); -} -template -bool operator!=(const _Vector &v1, const _Vector &v2) { - return !(v1 == v2); -} - -// Output operator -template -std::ostream& operator<<(std::ostream &s, const _Vector &v) { - return s<<"("< std::ostream& operator<< (std::ostream &s, const VectorType &v) { + return s << "(" << v.x << ", " << v.y << ")"; } #endif