Jump to content
  • Advertisement
Sign in to follow this  
shinypixel

How to fix collisions at rectangle corners when rotating?

This topic is 1671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

Edited by shinypixel

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!