Created
February 25, 2019 18:30
-
-
Save eeshangarg/77b80df07fe584a0fea84e3a381f1237 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Physics.h" | |
#include "Components.h" | |
Vec2 Physics::GetOverlap(std::shared_ptr<Entity> a, std::shared_ptr<Entity> b) | |
{ | |
auto x1 = a->getComponent<CTransform>()->pos.x; | |
auto y1 = a->getComponent<CTransform>()->pos.y; | |
auto x2 = b->getComponent<CTransform>()->pos.x; | |
auto y2 = b->getComponent<CTransform>()->pos.y; | |
Vec2 delta(abs(x1-x2), abs(y1-y2)); | |
auto w1 = a->getComponent<CBoundingBox>()->halfSize.x; | |
auto h1 = a->getComponent<CBoundingBox>()->halfSize.y; | |
auto w2 = b->getComponent<CBoundingBox>()->halfSize.x; | |
auto h2 = b->getComponent<CBoundingBox>()->halfSize.y; | |
auto ox = w1 + w2 - delta.x; | |
auto oy = h1 + h2 - delta.y; | |
return Vec2(ox, oy); | |
} | |
Vec2 Physics::GetPreviousOverlap(std::shared_ptr<Entity> a, std::shared_ptr<Entity> b) | |
{ | |
auto x1 = a->getComponent<CTransform>()->prevPos.x; | |
auto y1 = a->getComponent<CTransform>()->prevPos.y; | |
auto x2 = b->getComponent<CTransform>()->prevPos.x; | |
auto y2 = b->getComponent<CTransform>()->prevPos.y; | |
Vec2 delta(abs(x1-x2), abs(y1-y2)); | |
auto w1 = a->getComponent<CBoundingBox>()->halfSize.x; | |
auto h1 = a->getComponent<CBoundingBox>()->halfSize.y; | |
auto w2 = b->getComponent<CBoundingBox>()->halfSize.x; | |
auto h2 = b->getComponent<CBoundingBox>()->halfSize.y; | |
auto ox = w1 + w2 - delta.x; | |
auto oy = h1 + h2 - delta.y; | |
return Vec2(ox, oy); | |
} | |
Intersect Physics::LineIntersect(const Vec2 & a, const Vec2 & b, const Vec2 & c, const Vec2 & d) | |
{ | |
Vec2 r = (b - a); | |
Vec2 s = (d - c); | |
float rxs = r * s; | |
Vec2 cma = c - a; | |
float t = (cma * s) / rxs; | |
float u = (cma * r) / rxs; | |
if (t >= 0.0 && t <= 1.0 && u >= 0.0 && u <= 1.0) | |
return { true, Vec2(a.x + t*r.x, a.y + t*r.y) }; | |
else | |
return { false, Vec2(0.0, 0.0) }; | |
} | |
bool Physics::EntityIntersect(const Vec2 & a, const Vec2 & b, std::shared_ptr<Entity> e) | |
{ | |
Vec2 halfSize = e->getComponent<CBoundingBox>()->halfSize; | |
Vec2 pos = e->getComponent<CTransform>()->pos; | |
Vec2 c = Vec2(pos.x - halfSize.x, pos.y - halfSize.y); | |
Vec2 d = Vec2(pos.x + halfSize.x, c.y); | |
Intersect i1 = LineIntersect(a, b, c, d); | |
d = Vec2(c.x, pos.y + halfSize.y); | |
Intersect i2 = LineIntersect(a, b, c, d); | |
c = Vec2(pos.x + halfSize.x, pos.y - halfSize.y); | |
d = Vec2(c.x, pos.y + halfSize.y); | |
Intersect i3 = LineIntersect(a, b, c, d); | |
c = Vec2(pos.x - halfSize.x, pos.y + halfSize.y); | |
d = Vec2(pos.x + halfSize.x, c.y); | |
Intersect i4 = LineIntersect(a, b, c, d); | |
return i1.result || i2.result || i3.result || i4.result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment