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

Author Topic: Collision... yer driving me crazy  (Read 3591 times)

0 Members and 2 Guests are viewing this topic.

UglyIgloo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Collision... yer driving me crazy
« 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!

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Collision... yer driving me crazy
« Reply #1 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)

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Collision... yer driving me crazy
« Reply #2 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
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

UglyIgloo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Collision... yer driving me crazy
« Reply #3 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision... yer driving me crazy
« Reply #4 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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

UglyIgloo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Collision... yer driving me crazy
« Reply #5 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision... yer driving me crazy
« Reply #6 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

UglyIgloo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Collision... yer driving me crazy
« Reply #7 on: September 01, 2014, 01:50:01 am »
Have any suggestions for fixing it?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision... yer driving me crazy
« Reply #8 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

UglyIgloo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Collision... yer driving me crazy
« Reply #9 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.

« Last Edit: September 12, 2014, 03:31:15 am by UglyIgloo »