summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog11
-rw-r--r--src/game-server/collisiondetection.cpp100
-rw-r--r--src/game-server/collisiondetection.hpp3
-rw-r--r--src/utils/mathutils.cpp37
-rw-r--r--src/utils/mathutils.h8
5 files changed, 80 insertions, 79 deletions
diff --git a/ChangeLog b/ChangeLog
index f41c6804..9b503033 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,13 @@
-2007-03-03 Rogier Polak <rogier.l.a.polak@gmail.com>
+2007-03-03 Bjørn Lindeijer <bjorn@lindeijer.nl>
+
+ * src/utils/mathutils.cpp, src/utils/mathutils.h,
+ src/game-server/collisiondetection.hpp,
+ src/game-server/collisiondetection.cpp: Use M_PI_2 instead of
+ acos(0.0f) and FLT_MAX instead of 1.0E40f (which exceeded the maximum
+ float value). Some cosmetics (whitespace and declaring variables where
+ they are used).
+
+2007-03-03 Rogier Polak <rogier.l.a.polak@gmail.com>
* src/utils/mathutils.h, src/utils/mathutils.cpp,
src/utils/fastsqrt.h, src/Makefile.am, src/game-server/main-game.cpp:
diff --git a/src/game-server/collisiondetection.cpp b/src/game-server/collisiondetection.cpp
index 6297e3d6..b6cf47ff 100644
--- a/src/game-server/collisiondetection.cpp
+++ b/src/game-server/collisiondetection.cpp
@@ -28,28 +28,28 @@
#include "utils/mathutils.h"
bool
-Collision::circleWithCirclesector( const Point& circlePos, int circleRadius,
- const Point& secPos, int secRadius,
- float secAngle, float secSize)
+Collision::circleWithCirclesector(const Point &circlePos, int circleRadius,
+ const Point &secPos, int secRadius,
+ float secAngle, float secSize)
{
float targetAngle;
- //calculate distance
+ // Calculate distance
int distX = circlePos.x - secPos.x;
int distY = circlePos.y - secPos.y;
float invDist = utils::math::fastInvSqrt(distX * distX + distY * distY);
float dist = 1.0f / invDist;
- //if out of range we can't hit it
+ // If out of range we can't hit it
if (dist > secRadius + circleRadius) {
return false;
}
- //if we are standing in it we hit it in any case
+ // If we are standing in it we hit it in any case
if (dist < circleRadius) {
return true;
}
- //calculate target angle
+ // Calculate target angle
if (distX > 0)
{
targetAngle = asin(-distY * invDist);
@@ -63,7 +63,7 @@ Collision::circleWithCirclesector( const Point& circlePos, int circleRadius,
}
- //calculate difference from segment angle
+ // Calculate difference from segment angle
float targetDiff = fabs(targetAngle - secAngle);
if (targetDiff > M_PI)
{
@@ -71,7 +71,7 @@ Collision::circleWithCirclesector( const Point& circlePos, int circleRadius,
}
- //Add hit circle
+ // Add hit circle
secSize += asin(circleRadius * invDist) * 2;
return (targetDiff < secSize * 0.5f);
@@ -88,27 +88,19 @@ Collision::diskWithCircleSector(const Point &diskCenter, int diskRadius,
const Point &sectorCenter, int sectorRadius,
int halfTopAngle, int placeAngle)
{
- //The coordinates of the diskcenter the base coordinate systems and an
- //alternate coordinate system
- float Px, Py, Px1, Py1,
- distAx, distAy, distBx, distBy;
-
- //Converting the radii to float
+ // Converting the radii to float
float R = (float) sectorRadius;
float Rp = (float) diskRadius;
- //The values of the trigonomic functions (only have to be computed once)
- float sinAlpha, cosAlpha, sinBeta, cosBeta, tan2Alpha;
-
- //Transform to the primary coordinate system
- Px = diskCenter.x - sectorCenter.x;
- Py = diskCenter.y - sectorCenter.y;
+ // Transform to the primary coordinate system
+ float Px = diskCenter.x - sectorCenter.x;
+ float Py = diskCenter.y - sectorCenter.y;
- //Calculating the values of the trigonomic functions
- sinAlpha = utils::math::cachedSin(halfTopAngle);
- cosAlpha = utils::math::cachedCos(halfTopAngle);
- sinBeta = utils::math::cachedSin(placeAngle);
- cosBeta = utils::math::cachedCos(placeAngle);
+ // The values of the trigonomic functions (only have to be computed once)
+ float sinAlpha = utils::math::cachedSin(halfTopAngle);
+ float cosAlpha = utils::math::cachedCos(halfTopAngle);
+ float sinBeta = utils::math::cachedSin(placeAngle);
+ float cosBeta = utils::math::cachedCos(placeAngle);
/**
* This bounding circle implementation can be used up and until a
@@ -119,81 +111,81 @@ Collision::diskWithCircleSector(const Point &diskCenter, int diskRadius,
*/
// Calculating the coordinates of the disk's center in coordinate system 4
- Px1 = Px * cosBeta + Py * sinBeta;
- Py1 = Py * cosBeta - Px * sinBeta;
+ float Px1 = Px * cosBeta + Py * sinBeta;
+ float Py1 = Py * cosBeta - Px * sinBeta;
- //Check for an intersection with the bounding circle
+ // Check for an intersection with the bounding circle
// (>) : touching is accepted
if ((cosAlpha * Px1 * Px1 + cosAlpha * Py1 * Py1 - Px1 * R)
> (Rp * Rp * cosAlpha + Rp * R)) return false;
- //Check for a region 4 collision
+ // Check for a region 4 collision
if ((Px*Px + Py*Py) <= (Rp*Rp)) return true;
- //Calculating the coordinates of the disk's center in coordinate system 1
+ // Calculating the coordinates of the disk's center in coordinate system 1
Px1 = Px * (cosAlpha * cosBeta + sinAlpha * sinBeta)
+ Py * (cosAlpha * sinBeta - sinAlpha * cosBeta);
Py1 = Py * (cosAlpha * cosBeta + sinAlpha * sinBeta)
- Px * (cosAlpha * sinBeta - sinAlpha * cosBeta);
- //Check if P is in region 5 (using coordinate system 1)
+ // Check if P is in region 5 (using coordinate system 1)
if ((Px1 >= 0.0f) && (Px1 <= R) && (Py1 <= 0.0f))
{
- //Return true on intersection, false otherwise
+ // Return true on intersection, false otherwise
// (>=) : touching is accepted
return (Py1 >= -1.0f * Rp);
}
- //Check if P is in region 3 (using coordinate system 1)
+ // Check if P is in region 3 (using coordinate system 1)
if ((Px1 > R) && (Py1 <= 0.0f))
{
- //Calculating the vector from point A to the disk center
- distAx = Px - R * (cosAlpha*cosBeta + sinAlpha*sinBeta);
- distAy = Py - R * (cosAlpha*sinBeta - sinAlpha*cosBeta);
+ // Calculating the vector from point A to the disk center
+ float distAx = Px - R * (cosAlpha * cosBeta + sinAlpha * sinBeta);
+ float distAy = Py - R * (cosAlpha * sinBeta - sinAlpha * cosBeta);
- //Check for a region 3 collision
- return ((distAx*distAx + distAy*distAy) <= Rp*Rp);
+ // Check for a region 3 collision
+ return ((distAx * distAx + distAy * distAy) <= Rp * Rp);
}
- //Discard, if P is in region 4 (was previously checked)
+ // Discard, if P is in region 4 (was previously checked)
if ((Px1 < 0.0f) && (Py1 <= 0.0f)) return false;
- tan2Alpha = utils::math::cachedTan(2 * halfTopAngle);
+ float tan2Alpha = utils::math::cachedTan(2 * halfTopAngle);
- //Check if P is in region 1 (using coordinate system 1)
+ // Check if P is in region 1 (using coordinate system 1)
if ((Px1 >= 0.0f) && (Py1 >= 0.0f) && (Py1 <= Px1 * tan2Alpha))
{
- //Return true on intersection, false otherwise
+ // Return true on intersection, false otherwise
// (<=) : touching is accepted
- return ((Px*Px + Py*Py) <= (R*R + Rp*Rp + 2.0f*R*Rp));
+ return ((Px * Px + Py * Py) <= (R * R + Rp * Rp + 2.0f * R * Rp));
}
- //Calculating the coordinates of the disk's center in coordinate system 3
+ // Calculating the coordinates of the disk's center in coordinate system 3
Px1 = Px * (cosAlpha * cosBeta - sinAlpha * sinBeta)
+ Py * (sinAlpha * cosBeta + cosAlpha * sinBeta);
Py1 = Py * (cosAlpha * cosBeta - sinAlpha * sinBeta)
- Px * (sinAlpha * cosBeta + cosAlpha * sinBeta);
- //Discard, if P is in region 4 (was previously checked)
+ // Discard, if P is in region 4 (was previously checked)
if ((Px1 < 0.0f) && (Py1 >= 0.0f)) return false;
- //Check if P is in region 6 (using coordinate system 3)
+ // Check if P is in region 6 (using coordinate system 3)
if ((Px1 >= 0.0f) && (Px1 <= R) && (Py1 >= 0.0f))
{
- //Return true on intersection, false otherwise
+ // Return true on intersection, false otherwise
// (<=) : touching is accepted
return (Py1 <= Rp);
}
- //Check if P is in region 2 (using coordinate system 3)
+ // Check if P is in region 2 (using coordinate system 3)
if ((Px1 > R) && (Py1 <= 0.0f))
{
- //Calculating the vector from point B to the disk center
- distBx = Px - R * (cosAlpha*cosBeta - sinAlpha*sinBeta);
- distBy = Py - R * (cosAlpha*sinBeta + sinAlpha*cosBeta);
+ // Calculating the vector from point B to the disk center
+ float distBx = Px - R * (cosAlpha * cosBeta - sinAlpha * sinBeta);
+ float distBy = Py - R * (cosAlpha * sinBeta + sinAlpha * cosBeta);
- //Check for a region 2 collision
- return ((distBx*distBx + distBy*distBy) <= (Rp*Rp));
+ // Check for a region 2 collision
+ return ((distBx * distBx + distBy * distBy) <= (Rp * Rp));
}
// The intersection with the bounding circle is in region 4,
diff --git a/src/game-server/collisiondetection.hpp b/src/game-server/collisiondetection.hpp
index 88631031..bfbab413 100644
--- a/src/game-server/collisiondetection.hpp
+++ b/src/game-server/collisiondetection.hpp
@@ -47,7 +47,6 @@ namespace Collision
diskWithCircleSector(const Point &diskCenter, int diskRadius,
const Point &sectorCenter, int sectorRadius,
int halfTopAngle, int placeAngle);
-
-};
+}
#endif
diff --git a/src/utils/mathutils.cpp b/src/utils/mathutils.cpp
index 0a46ff2c..407bba7f 100644
--- a/src/utils/mathutils.cpp
+++ b/src/utils/mathutils.cpp
@@ -23,6 +23,7 @@
#include "mathutils.h"
#include <cmath>
+#include <float.h>
#define MATH_UTILS_MAX_ANGLE 360
@@ -30,9 +31,7 @@ float sinList[MATH_UTILS_MAX_ANGLE];
float cosList[MATH_UTILS_MAX_ANGLE];
float tanList[MATH_UTILS_MAX_ANGLE];
-/**
- * fastInvSqrt:
- *
+/*
* A very fast function to calculate the approximate inverse square root of a
* floating point value. For an explanation of the inverse squareroot function
* read:
@@ -47,46 +46,40 @@ float tanList[MATH_UTILS_MAX_ANGLE];
*/
float utils::math::fastInvSqrt(float x)
{
- float xhalf = 0.5f*x;
- int i = *(int*)&x;
- i = 0x5f375a86- (i>>1);
- x = *(float*)&i;
- x = x*(1.5f-xhalf*x*x);
+ float xhalf = 0.5f * x;
+ int i = *(int*) &x;
+ i = 0x5f375a86 - (i >> 1);
+ x = *(float*) &i;
+ x = x * (1.5f-xhalf * x * x);
return x;
}
-/**
- * fastSqrt:
- *
- * A helper function that uses the fastInvSqrt for getting the
- * normal squareroot.
- */
float utils::math::fastSqrt(float x)
{
- return 1.0f/utils::math::fastInvSqrt(x);
+ return 1.0f / utils::math::fastInvSqrt(x);
}
void utils::math::init()
{
// Constant for calculating an angle in radians out of an angle in degrees
- const float radianAngleRatio = acos(0.0f) / 90.0f; // pi/2 / 90[deg]
+ const float radianAngleRatio = M_PI_2 / 90.0f; // pi/2 / 90[deg]
- for(int i = 0; i < MATH_UTILS_MAX_ANGLE; i++)
+ for (int i = 0; i < MATH_UTILS_MAX_ANGLE; i++)
{
- sinList[i] = sin(radianAngleRatio * (float)i);
- cosList[i] = cos(radianAngleRatio * (float)i);
+ sinList[i] = sin(radianAngleRatio * (float) i);
+ cosList[i] = cos(radianAngleRatio * (float) i);
if (i == 90)
{
- tanList[i] = 1.0E40f; //approximately infinity
+ tanList[i] = FLT_MAX; // approximately infinity
continue;
}
if (i == 270)
{
- tanList[i] = -1.0E40f; //approximately infinity
+ tanList[i] = -FLT_MAX; // approximately infinity
continue;
}
- tanList[i] = tan(radianAngleRatio * (float)i);
+ tanList[i] = tan(radianAngleRatio * (float) i);
}
}
diff --git a/src/utils/mathutils.h b/src/utils/mathutils.h
index 3d90b022..31f48805 100644
--- a/src/utils/mathutils.h
+++ b/src/utils/mathutils.h
@@ -27,8 +27,16 @@ namespace utils
{
namespace math
{
+ /**
+ * A very fast function to calculate the approximate inverse square
+ * root of a floating point value.
+ */
float fastInvSqrt(float x);
+ /**
+ * A helper function that uses the fastInvSqrt for getting the
+ * normal squareroot.
+ */
float fastSqrt(float x);
float cachedSin(int angle);