SFML community forums

Help => General => Topic started by: UglyIgloo on August 29, 2014, 05:51:58 am

Title: Collision... yer driving me crazy
Post by: UglyIgloo on August 29, 2014, 05:51:58 am
void player::collision(mob M)
{

        //horizontal
        if(right >= M.left && top < M.bottom && bottom > M.top && right < M.right)
        {
                rect.setPosition(M.left - rect.getSize().x, rect.getPosition().y);
        }
        if(left <= M.right && top < M.bottom && bottom > M.top && left > M.left)
        {
                rect.setPosition(M.right, rect.getPosition().y);
        }
}


Hi, I'm trying to create a solid bounding box style collision system for my game. So far, I can tell whether or not the horizontal planes intersect, but whenever I attempt at detecting whether or not they intersect vertically, the horizontal detection overrides it no matter what order I program it in.


(Also, here are the directional variable if necessary)


void player::update()
{
        bottom = rect.getPosition().y + rect.getSize().y;
        left = rect.getPosition().x;
        right = rect.getPosition().x + rect.getSize().x;
        top = rect.getPosition().y;
}

I was wondering if someone could point me in the right direction. Thanks!
Title: Re: Collision... yer driving me crazy
Post by: smguyk on August 29, 2014, 08:19:06 am
Can't you just use intersects() and set the rect's new position based on its x/y velocities?

if (rect.getGlobalBounds().intersects(M.getGlobalBounds())) {
  // set position based on current velocity
}

and player::collision's parameter should be (mob &M)
Title: Re: Collision... yer driving me crazy
Post by: AlexxanderX on August 29, 2014, 08:50:42 am
Also you can get an rect with the intersection:
sf::FloatRect intersection;
if (rect.getGlobalBounds().intersects(M.getGlobalBounds(), intersection)) {
  // set new position based on the intersection rect
}

You can read more in my tutorial (http://alexanderx.net/how-apply-collision/)
Title: Re: Collision... yer driving me crazy
Post by: UglyIgloo on August 29, 2014, 06:34:21 pm
Doesn't Intersect() just tell you whether or not they're colliding? I'm trying to make the edges collide by preventing intersection.
Title: Re: Collision... yer driving me crazy
Post by: Hapax on August 29, 2014, 07:54:35 pm
Hi, I'm trying to create a solid bounding box style collision system for my game. So far, I can tell whether or not the horizontal planes intersect, but whenever I attempt at detecting whether or not they intersect vertically, the horizontal detection overrides it no matter what order I program it in.
Could we see the vertical detection code and where you put it?
Title: Re: Collision... yer driving me crazy
Post by: UglyIgloo on August 31, 2014, 01:55:51 am
Hi, thanks for responding. Sadly, I'm on mobile right now and will probably stay in mobile for about a week because I just moved. The most I can say is that the vertical code is literally just the horizontal code with the variables switched. I can confirm that the variable are right because when I remove the horizontal code, it works fine.
Title: Re: Collision... yer driving me crazy
Post by: Hapax on August 31, 2014, 09:16:44 pm
It looks like this:
When the horizontal detection is found, it corrects the collision by moving it so that it no longer collides.
Then, when the vertical detection goes ahead, it's no longer colliding, even if it did before.

However, if you put the vertical code first - the logic and fix is identical to the horizontal code - and it still skips the vertical detection, then it can't be this. I bolded "fix" because if the vertical code finds that it collides but fixes it the same way the horizontal code does, it would look like the horizontal code did it.
Title: Re: Collision... yer driving me crazy
Post by: UglyIgloo on September 01, 2014, 01:50:01 am
Have any suggestions for fixing it?
Title: Re: Collision... yer driving me crazy
Post by: Hapax on September 01, 2014, 02:12:14 am
If it is that - and I'm guessing because I can't actually see the code - then you'll need to do all testing first, then do all the fixes afterwards.
Title: Re: Collision... yer driving me crazy
Post by: UglyIgloo on September 12, 2014, 03:22:00 am
Thanks for the help so far. I recently got back my internet connection and I've made some major alterations to my collision code while I was away.

bool player::collision(entity* cEntity)
{
        if(right < cEntity->left || left > cEntity->right ||
                 top > cEntity->bottom || bottom < cEntity->top )
        {
                return false;
        }

        velocity = -velocity;

        return true;
}

When a player presses the WSAD keys, their sf::Vector2f velocity changes to a certain value. The game is constantly updating to move() the player based on their velocity vector.

Right now, one major issue I'm experiencing with this code is that it will make sure the players controls are reversed until it is no longer intersecting with CEntity. I just can't wrap my head around collision response and I don't know how to use Box2D.


My collision code is loosely based off of this tutorial by CodingMadeEasy.

http://www.youtube.com/watch?v=n0U-NBmLj78