summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2009-12-06 18:02:03 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2009-12-06 18:02:03 +0100
commit49cd2c5d236294fa99a612dbf4833c72a7f89de8 (patch)
tree9b683eeab5ebd4bacdcf08b07e742071e24ca386 /src
parent70e357b136a69b5ddbca1c44b2ab35b13b694484 (diff)
downloadmanaserv-49cd2c5d236294fa99a612dbf4833c72a7f89de8.tar.gz
manaserv-49cd2c5d236294fa99a612dbf4833c72a7f89de8.tar.bz2
manaserv-49cd2c5d236294fa99a612dbf4833c72a7f89de8.tar.xz
manaserv-49cd2c5d236294fa99a612dbf4833c72a7f89de8.zip
Moved rectangle intersection to the Rectangle class
Also a bit of pointless cleanup in the math utils.
Diffstat (limited to 'src')
-rw-r--r--src/game-server/collisiondetection.cpp28
-rw-r--r--src/game-server/collisiondetection.hpp26
-rw-r--r--src/point.h8
-rw-r--r--src/scripting/lua.cpp2
-rw-r--r--src/utils/mathutils.cpp29
-rw-r--r--src/utils/mathutils.h45
6 files changed, 63 insertions, 75 deletions
diff --git a/src/game-server/collisiondetection.cpp b/src/game-server/collisiondetection.cpp
index 0a7a97ea..f8582df8 100644
--- a/src/game-server/collisiondetection.cpp
+++ b/src/game-server/collisiondetection.cpp
@@ -32,10 +32,9 @@
#define test_degrees(pos,s1,s2) (pos > s1 && pos < s2) || (s1 > s2 && !(pos < s1 && pos > s2))
-bool
-Collision::circleWithCirclesector(const Point &circlePos, int circleRadius,
- const Point &secPos, int secRadius,
- float secAngle, float secSize)
+bool Collision::circleWithCirclesector(const Point &circlePos, int circleRadius,
+ const Point &secPos, int secRadius,
+ float secAngle, float secSize)
{
float targetAngle;
@@ -279,27 +278,12 @@ Collision::diskWithCircleSector2(const Point &diskCenter, int diskRadius,
return false;
}
*/
-bool
-Collision::CircleWithCircle(const Point &center1, int radius1,
- const Point &center2, int radius2)
+
+bool Collision::circleWithCircle(const Point &center1, int radius1,
+ const Point &center2, int radius2)
{
int distx = center1.x - center2.x;
int disty = center1.y - center2.y;
double dist = sqrt((distx * distx) + (disty * disty));
return (dist < radius1 + radius2);
}
-
-bool
-Collision::rectWithRect(const Point &pos1, const Point &size1,
- const Point &pos2, const Point &size2)
-{
- return(
- pos1.x < (pos2.x + size2.x) //left edge 1 left of right edge 2
- &&
- (pos1.x + size1.x) > pos2.x //right edge 1 right of left edge 2
- &&
- pos1.y < (pos2.y + size2.y) //upper edge 1 above lower edge 2
- &&
- (pos1.y + size1.y) > pos2.y //lower edge 1 under upper edge 2
- );
-}
diff --git a/src/game-server/collisiondetection.hpp b/src/game-server/collisiondetection.hpp
index c60d9d64..d0731e94 100644
--- a/src/game-server/collisiondetection.hpp
+++ b/src/game-server/collisiondetection.hpp
@@ -29,10 +29,10 @@ class Point;
namespace Collision
{
- bool
- circleWithCirclesector(const Point &circlePos, int circleRadius,
- const Point &secPos, int secRadius,
- float secAngle, float secSize);
+ bool circleWithCirclesector(const Point &circlePos, int circleRadius,
+ const Point &secPos, int secRadius,
+ float secAngle, float secSize);
+
/**
* Checks if a disk and a circle-sector collide
*
@@ -41,23 +41,15 @@ namespace Collision
* @param placeAngle
* The placement-angle of the circle sector in degrees (0,359).
*/
- bool
- diskWithCircleSector(const Point &diskCenter, int diskRadius,
- const Point &sectorCenter, int sectorRadius,
- int halfTopAngle, int placeAngle);
+ bool diskWithCircleSector(const Point &diskCenter, int diskRadius,
+ const Point &sectorCenter, int sectorRadius,
+ int halfTopAngle, int placeAngle);
/**
* Checks if two circles intersect.
*/
- bool
- CircleWithCircle(const Point &center1, int radius1,
- const Point &center2, int radius2);
-
- /** checks if two rectangles intersect */
- bool
- rectWithRect(const Point &pos1, const Point &size1,
- const Point &pos2, const Point &size2);
-
+ bool circleWithCircle(const Point &center1, int radius1,
+ const Point &center2, int radius2);
}
#endif
diff --git a/src/point.h b/src/point.h
index d6ec0841..3d3d7c1d 100644
--- a/src/point.h
+++ b/src/point.h
@@ -77,6 +77,14 @@ class Rectangle
return (unsigned short)(p.x - x) < w &&
(unsigned short)(p.y - y) < h;
}
+
+ bool intersects(const Rectangle &r) const
+ {
+ return x < (r.x + r.w) &&
+ y < (r.y + r.h) &&
+ x + w > r.x &&
+ y + h > r.y;
+ }
};
#endif // _TMWSERV_POINT_H_
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index fe777a0c..4cb4d213 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -1043,7 +1043,7 @@ static int get_beings_in_circle(lua_State *s)
if (t == OBJECT_NPC || t == OBJECT_CHARACTER || t == OBJECT_MONSTER)
{
Being *b = static_cast<Being *> (*i);
- if (Collision::CircleWithCircle(b->getPosition(), b->getSize(),
+ if (Collision::circleWithCircle(b->getPosition(), b->getSize(),
Point(x, y), r))
{
lua_pushinteger(s, tableIndex);
diff --git a/src/utils/mathutils.cpp b/src/utils/mathutils.cpp
index 55668ddf..9024a400 100644
--- a/src/utils/mathutils.cpp
+++ b/src/utils/mathutils.cpp
@@ -25,11 +25,14 @@
#include <string.h>
#include <float.h>
-#define MATH_UTILS_MAX_ANGLE 360
+static const int MATH_UTILS_MAX_ANGLE = 360;
-float sinList[MATH_UTILS_MAX_ANGLE];
-float cosList[MATH_UTILS_MAX_ANGLE];
-float tanList[MATH_UTILS_MAX_ANGLE];
+static float sinList[MATH_UTILS_MAX_ANGLE];
+static float cosList[MATH_UTILS_MAX_ANGLE];
+static float tanList[MATH_UTILS_MAX_ANGLE];
+
+namespace utils {
+namespace math {
/*
* A very fast function to calculate the approximate inverse square root of a
@@ -41,7 +44,7 @@ float tanList[MATH_UTILS_MAX_ANGLE];
*
* I wholeheartedly disagree with the use of this function -- silene
*/
-float utils::math::fastInvSqrt(float x)
+float fastInvSqrt(float x)
{
typedef char float_must_be_32_bits[(sizeof(float) == 4) * 2 - 1];
float xhalf = 0.5f * x;
@@ -53,12 +56,7 @@ float utils::math::fastInvSqrt(float x)
return x;
}
-float utils::math::fastSqrt(float x)
-{
- return x * utils::math::fastInvSqrt(x);
-}
-
-void utils::math::init()
+void init()
{
// Constant for calculating an angle in radians out of an angle in degrees
const float radianAngleRatio = M_PI_2 / 90.0f; // pi/2 / 90[deg]
@@ -82,17 +80,20 @@ void utils::math::init()
}
}
-float utils::math::cachedSin(int angle)
+float cachedSin(int angle)
{
return sinList[angle];
}
-float utils::math::cachedCos(int angle)
+float cachedCos(int angle)
{
return cosList[angle];
}
-float utils::math::cachedTan(int angle)
+float cachedTan(int angle)
{
return tanList[angle];
}
+
+} // namespace math
+} // namespace utils
diff --git a/src/utils/mathutils.h b/src/utils/mathutils.h
index 04639d0f..ebada25c 100644
--- a/src/utils/mathutils.h
+++ b/src/utils/mathutils.h
@@ -21,32 +21,35 @@
#ifndef _TMWSERV_MATHUTILS_H_
#define _TMWSERV_MATHUTILS_H_
-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);
+namespace utils {
+namespace math {
- float cachedSin(int angle);
-
- float cachedCos(int angle);
+/**
+ * A very fast function to calculate the approximate inverse square
+ * root of a floating point value.
+ */
+float fastInvSqrt(float x);
- float cachedTan(int angle);
+/**
+ * A helper function that uses the fastInvSqrt for getting the
+ * normal squareroot.
+ */
+inline float fastSqrt(float x)
+{
+ return x * fastInvSqrt(x);
+}
- void init();
+float cachedSin(int angle);
+float cachedCos(int angle);
+float cachedTan(int angle);
- } // namespace math
+/**
+ * Pre-calculates the needed trigomic function values. Should be called before
+ * using cachedSin, cachedCos or cachedTan.
+ */
+void init();
+} // namespace math
} // namespace utils
#endif