feat: add more shape distances
This commit is contained in:
@@ -39,6 +39,14 @@ bool Rectangle::collidesWith(const Circle& circle) const {
|
||||
return circle.collidesWith(*this);
|
||||
}
|
||||
|
||||
float Rectangle::distance(const Circle& circle) const {
|
||||
return circle.distance(*this);
|
||||
}
|
||||
|
||||
float Rectangle::distanceSquared(const Circle& circle) const {
|
||||
return circle.distanceSquared(*this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -48,21 +56,29 @@ bool Circle::collidesWith(const Point& point) const {
|
||||
}
|
||||
|
||||
bool Circle::collidesWith(const Rectangle& rect) const {
|
||||
float closestX = std::clamp(m_Center.getX(), rect.getTopLeft().getX(), rect.getBottomRight().getX());
|
||||
float closestY = std::clamp(m_Center.getY(), rect.getTopLeft().getY(), rect.getBottomRight().getY());
|
||||
float distanceSquared_ = distanceSquared(rect);
|
||||
|
||||
float distanceX = m_Center.getX() - closestX;
|
||||
float distanceY = m_Center.getY() - closestY;
|
||||
|
||||
float distanceSquared = (distanceX * distanceX) + (distanceY * distanceY);
|
||||
|
||||
return distanceSquared < m_Radius * m_Radius;
|
||||
return distanceSquared_ < m_Radius* m_Radius;
|
||||
}
|
||||
|
||||
bool Circle::collidesWith(const Circle& circle) const {
|
||||
return m_Radius + circle.getRadius() > m_Center.distanceSquared(circle.getCenter());
|
||||
}
|
||||
|
||||
float Circle::distance(const Rectangle& rect) const {
|
||||
return std::sqrt(distanceSquared(rect));
|
||||
}
|
||||
|
||||
float Circle::distanceSquared(const Rectangle& rect) const {
|
||||
float closestX = std::clamp(m_Center.getX(), rect.getTopLeft().getX(), rect.getBottomRight().getX());
|
||||
float closestY = std::clamp(m_Center.getY(), rect.getTopLeft().getY(), rect.getBottomRight().getY());
|
||||
|
||||
float distanceX = m_Center.getX() - closestX;
|
||||
float distanceY = m_Center.getY() - closestY;
|
||||
|
||||
return (distanceX * distanceX) + (distanceY * distanceY);
|
||||
}
|
||||
|
||||
} // namespace shape
|
||||
} // namespace utils
|
||||
} // namespace td
|
||||
|
||||
Reference in New Issue
Block a user