Remove Point2f, replace with Vector2f

This commit is contained in:
Bram Verhulst
2024-04-17 13:54:48 +02:00
parent 64e96ab209
commit db83ae5e13
42 changed files with 494 additions and 634 deletions

View File

@@ -24,11 +24,11 @@ void utils::DrawPoint(float x, float y, float pointSize) {
glEnd();
}
void utils::DrawPoint(const Point2f& p, float pointSize) {
void utils::DrawPoint(const Vector2f& p, float pointSize) {
DrawPoint(p.x, p.y, pointSize);
}
void utils::DrawPoints(Point2f* pVertices, int nrVertices, float pointSize) {
void utils::DrawPoints(Vector2f* pVertices, int nrVertices, float pointSize) {
glPointSize(pointSize);
glBegin(GL_POINTS);
{
@@ -49,11 +49,11 @@ void utils::DrawLine(float x1, float y1, float x2, float y2, float lineWidth) {
glEnd();
}
void utils::DrawLine(const Point2f& p1, const Point2f& p2, float lineWidth) {
void utils::DrawLine(const Vector2f& p1, const Vector2f& p2, float lineWidth) {
DrawLine(p1.x, p1.y, p2.x, p2.y, lineWidth);
}
void utils::DrawTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3, float lineWidth) {
void utils::DrawTriangle(const Vector2f& p1, const Vector2f& p2, const Vector2f& p3, float lineWidth) {
glLineWidth(lineWidth);
glBegin(GL_LINE_LOOP);
{
@@ -64,7 +64,7 @@ void utils::DrawTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3
glEnd();
}
void utils::FillTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3) {
void utils::FillTriangle(const Vector2f& p1, const Vector2f& p2, const Vector2f& p3) {
glBegin(GL_TRIANGLES);
{
glVertex2f(p1.x, p1.y);
@@ -89,7 +89,7 @@ void utils::DrawRect(float left, float bottom, float width, float height, float
}
}
void utils::DrawRect(const Point2f& bottomLeft, float width, float height, float lineWidth) {
void utils::DrawRect(const Vector2f& bottomLeft, float width, float height, float lineWidth) {
DrawRect(bottomLeft.x, bottomLeft.y, width, height, lineWidth);
}
@@ -111,7 +111,7 @@ void utils::FillRect(float left, float bottom, float width, float height) {
}
}
void utils::FillRect(const Point2f& bottomLeft, float width, float height) {
void utils::FillRect(const Vector2f& bottomLeft, float width, float height) {
FillRect(bottomLeft.x, bottomLeft.y, width, height);
}
@@ -135,7 +135,7 @@ void utils::DrawEllipse(float centerX, float centerY, float radX, float radY, fl
}
}
void utils::DrawEllipse(const Point2f& center, float radX, float radY, float lineWidth) {
void utils::DrawEllipse(const Vector2f& center, float radX, float radY, float lineWidth) {
DrawEllipse(center.x, center.y, radX, radY, lineWidth);
}
@@ -162,7 +162,7 @@ void utils::FillEllipse(const Ellipsef& ellipse) {
FillEllipse(ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY);
}
void utils::FillEllipse(const Point2f& center, float radX, float radY) {
void utils::FillEllipse(const Vector2f& center, float radX, float radY) {
FillEllipse(center.x, center.y, radX, radY);
}
@@ -185,7 +185,7 @@ void utils::DrawArc(float centerX, float centerY, float radX, float radY, float
}
void utils::DrawArc(const Point2f& center, float radX, float radY, float fromAngle, float tillAngle, float lineWidth) {
void utils::DrawArc(const Vector2f& center, float radX, float radY, float fromAngle, float tillAngle, float lineWidth) {
DrawArc(center.x, center.y, radX, radY, fromAngle, tillAngle, lineWidth);
}
@@ -206,15 +206,15 @@ void utils::FillArc(float centerX, float centerY, float radX, float radY, float
glEnd();
}
void utils::FillArc(const Point2f& center, float radX, float radY, float fromAngle, float tillAngle) {
void utils::FillArc(const Vector2f& center, float radX, float radY, float fromAngle, float tillAngle) {
FillArc(center.x, center.y, radX, radY, fromAngle, tillAngle);
}
void utils::DrawPolygon(const std::vector<Point2f>& vertices, bool closed, float lineWidth) {
void utils::DrawPolygon(const std::vector<Vector2f>& vertices, bool closed, float lineWidth) {
DrawPolygon(vertices.data(), vertices.size(), closed, lineWidth);
}
void utils::DrawPolygon(const Point2f* pVertices, size_t nrVertices, bool closed, float lineWidth) {
void utils::DrawPolygon(const Vector2f* pVertices, size_t nrVertices, bool closed, float lineWidth) {
glLineWidth(lineWidth);
closed ? glBegin(GL_LINE_LOOP) : glBegin(GL_LINE_STRIP);
{
@@ -225,11 +225,11 @@ void utils::DrawPolygon(const Point2f* pVertices, size_t nrVertices, bool closed
glEnd();
}
void utils::FillPolygon(const std::vector<Point2f>& vertices) {
void utils::FillPolygon(const std::vector<Vector2f>& vertices) {
FillPolygon(vertices.data(), vertices.size());
}
void utils::FillPolygon(const Point2f* pVertices, size_t nrVertices) {
void utils::FillPolygon(const Vector2f* pVertices, size_t nrVertices) {
glBegin(GL_POLYGON);
{
for (size_t idx { 0 }; idx < nrVertices; ++idx) {
@@ -245,34 +245,34 @@ float utils::GetDistance(float x1, float y1, float x2, float y2) {
return ( sqrtf(( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 )) );
}
float utils::GetDistance(const Point2f& p1, const Point2f& p2) {
float utils::GetDistance(const Vector2f& p1, const Vector2f& p2) {
return GetDistance(p1.x, p1.y, p2.x, p2.y);
}
bool utils::IsPointInRect(const Point2f& p, const Rectf& r) {
bool utils::IsPointInRect(const Vector2f& p, const Rectf& r) {
return ( p.x >= r.left &&
p.x <= r.left + r.width &&
p.y >= r.bottom &&
p.y <= r.bottom + r.height );
}
bool utils::IsPointInCircle(const Point2f& p, const Circlef& c) {
bool utils::IsPointInCircle(const Vector2f& p, const Circlef& c) {
float squaredDist { ( p.x - c.center.x ) * ( p.x - c.center.x ) + ( p.y - c.center.y ) * ( p.y - c.center.y ) };
float squaredRadius { c.radius * c.radius };
return ( squaredRadius >= squaredDist );
}
bool utils::IsOverlapping(const Point2f& a, const Point2f& b, const Rectf& r) {
bool utils::IsOverlapping(const Vector2f& a, const Vector2f& b, const Rectf& r) {
// if one of the line segment end points is in the rect
if (utils::IsPointInRect(a, r) || utils::IsPointInRect(b, r)) {
return true;
}
HitInfo hitInfo {};
Point2f vertices[] { Point2f { r.left, r.bottom },
Point2f { r.left + r.width, r.bottom },
Point2f { r.left + r.width, r.bottom + r.height },
Point2f { r.left, r.bottom + r.height } };
Vector2f vertices[] { Vector2f { r.left, r.bottom },
Vector2f { r.left + r.width, r.bottom },
Vector2f { r.left + r.width, r.bottom + r.height },
Vector2f { r.left, r.bottom + r.height } };
return Raycast(vertices, 4, a, b, hitInfo);
}
@@ -297,16 +297,16 @@ bool utils::IsOverlapping(const Rectf& r, const Circlef& c) {
return true;
}
// Check line segments
if (utils::DistPointLineSegment(c.center, Point2f { r.left, r.bottom }, Point2f { r.left, r.bottom + r.height }) <= c.radius) {
if (utils::DistPointLineSegment(c.center, Vector2f { r.left, r.bottom }, Vector2f { r.left, r.bottom + r.height }) <= c.radius) {
return true;
}
if (utils::DistPointLineSegment(c.center, Point2f { r.left, r.bottom }, Point2f { r.left + r.width, r.bottom }) <= c.radius) {
if (utils::DistPointLineSegment(c.center, Vector2f { r.left, r.bottom }, Vector2f { r.left + r.width, r.bottom }) <= c.radius) {
return true;
}
if (utils::DistPointLineSegment(c.center, Point2f { r.left + r.width, r.bottom + r.height }, Point2f { r.left, r.bottom + r.height }) <= c.radius) {
if (utils::DistPointLineSegment(c.center, Vector2f { r.left + r.width, r.bottom + r.height }, Vector2f { r.left, r.bottom + r.height }) <= c.radius) {
return true;
}
if (utils::DistPointLineSegment(c.center, Point2f { r.left + r.width, r.bottom + r.height }, Point2f { r.left + r.width, r.bottom }) <= c.radius) {
if (utils::DistPointLineSegment(c.center, Vector2f { r.left + r.width, r.bottom + r.height }, Vector2f { r.left + r.width, r.bottom }) <= c.radius) {
return true;
}
return false;
@@ -322,15 +322,15 @@ bool utils::IsOverlapping(const Circlef& c1, const Circlef& c2) {
return ( squaredDistance < squaredTouchingDistance );
}
bool utils::IsOverlapping(const Point2f& a, const Point2f& b, const Circlef& c) {
bool utils::IsOverlapping(const Vector2f& a, const Vector2f& b, const Circlef& c) {
return utils::DistPointLineSegment(c.center, a, b) <= c.radius;
}
bool utils::IsOverlapping(const std::vector<Point2f>& vertices, const Circlef& c) {
bool utils::IsOverlapping(const std::vector<Vector2f>& vertices, const Circlef& c) {
return IsOverlapping(vertices.data(), vertices.size(), c);
}
bool utils::IsOverlapping(const Point2f* vertices, size_t nrVertices, const Circlef& c) {
bool utils::IsOverlapping(const Vector2f* vertices, size_t nrVertices, const Circlef& c) {
// Verify whether one of vertices is in circle
for (size_t i { 0 }; i < nrVertices; ++i) {
if (IsPointInCircle(vertices[i], c)) {
@@ -352,11 +352,11 @@ bool utils::IsOverlapping(const Point2f* vertices, size_t nrVertices, const Circ
return false;
}
bool utils::IsPointInPolygon(const Point2f& p, const std::vector<Point2f>& vertices) {
bool utils::IsPointInPolygon(const Vector2f& p, const std::vector<Vector2f>& vertices) {
return IsPointInPolygon(p, vertices.data(), vertices.size());
}
bool utils::IsPointInPolygon(const Point2f& p, const Point2f* vertices, size_t nrVertices) {
bool utils::IsPointInPolygon(const Vector2f& p, const Vector2f* vertices, size_t nrVertices) {
if (nrVertices < 2) {
return false;
}
@@ -387,7 +387,7 @@ bool utils::IsPointInPolygon(const Point2f& p, const Point2f* vertices, size_t n
// and count how often it hits any side of the polygon.
// If the number of hits is even, it's outside of the polygon, if it's odd, it's inside.
int numberOfIntersectionPoints { 0 };
Point2f p2 { xMax + 10.0f, p.y }; // Horizontal line from point to point outside polygon (p2)
Vector2f p2 { xMax + 10.0f, p.y }; // Horizontal line from point to point outside polygon (p2)
// Count the number of intersection points
float lambda1 {}, lambda2 {};
@@ -406,7 +406,7 @@ bool utils::IsPointInPolygon(const Point2f& p, const Point2f* vertices, size_t n
}
}
bool utils::IntersectLineSegments(const Point2f& p1, const Point2f& p2, const Point2f& q1, const Point2f& q2, float& outLambda1, float& outLambda2, float epsilon) {
bool utils::IntersectLineSegments(const Vector2f& p1, const Vector2f& p2, const Vector2f& q1, const Vector2f& q2, float& outLambda1, float& outLambda2, float epsilon) {
bool intersecting { false };
Vector2f p1p2 { p1, p2 };
@@ -451,11 +451,11 @@ bool utils::IntersectLineSegments(const Point2f& p1, const Point2f& p2, const Po
return intersecting;
}
bool utils::Raycast(const std::vector<Point2f>& vertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo) {
bool utils::Raycast(const std::vector<Vector2f>& vertices, const Vector2f& rayP1, const Vector2f& rayP2, HitInfo& hitInfo) {
return Raycast(vertices.data(), vertices.size(), rayP1, rayP2, hitInfo);
}
bool utils::Raycast(const Point2f* vertices, const size_t nrVertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo) {
bool utils::Raycast(const Vector2f* vertices, const size_t nrVertices, const Vector2f& rayP1, const Vector2f& rayP2, HitInfo& hitInfo) {
if (nrVertices == 0) {
return false;
}
@@ -473,8 +473,8 @@ bool utils::Raycast(const Point2f* vertices, const size_t nrVertices, const Poin
for (size_t idx { 0 }; idx <= nrVertices; ++idx) {
// Consider line segment between 2 consecutive vertices
// (modulo to allow closed polygon, last - first vertice)
Point2f q1 = vertices[( idx + 0 ) % nrVertices];
Point2f q2 = vertices[( idx + 1 ) % nrVertices];
Vector2f q1 = vertices[( idx + 0 ) % nrVertices];
Vector2f q2 = vertices[( idx + 1 ) % nrVertices];
// r2: minimal AABB rect enclosing the 2 vertices
r2.left = std::min(q1.x, q2.x);
@@ -489,7 +489,7 @@ bool utils::Raycast(const Point2f* vertices, const size_t nrVertices, const Poin
if (lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1) {
HitInfo linesHitInfo {};
linesHitInfo.lambda = lambda1;
linesHitInfo.intersectPoint = Point2f { rayP1.x + ( ( rayP2.x - rayP1.x ) * lambda1 ), rayP1.y + ( ( rayP2.y - rayP1.y ) * lambda1 ) };
linesHitInfo.intersectPoint = Vector2f { rayP1.x + ( ( rayP2.x - rayP1.x ) * lambda1 ), rayP1.y + ( ( rayP2.y - rayP1.y ) * lambda1 ) };
linesHitInfo.normal = Vector2f { q2.x - q1.x, q2.y - q2.y }.Orthogonal().Normalized();
hits.push_back(linesHitInfo);
}
@@ -513,7 +513,7 @@ bool utils::Raycast(const Point2f* vertices, const size_t nrVertices, const Poin
return true;
}
bool utils::IsPointOnLineSegment(const Point2f& p, const Point2f& a, const Point2f& b) {
bool utils::IsPointOnLineSegment(const Vector2f& p, const Vector2f& a, const Vector2f& b) {
Vector2f ap { a, p }, bp { b, p };
// If not on same line, return false
if (abs(ap.CrossProduct(bp)) > 0.001f) {
@@ -528,7 +528,7 @@ bool utils::IsPointOnLineSegment(const Point2f& p, const Point2f& a, const Point
return true;
}
float utils::DistPointLineSegment(const Point2f& p, const Point2f& a, const Point2f& b) {
float utils::DistPointLineSegment(const Vector2f& p, const Vector2f& a, const Vector2f& b) {
Vector2f ab { a, b };
Vector2f ap { a, p };
Vector2f abNorm { ab.Normalized() };
@@ -551,7 +551,7 @@ float utils::DistPointLineSegment(const Point2f& p, const Point2f& a, const Poin
return Vector2f { p.x - intersection.x, p.y - intersection.y }.Length();
}
bool utils::IntersectRectLine(const Rectf& r, const Point2f& p1, const Point2f& p2, float& intersectMin, float& intersectMax) {
bool utils::IntersectRectLine(const Rectf& r, const Vector2f& p1, const Vector2f& p2, float& intersectMin, float& intersectMax) {
// Parameters
// input:
// r: axis aligned bounding box, start and end points of line segment.
@@ -566,8 +566,8 @@ bool utils::IntersectRectLine(const Rectf& r, const Point2f& p1, const Point2f&
//float max{};
//if (utils::IntersectRectLine(rect, p1, p2, min, max))
//{
// Point2f intersectP1{ p1 + (Vector2f(p2) - Vector2f(p1)) * min };
// Point2f intersectP2{ p1 + (Vector2f(p2) - Vector2f(p1)) * max };
// Vector2f intersectP1{ p1 + (Vector2f(p2) - Vector2f(p1)) * min };
// Vector2f intersectP2{ p1 + (Vector2f(p2) - Vector2f(p1)) * max };
//}
// 4 floats to convert rect space to line space
@@ -592,14 +592,14 @@ bool utils::IsRectInRect(const Rectf& r1, const Rectf& r2) {
// the origin of both rectangles is in bottom left
return ( r1.left < r2.left + r2.width && r1.left + r1.width > r2.left && r1.bottom < r2.bottom + r2.height && r1.bottom + r1.height > r2.bottom );
}
bool utils::RayVsRect(const Point2f& rayOrigin, const Point2f& rayDir, const Rectf& target,
Point2f& contactPoint, Point2f& contactNormal, float& t_hit_near) {
bool utils::RayVsRect(const Vector2f& rayOrigin, const Vector2f& rayDir, const Rectf& target,
Vector2f& contactPoint, Vector2f& contactNormal, float& t_hit_near) {
// Point2f t_near = Point2f{(target.BottomLeft() - rayOrigin).x / rayDir.x, (target.BottomLeft() - rayOrigin).y / rayDir.y};
// Point2f t_far = Point2f{(target.BottomLeft() + Point2f{target.width, target.height} - rayOrigin).x / rayDir.x, (target.BottomLeft() + Point2f{target.width, target.height} - rayOrigin).y / rayDir.y};
// Vector2f t_near = Vector2f{(target.BottomLeft() - rayOrigin).x / rayDir.x, (target.BottomLeft() - rayOrigin).y / rayDir.y};
// Vector2f t_far = Vector2f{(target.BottomLeft() + Vector2f{target.width, target.height} - rayOrigin).x / rayDir.x, (target.BottomLeft() + Vector2f{target.width, target.height} - rayOrigin).y / rayDir.y};
Point2f t_near {};
Point2f t_far {};
Vector2f t_near {};
Vector2f t_far {};
if (std::isnan(t_far.y) || std::isnan(t_far.x))
return false;
@@ -624,24 +624,24 @@ bool utils::RayVsRect(const Point2f& rayOrigin, const Point2f& rayDir, const Rec
if (t_near.x > t_near.y) {
if (rayDir.x < 0) {
contactNormal = Point2f { 1, 0 };
contactNormal = Vector2f { 1, 0 };
}
else {
contactNormal = Point2f { -1, 0 };
contactNormal = Vector2f { -1, 0 };
}
}
else if (t_near.x < t_near.y) {
if (rayDir.y < 0) {
contactNormal = Point2f { 0, 1 };
contactNormal = Vector2f { 0, 1 };
}
else {
contactNormal = Point2f { 0, -1 };
contactNormal = Vector2f { 0, -1 };
}
}
return true;
}
bool utils::DynamicRectVsRect(const MovingRectf& in, const Rectf& target, Point2f& contactPoint, Point2f& contactNormal, float& contactTime, float dt) {
bool utils::DynamicRectVsRect(const MovingRectf& in, const Rectf& target, Vector2f& contactPoint, Vector2f& contactNormal, float& contactTime, float dt) {
if (in.velocity.x == 0 && in.velocity.y == 0)
return false;
@@ -652,14 +652,14 @@ bool utils::DynamicRectVsRect(const MovingRectf& in, const Rectf& target, Point2
expanded_target.width = target.width + in.width;
expanded_target.height = target.height + in.height;
if (RayVsRect(Point2f { in.bottomLeft.x + in.width / 2, in.bottomLeft.y + in.height / 2 }, in.velocity * dt, expanded_target, contactPoint, contactNormal, contactTime)) {
if (RayVsRect(Vector2f { in.bottomLeft.x + in.width / 2, in.bottomLeft.y + in.height / 2 }, in.velocity * dt, expanded_target, contactPoint, contactNormal, contactTime)) {
if (contactTime <= 1.0f && contactTime >= 0.0f) {
return true;
}
}
return false;
}
float utils::DotProduct(const Point2f& a, const Point2f& b) {
float utils::DotProduct(const Vector2f& a, const Vector2f& b) {
return a.x * b.x + a.y * b.y;
}
#pragma endregion CollisionFunctionality
@@ -667,6 +667,16 @@ float utils::DotProduct(const Point2f& a, const Point2f& b) {
int utils::randRange(int min, int max) {
return min + rand() % ( ( max + 1 ) - min );
}
float utils::lerp(float a, float b, float t) {
return a + t * ( b - a );
}
Vector2f utils::lerp(const Vector2f& a, const Vector2f& b, float t) {
return Vector2f { lerp(a.x, b.x, t), lerp(a.y, b.y, t) };
}
float utils::map(float value, float start1, float stop1, float start2, float stop2) {
float newVal = (value - start1) / (stop1 - start1) * (stop2 - start2) + start2;
return newVal;
}
bool utils::isKeyDown(int keycode) {
const Uint8* pStates = SDL_GetKeyboardState(nullptr);
if (pStates != nullptr) {
@@ -699,11 +709,11 @@ bool utils::isKeyUp(int keycode) {
}
return false;
}
Point2f utils::GetMousePos() {
Vector2f utils::GetMousePos() {
int x, y;
SDL_GetMouseState(&x, &y);
//TODO: make the screen size a global or something
return Point2f { float(x), float(500.f - y) };
return Vector2f { float(x), float(500.f - y) };
}
bool utils::IsMouseButtonDown(int button) {
const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr);
@@ -712,8 +722,8 @@ bool utils::IsMouseButtonDown(int button) {
}
return false;
}
static Point2f ViewportSize{ 900.f, 500.f }; //TODO: somehow move this (Ask teacher)
Point2f utils::GetViewport() {
static Vector2f ViewportSize{ 900.f, 500.f }; //TODO: somehow move this (Ask teacher)
Vector2f utils::GetViewport() {
return ViewportSize;
}