Refactored sprite & transform. Replaced distance with collider when picking objects up.
This commit is contained in:
@@ -5,36 +5,23 @@
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#include <components/basic/Sprite.h>
|
||||
#include <math/GameMath.h>
|
||||
#include <model/Transform.h>
|
||||
#include <utils/Rect.h>
|
||||
#include <cmath>
|
||||
|
||||
using namespace farmlands::components::basic;
|
||||
|
||||
namespace farmlands {
|
||||
|
||||
static const float Sqrt2 = 1.41421356237309504880f;
|
||||
|
||||
static const float DirectionVectorX[] =
|
||||
void move(float *x, float *y, model::Direction direction, float distance)
|
||||
{
|
||||
0, 1, 0, Sqrt2, // none, E, N, NE
|
||||
-1, 0, -Sqrt2, 0, // W, inv, NW, inv
|
||||
0, Sqrt2, 0, 0, // S, SE, inv, inv
|
||||
-Sqrt2, 0, 0, 0 // SW, inv, inv, inv
|
||||
};
|
||||
float dx = cosf(degToRad(direction));
|
||||
float dy = sinf(degToRad(direction));
|
||||
|
||||
static const float DirectionVectorY[] =
|
||||
{
|
||||
0, 0, -1, -Sqrt2, // none, E, N, NE
|
||||
0, 0, -Sqrt2, 0, // W, inv, NW, inv
|
||||
1, Sqrt2, 0, 0, // S, SE, inv, inv
|
||||
Sqrt2, 0, 0, 0 // SW, inv, inv, inv
|
||||
};
|
||||
|
||||
void translate(float x, float y, model::Direction direction, float distance, float* outX, float *outY)
|
||||
{
|
||||
float dx = DirectionVectorX[direction];
|
||||
float dy = DirectionVectorY[direction];
|
||||
|
||||
*outX = x + dx * distance;
|
||||
*outY = y + dy * distance;
|
||||
*x += dx * distance;
|
||||
*x += dy * distance;
|
||||
}
|
||||
|
||||
void moveTowards(float *x, float *y, float towardsX, float towardsY, float speed)
|
||||
@@ -49,4 +36,24 @@ float distanceSq(float x0, float y0, float x1, float y1)
|
||||
return (x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1);
|
||||
}
|
||||
|
||||
bool checkCollision(model::GameObject* objA, model::GameObject* objB)
|
||||
{
|
||||
Sprite* sprA = objA->component<Sprite>();
|
||||
Sprite* sprB = objB->component<Sprite>();
|
||||
|
||||
// If both have sprites, they intersect if their rectangles intersect
|
||||
if (sprA && sprB)
|
||||
return sprA->boundaries().intersects(sprB->boundaries());
|
||||
|
||||
// Only one has sprite. They collide if sprite rectangle contains the other point
|
||||
if (sprA)
|
||||
return sprA->boundaries().contains(objB->transform.globalX(), objB->transform.globalY());
|
||||
if (sprB)
|
||||
return sprB->boundaries().contains(objA->transform.globalX(), objA->transform.globalY());
|
||||
|
||||
// Objects collide if their coordinates are equal.
|
||||
return std::abs(objA->transform.globalX() - objB->transform.globalX()) < 1e-10
|
||||
&& std::abs(objA->transform.globalY() - objB->transform.globalY()) < 1e-10;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -9,9 +9,20 @@
|
||||
#define MATH_GAMEMATH_H_
|
||||
|
||||
#include <model/Direction.h>
|
||||
#include <model/GameObject.h>
|
||||
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <cmath>
|
||||
|
||||
namespace farmlands {
|
||||
|
||||
void move(float* x, float* y, model::Direction direction, float distance);
|
||||
void moveTowards(float *x, float *y, float towardsX, float towardsY, float speed);
|
||||
|
||||
float distanceSq(float x0, float y0, float x1, float y1);
|
||||
|
||||
bool checkCollision(model::GameObject* objA, model::GameObject* objB);
|
||||
|
||||
template<typename TVal, typename TMin, typename TMax>
|
||||
TVal clamp (TVal value, TMin min, TMax max)
|
||||
{
|
||||
@@ -24,9 +35,12 @@ namespace farmlands {
|
||||
return value;
|
||||
}
|
||||
|
||||
void translate(float x, float y, model::Direction direction, float distance, float* outX, float* outY);
|
||||
void moveTowards(float *x, float *y, float towardsX, float towardsY, float speed);
|
||||
float distanceSq(float x0, float y0, float x1, float y1);
|
||||
|
||||
/**
|
||||
* Converts from degrees to radians
|
||||
*/
|
||||
inline float degToRad(float angle)
|
||||
{
|
||||
return angle * (float)M_PI / 360.0f;
|
||||
}
|
||||
}
|
||||
#endif /* MATH_GAMEMATH_H_ */
|
||||
|
Reference in New Issue
Block a user