How to fix collisions at rectangle corners when rotating?

Started by
3 comments, last by DiegoSLTS 9 years, 11 months ago

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:

Advertisement

It looks like rect_a and rect_b are not being rotated before the collision check, and I guess that the rectangles around those characters are not rect_a and rect_b. Try drawing those rectangles in the screen and you'll see when the collision is actually happening.

It's the same rects. I think the issue is I'm using glRotatef(), which is done in hardware and I'm not currently keeping track of the new dimensions in software, not do I have any idea of where to begin with that haha, though I do have its angle in software.
Would cos(angle) and sin(angle) go in each rect's right and bottom?

I've never used the Win32 API, but it looks like the RECT structure doesn't let you define a rotated rectangle, you can only set the position of the corners, the sides of the rectangle will always be oriented horizontally or vertically. If you add cos(angle) or sin(angle) to any of those values you'll only get bigger or smaller rectangles since you'll only be moving one of the corners.

You might need to define other structure and a function to check the intersection of two of those structures. To define a rectangle anywhere with any orientation you need at 5 values (center x, center y, height, width, angle) or the position of two oposite corners and an angle.

For the intersection you can do it in lots of ways, but one could be the answer of this wuestion http://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles (note it's the first google result I found, it looks really simple but maybe you like other)

This topic is closed to new replies.

Advertisement