| #pragma once |
|
|
| #include "utils.hpp" |
| #include <madrona/components.hpp> |
| #include <madrona/math.hpp> |
|
|
| namespace madrona_gpudrive { |
|
|
| |
| |
| struct OrientedBoundingBox2D { |
| static OrientedBoundingBox2D from(const madrona::base::Position &position, |
| const madrona::base::Rotation &rotation, |
| const madrona::base::Scale &scale) { |
| float theta{utils::quatToYaw(rotation)}; |
| madrona::math::Vector2 X{cosf(theta), sinf(theta)}; |
| madrona::math::Vector2 Y{-sinf(theta), cosf(theta)}; |
|
|
| X *= scale.d0; |
| Y *= scale.d1; |
|
|
| madrona::math::Vector2 center{.x = position.x, .y = position.y}; |
|
|
| OrientedBoundingBox2D obb; |
| obb.corners[0] = center - X - Y; |
| obb.corners[1] = center + X - Y; |
| obb.corners[2] = center + X + Y; |
| obb.corners[3] = center - X + Y; |
|
|
| obb.updateAxes(); |
|
|
| return obb; |
| } |
| static bool hasCollided(const OrientedBoundingBox2D &obbA, |
| const OrientedBoundingBox2D &obbB) { |
| return obbA.overlaps(obbB) && obbB.overlaps(obbA); |
| } |
|
|
| void updateAxes() { |
| axes[0] = corners[1] - corners[0]; |
| axes[1] = corners[3] - corners[0]; |
|
|
| |
| |
|
|
| for (int a = 0; a < 2; ++a) { |
| axes[a] /= axes[a].length2(); |
| origin[a] = corners[0].dot(axes[a]); |
| } |
| } |
| bool overlaps(const OrientedBoundingBox2D &other) const { |
| for (int a = 0; a < 2; ++a) { |
| float t = other.corners[0].dot(axes[a]); |
|
|
| |
| float tMin = t; |
| float tMax = t; |
|
|
| for (int c = 1; c < 4; ++c) { |
| t = other.corners[c].dot(axes[a]); |
|
|
| if (t < tMin) { |
| tMin = t; |
| } else if (t > tMax) { |
| tMax = t; |
| } |
| } |
|
|
| |
|
|
| |
| if ((tMin > 1 + origin[a]) || (tMax < origin[a])) { |
| |
| |
| return false; |
| } |
| } |
|
|
| |
| |
| return true; |
| } |
|
|
| |
| madrona::math::Vector2 corners[4]; |
|
|
| |
| madrona::math::Vector2 axes[2]; |
|
|
| |
| float origin[2]; |
| }; |
|
|
| } |
|
|