12 #include <SFML/Graphics/Transform.hpp> 13 #include <SFML/System/Vector2.hpp> 38 std::vector<sf::Vector2f> m_arr;
39 sf::FloatRect m_globalBounds;
41 static bool nonnegatively_dimensional_rect_intersection(sf::FloatRect lhs, sf::FloatRect rhs) {
42 lhs.top = std::abs(lhs.top);
43 lhs.left = std::abs(lhs.left);
45 rhs.top = std::abs(rhs.top);
46 rhs.left = std::abs(rhs.left);
48 if(lhs.left > rhs.left + rhs.width) {
51 if(lhs.left + lhs.width < rhs.left) {
54 if(lhs.top > rhs.top + rhs.height) {
57 if(lhs.top + lhs.height < rhs.top) {
64 void updateGlobalBounds() {
65 sf::Vector2f max {0, 0};
66 sf::Vector2f pos {m_arr.empty() ? sf::Vector2f{0, 0} : m_arr[0]};
68 for(
const sf::Vector2f& pt: m_arr) {
69 max.x = pt.x > max.x ? pt.x : max.x;
70 max.y = pt.y > max.y ? pt.y : max.y;
72 pos.x = pt.x < pos.x ? pt.x : pos.x;
73 pos.y = pt.y < pos.y ? pt.y : pos.y;
76 m_globalBounds.top = pos.y;
77 m_globalBounds.left = pos.x;
78 m_globalBounds.width = max.x - pos.x;
79 m_globalBounds.height = max.y - pos.y;
115 static bool lineIntersection(sf::Vector2f p1, sf::Vector2f q1, sf::Vector2f p2, sf::Vector2f q2);
152 return m_globalBounds;
169 bool contains(sf::Vector2f point)
const;
182 for(
auto i = 0u; i < cnt; ++i) {
184 std::sin(6.28 / cnt * i) * radius + radius,
185 std::cos(6.28 / cnt * i) * radius + radius
202 auto isOnSegment = [](sf::Vector2f p, sf::Vector2f q, sf::Vector2f r) {
203 return (q.x <= std::max(p.x, r.x) && q.x >= std::min(p.x, r.x) &&
204 q.y <= std::max(p.y, r.y) && q.y >= std::min(p.y, r.y));
206 auto orientation = [](sf::Vector2f p, sf::Vector2f q, sf::Vector2f r) {
207 int val = (q.y - p.y) * (r.x - q.x) -
208 (q.x - p.x) * (r.y - q.y);
214 return (val > 0) ? 1 : 2;
219 int o1 = orientation(p1, q1, p2);
220 int o2 = orientation(p1, q1, q2);
221 int o3 = orientation(p2, q2, p1);
222 int o4 = orientation(p2, q2, q1);
225 if (o1 != o2 && o3 != o4)
229 if ((o1 == 0 && isOnSegment(p1, p2, q1))
230 || (o2 == 0 && isOnSegment(p1, q2, q1))
231 || (o3 == 0 && isOnSegment(p2, p1, q2))
232 || (o4 == 0 && isOnSegment(p2, q1, q2))) {
244 [&t](sf::Vector2f p) {
245 return t.transformPoint(p);
249 updateGlobalBounds();
253 updateGlobalBounds();
257 updateGlobalBounds();
262 || poly.m_arr.empty()
268 arr.push_back(arr.front());
270 auto poly_arr = poly.m_arr;
271 poly_arr.push_back(poly_arr.front());
273 for(
auto i = 1u; i < arr.size(); ++i) {
274 auto p1 = arr[i - 1], q1 = arr[i];
276 for(
auto j = 1u; j < poly_arr.size(); ++j) {
277 auto p2 = poly_arr[j - 1], q2 = poly_arr[j];
288 int i, j, nvert = m_arr.size();
291 for(i = 0, j = nvert - 1; i < nvert; j = i++) {
295 if(((pti.y >= point.y) != (ptj.y >= point.y)) &&
296 (point.x <= (ptj.x - pti.x) * (point.y - pti.y) / (ptj.y - pti.y) + pti.x)) {
313 for(
auto v: poly.m_arr) {
323 #endif // COLLIDER_HPP static bool lineIntersection(sf::Vector2f p1, sf::Vector2f q1, sf::Vector2f p2, sf::Vector2f q2)
Check two lines for intersection.
Definition: Collider.hpp:201
static Collider rectangle(sf::Vector2f size)
Returns collider in shape of rectangle.
Definition: Collider.hpp:191
Contains all SFGF classes.
void clear()
Clears points.
Definition: Collider.hpp:255
static Collider circle(float radius, size_t cnt=128)
Returns collider in shape of circle.
Definition: Collider.hpp:180
bool intersects(const Collider &poly) const
Check for intersection.
Definition: Collider.hpp:260
void applyTransform(const sf::Transform &t)
Applies transform.
Definition: Collider.hpp:239
bool collides(const Collider &poly) const
Check if two colliders collide.
Definition: Collider.hpp:304
Handles collisions.
Definition: Collider.hpp:37
void pushBack(sf::Vector2f pt)
Adds point to internal array.
Definition: Collider.hpp:251
sf::FloatRect getGlobalBounds() const
Returns global bounds.
Definition: Collider.hpp:151
bool contains(sf::Vector2f point) const
Check if point is inside of collider.
Definition: Collider.hpp:287