7#include "../types/vector2.hpp"
8#include "../types/vector3.hpp"
9#include "../util/color.hpp"
11#include "../engine/components/transform_component.hpp"
40 float dx = point.
x -
pos.
x;
41 float dy = point.
y -
pos.
y;
43 float radians = -rot.
z * 3.14159265f / 180.0f;
44 float c = std::cos(radians);
45 float s = std::sin(radians);
47 float localX = dx * c - dy * s;
48 float localY = dx * s + dy * c;
50 float hw = (rect.
width * scl.
x) / 2.0f;
51 float hh = (rect.
height * scl.
y) / 2.0f;
53 return (localX >= -hw && localX <= hw) &&
54 (localY >= -hh && localY <= hh);
77 float hw = (r.
width * scl.
x) / 2.0f;
78 float hh = (r.
height * scl.
y) / 2.0f;
79 float rad = rot.
z * 3.14159265f / 180.0f;
80 float c = std::cos(rad), s = std::sin(rad);
82 auto rotate = [&](
float x,
float y) ->
Vector2 {
83 return {
pos.
x + x*c - y*s,
pos.
y + x*s + y*c };
86 return std::array<Vector2, 4>{
87 rotate(-hw, -hh), rotate( hw, -hh),
88 rotate( hw, hh), rotate(-hw, hh)
92 auto corners1 = getCorners(r1, t1);
93 auto corners2 = getCorners(r2, t2);
96 auto testAxis = [](
const Vector2& axis,
97 const std::array<Vector2, 4>& c1,
98 const std::array<Vector2, 4>& c2) ->
bool {
99 float min1 = FLT_MAX, max1 = -FLT_MAX;
100 float min2 = FLT_MAX, max2 = -FLT_MAX;
103 float p = v.x * axis.
x + v.y * axis.
y;
104 min1 = std::min(min1, p); max1 = std::max(max1, p);
107 float p = v.x * axis.
x + v.y * axis.
y;
108 min2 = std::min(min2, p); max2 = std::max(max2, p);
111 return max1 >= min2 && max2 >= min1;
116 float rad1 = rot1.
z * 3.14159265f / 180.0f;
117 float rad2 = rot2.z * 3.14159265f / 180.0f;
119 std::array<Vector2, 4> axes = {{
120 { std::cos(rad1), std::sin(rad1) },
121 {-std::sin(rad1), std::cos(rad1) },
122 { std::cos(rad2), std::sin(rad2) },
123 {-std::sin(rad2), std::cos(rad2) }
126 for (
auto& axis : axes) {
127 if (!testAxis(axis, corners1, corners2))
142 float halfW = rect.width / 2.0f;
143 float halfH = rect.height / 2.0f;
144 return (vec.x >= rect.pos.x - halfW && vec.x <= rect.pos.x + halfW) &&
145 (vec.y >= rect.pos.y - halfH && vec.y <= rect.pos.y + halfH);
163 return (std::abs(r1.pos.x - r2.pos.x) < (r1.width + r2.width) / 2.0f) &&
164 (std::abs(r1.pos.y - r2.pos.y) < (r1.height + r2.height) / 2.0f);
Definition audiomanager.cpp:4
constexpr bool operator>>(const Vector2 &vec, const Rectangle &rect) noexcept
Point-in-rectangle test (axis-aligned, untransformed).
Definition rectangle.hpp:141
RGBA color stored as normalized floating-point components.
Definition color.hpp:12
An axis-aligned rectangle with a position, size and color.
Definition rectangle.hpp:21
Color color
Fill color.
Definition rectangle.hpp:25
Vector3 pos
Center position (z is the layer).
Definition rectangle.hpp:22
static bool containsPoint(const Rectangle &rect, const Transform &transform, const Vector2 &point)
Tests whether a point lies inside a (possibly transformed) rectangle.
Definition rectangle.hpp:34
float width
Width before scaling.
Definition rectangle.hpp:23
float height
Height before scaling.
Definition rectangle.hpp:24
static bool overlaps(const Rectangle &r1, const Transform &t1, const Rectangle &r2, const Transform &t2)
Tests whether two (possibly transformed) rectangles overlap.
Definition rectangle.hpp:68
Two-component floating-point vector.
Definition vector2.hpp:13
float x
Definition vector2.hpp:14
float y
Vector components.
Definition vector2.hpp:14
Three-component floating-point vector.
Definition vector3.hpp:13
float y
Definition vector3.hpp:14
float z
Vector components.
Definition vector3.hpp:14
float x
Definition vector3.hpp:14