I wrote this in C++/OpenGL, and the collision is kind of bothering me. The corners don't seem to detect collision when rotating the sprite. I'm figuring the math is off a bit.
Code explanation:
m_currentCell = is the current frame in a sprite map. I call it a cell.
spr.getCellWidth() - spr.getCellX() = This just gets the width of the current cell.
m_x, m_y = The sprite's position with (0,0) at the bottom-left corner.
Collision detection using Win32's IntersectRect(). When collision is detected, it currently sets the player back a little in the x position.
bool Sprite::getCollision(const Sprite &spr)
{
RECT rect_a;
RECT rect_b;
RECT rectDest;
// Sprite1's rect (Player)
rect_a.left = m_x;
rect_a.top = m_y;
rect_a.right = rect_a.left + (m_currentCell.width - m_currentCell.x) * m_scale;
rect_a.bottom = rect_a.top + (m_currentCell.height + m_currentCell.y) * m_scale;
// Sprite2's rect (Other sprite)
rect_b.left = spr.getX();
rect_b.top = spr.getY();
rect_b.right = rect_b.left + (spr.getCellWidth() - spr.getCellX()) * m_scale;
rect_b.bottom = rect_b.top + (spr.getCellHeight() - spr.getCellY()) * m_scale;
if (IntersectRect(&rectDest, &rect_a, &rect_b))
{
return true;
}
return false;
}
Video example: