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

Author Topic: C++ SFML Collision Bug  (Read 2141 times)

0 Members and 1 Guest are viewing this topic.

Personwwww

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
C++ SFML Collision Bug
« on: May 11, 2020, 07:29:49 pm »
I wrote some collision code that responds to collisions. So, I put it in a function. Then, the response to the collision stopped working but the collision detection still worked.

P.S OG_INFO means printf.
#include "Collision.h"

void OpenGEC::Collision::AddCollider(Sprite& sprite, Sprite& _sprite)
{

    if (sprite.getGlobalBounds().intersects(_sprite.getGlobalBounds())) {
        Vector2f pos_ = _sprite.getPosition();
        Vector2f pos = sprite.getPosition();
        if (pos.y > pos_.y) {
            pos.y++;
            sprite.setPosition(pos);
            _sprite.setPosition(pos_);
            OG_INFO("{0},{1}", pos.x, pos.y);
        }
        else if (pos.y < pos_.y) {
            pos.y--;
            sprite.setPosition(pos);
            _sprite.setPosition(pos_);
            OG_INFO("{0},{1}", pos.x, pos.y);
        }
        else if (pos.x > pos_.x) {
            pos.x++;
            sprite.setPosition(pos);
            _sprite.setPosition(pos_);
            OG_INFO("{0},{1}", pos.x, pos.y);
        }
        else if (pos.x < pos_.x) {
            pos.x --;
            sprite.setPosition(pos);
            _sprite.setPosition(pos_);
            OG_INFO("{0},{1}", pos.x, pos.y);
        }
    }
}

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: C++ SFML Collision Bug
« Reply #1 on: May 11, 2020, 09:38:51 pm »
What doesn't work exactly? It's still detecting the collision fine?

What you are doing with pos and the unit increments, we can only guess without more information.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Personwwww

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: C++ SFML Collision Bug
« Reply #2 on: May 12, 2020, 12:02:05 am »
What doesn't work exactly? It's still detecting the collision fine?

What you are doing with pos and the unit increments, we can only guess without more information.

The collision detection is working fine but I am stuck on how to react.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: C++ SFML Collision Bug
« Reply #3 on: May 12, 2020, 12:37:41 am »
How do you want it to react? How is it currently reacting?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Personwwww

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: C++ SFML Collision Bug
« Reply #4 on: May 12, 2020, 12:48:24 am »
It barely is reacting at all. I want it to react by stopping the object that is colliding.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: C++ SFML Collision Bug
« Reply #5 on: May 12, 2020, 01:44:58 am »
They are both colliding. I presume you want to 'stop' the first one when it collides with the second?

To stop, simply stop moving it.

However, I gather you probably want to move it to where it was no longer colliding?

There are a number of approaches.
1) Easiest is to just put it back to where it was before it collided.
2) Another would be to work out which direction (of the four main directions) you want it be 'ejected' from the other object and then place it so that their edges match (this seems to be what you're going for).
3) A more accurate way would be trace the move between the two positions and calculate the position where they would intersect. You can do this after colliding and return it to this position, or before colliding when working out if it will be collided in advance.

As an example of (2), if you wanted to 'expel' a sprite (sprite) to the left, you would set it's x position so that it's right side matches the other object's (_sprite) left side. Something like this:
sf::Vector spritePosition{ sprite.getPosition() };
spritePosition.x = _sprite.getPosition().x - sprite.getTexture().getSize().x;
sprite.setPosition(spritePosition);
Note that getTexture().getSize().x is the width of the sprite unless the sprite is scaled.
Also, we presume no rotation is involved.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*