Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Good Collision detection technique?  (Read 2032 times)

0 Members and 1 Guest are viewing this topic.

Vita

  • Newbie
  • *
  • Posts: 6
    • View Profile
Good Collision detection technique?
« on: April 19, 2011, 03:20:20 pm »
Hi,

I was creating some sort of game and I got a problem with my collision detection technique. Here is how I check the collision:
Code: [Select]

bool f_CheckCollision(c_Object A, c_Object B)
{
float leftA, leftB;
float rightA, rightB;
float topA, topB;
float bottomA, bottomB;
leftA = A.x;
leftB = B.x;
rightA = A.x + A.w;
rightB = B.x + B.w;
topA = A.y;
topB = B.y;
bottomA = A.y + A.h;
bottomB = B.y + B.h;
//If any of the sides from A are outside of B
    if( bottomA <= topB )
    {
        return false;
    }
   
    if( topA >= bottomB )
    {
        return false;
    }
   
    if( rightA <= leftB )
    {
        return false;
    }
   
    if( leftA >= rightB )
    {
        return false;
    }
   
    //If none of the sides from A are outside B
    return true;
}


It works ok If my object goes straight on X, or Y but when I press two arrows like UP and DOWN and my object goes trough the wall.
So is there a better way of checking collision between two objects but does not use much of CPU?

Thanks in advance!
SFML is the best! :)

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Good Collision detection technique?
« Reply #1 on: April 19, 2011, 04:39:54 pm »
You need to eliminate the players ability to press up and down at the same time, well at least deal with it if they do. In my project I just cancel any up or down movement if both keys are pressed.

I chose a similar box style detection technique for bouncing my sprite off of the target object. It only worked for head-on collisions. Coming at the object at an angle made me bounce back in the same direction I was travelling.

At the point of collision you'd also need to evaluate the x/y speeds to determine which direction you're travelling. eg. Xnegative & Ynegative is up and to the left. Once you know that the correct reaction to the collision can be made.

Don't know if that made sense, I hope so!
{much better code}

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Good Collision detection technique?
« Reply #2 on: April 20, 2011, 09:07:09 am »
sf::Rect::Intersect handles detecting intersecting rectangles for you.

Rect (SFML 1.6)
Rect (SFML 2.0)



SFML 2.0 implementation
Code: [Select]
template <typename T>
bool Rect<T>::Intersects(const Rect<T>& rectangle, Rect<T>& intersection) const
{
    // Compute the intersection boundaries
    T left   = std::max(Left,         rectangle.Left);
    T top    = std::max(Top,          rectangle.Top);
    T right  = std::min(Left + Width, rectangle.Left + rectangle.Width);
    T bottom = std::min(Top + Height, rectangle.Top + rectangle.Height);

    // If the intersection is valid (positive non zero area), then there is an intersection
    if ((left < right) && (top < bottom))
    {
        intersection = Rect<T>(left, top, right - left, bottom - top);
        return true;
    }
    else
    {
        intersection = Rect<T>(0, 0, 0, 0);
        return false;
    }
}

 

anything