Skip to content

Commit eb1ae41

Browse files
committed
vec2: minor refactoring
1 parent f6ff471 commit eb1ae41

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

twinhook/util/vec2.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,13 @@ bool vec2::isContainAABB(const vec2& p1, const vec2& p2, const vec2& s1, const v
256256
float vec2::willCollideAABB(const vec2& p1, const vec2& p2, const vec2& s1, const vec2& s2,
257257
const vec2& v1, const vec2& v2)
258258
{
259-
// BUG this implementation is way too inefficient
259+
// TODO this implementation is way too inefficient
260260

261261
// check if they're already colliding
262262
if (isCollideAABB(p1, p2, s1, s2))
263263
return 0;
264264

265-
// check if they're moving away from each other
266-
// actually, this code doesn't work
267-
/*if (p1.x < p2.x && v2.x > v1.x || p1.x > p2.x && v2.x < v1.x)
268-
return -1;
269-
if (p1.y < p2.y && v2.y > v1.y || p1.y > p2.y && v2.y < v1.y)
270-
return -1;*/
271-
272-
// check time required until collision for each side
265+
// check time required until collision for each side
273266
float t = (p1.x - p2.x - s2.x) / (v2.x - v1.x);
274267
float minE = FLT_MAX;
275268
if (t >= 0 && isCollideAABB(p1 + t * v1, p2 + t * v2, s1, s2))
@@ -297,7 +290,6 @@ float vec2::willExitAABB(const vec2& p1, const vec2& p2, const vec2& s1, const v
297290
return 0;
298291

299292
// check time required for each side to exit
300-
301293
float minE = FLT_MAX;
302294
float t = (p1.x + s1.x - p2.x - s2.x) / (v2.x - v1.x);
303295
if (t >= 0)
@@ -418,8 +410,8 @@ float vec2::willCollideSAT(const std::vector<vec2>& a, const vec2 &va,
418410
const std::vector<vec2>& b, const vec2 &vb)
419411
{
420412
std::set<vec2> normals;
421-
int sizeA = a.size();
422-
int sizeB = b.size();
413+
const int sizeA = a.size();
414+
const int sizeB = b.size();
423415

424416
// calculate normals
425417
for (int i = 0; i < sizeA; ++i)
@@ -435,9 +427,6 @@ float vec2::willCollideSAT(const std::vector<vec2>& a, const vec2 &va,
435427
float minProjA = FLT_MAX, maxProjA = -FLT_MAX;
436428
float minProjB = FLT_MAX, maxProjB = -FLT_MAX;
437429

438-
float vAxisA = dot(va, n);
439-
float vAxisB = dot(vb, n);
440-
441430
// determine extents of projections onto axis
442431
for (const vec2& pa : a)
443432
{
@@ -452,7 +441,8 @@ float vec2::willCollideSAT(const std::vector<vec2>& a, const vec2 &va,
452441
maxProjB = std::max(maxProjB, pj);
453442
}
454443

455-
auto interval = willOverlapInterval(minProjA, maxProjA, vAxisA, minProjB, maxProjB, vAxisB);
444+
auto interval = willOverlapInterval(minProjA, maxProjA, dot(va, n),
445+
minProjB, maxProjB, dot(vb, n));
456446
currentInterval = intersectInterval(interval, currentInterval);
457447

458448
// i.e. there exists an axis that is always separating
@@ -471,7 +461,7 @@ bool vec2::isOverlapInterval(float minA, float maxA, float minB, float maxB)
471461
return !(minB < minA && maxB < minA || minB > maxA && maxB > maxA);
472462
}
473463

474-
std::pair<float, float> vec2::willOverlapInterval(float minA, float maxA, float va,
464+
std::pair<float, float> vec2::willOverlapInterval(float minA, float maxA, float va,
475465
float minB, float maxB, float vb)
476466
{
477467
// check if intervals are not moving relative to each other

0 commit comments

Comments
 (0)