Not really relevant, but this might save some typing in some cases.
Instead of:
Code: Select all
centerA.x < centerB.x ? xOverlap = A.Max().x - B.Min().x : xOverlap = B.Max().x - A.Min().x;
centerA.y < centerB.y ? yOverlap = A.Max().y - B.Min().y : yOverlap = B.Max().y - A.Min().y;
correction = Vec2f(xOverlap, yOverlap);
if (m_player.Type() == _EntityType::player)
{
dir.y > 0 ? m_player.SetState(EntityStates::idle) : m_player.SetState(EntityStates::jumping);
}
You could use the fact that the ternary operator can return values, not just choose a path of execution
Code: Select all
correction.x = centerA.x < centerB.x ? A.Max().x - B.Min().x : B.Max().x - A.Min().x;
correction.y = centerA.y < centerB.y ? A.Max().y - B.Min().y : B.Max().y - A.Min().y;
if (m_player.Type() == _EntityType::player)
{
m_player.SetState( dir.y > 0 ? EntityStates::idle : EntityStates::jumping );
// or
// const auto state = dir.y > 0 ? EntityState::idle : EntityState::jumping;
// m_player.SetState( state );
}
This has the benefit of making your variables const when initializing them.
This:
Code: Select all
bool Intersect::BoundingBox(const RectF& A, const RectF& B, Vec2f& correction)
{
float xOverlap = 0.0f, yOverlap = 0.0f;
correction = { 0.0f,0.0f };
// axis are out of any possible collision ?
bool axisX = (A.Max().x <= B.Min().x || B.Max().x <= A.Min().x);
bool axisY = (A.Max().y <= B.Min().y || B.Max().y <= A.Min().y);
if (axisX || axisY)
return false;
Vec2f centerA = Vec2f((A.Min().x + A.Max().x) / 2.0f, (A.Min().y + A.Max().y) / 2.0f);
Vec2f centerB = Vec2f((B.Min().x + B.Max().x) / 2.0f, (B.Min().y + B.Max().y) / 2.0f);
centerA.x < centerB.x ? xOverlap = A.Max().x - B.Min().x :
xOverlap = B.Max().x - A.Min().x;
centerA.y < centerB.y ? yOverlap = A.Max().y - B.Min().y :
yOverlap = B.Max().y - A.Min().y;
correction = Vec2f(xOverlap, yOverlap);
return true;
}
Becomes:
Code: Select all
bool Intersect::BoundingBox(const RectF& A, const RectF& B, Vec2f& correction)
{
// axis are out of any possible collision ?
const bool axisX = (A.Max().x <= B.Min().x || B.Max().x <= A.Min().x);
const bool axisY = (A.Max().y <= B.Min().y || B.Max().y <= A.Min().y);
if (axisX || axisY)
{
correction = { 0.0f,0.0f };
return false;
}
const Vec2f centerA = Vec2f((A.Min().x + A.Max().x) / 2.0f, (A.Min().y + A.Max().y) / 2.0f);
const Vec2f centerB = Vec2f((B.Min().x + B.Max().x) / 2.0f, (B.Min().y + B.Max().y) / 2.0f);
correction = Vec2f(
centerA.x < centerB.x ? A.Max().x - B.Min().x : B.Max().x - A.Min().x,
centerA.y < centerB.y ? A.Max().y - B.Min().y : B.Max().y - A.Min().y
);
return true;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com