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

Author Topic: Collision Detection Problems  (Read 1868 times)

0 Members and 1 Guest are viewing this topic.

MrUnlucky

  • Newbie
  • *
  • Posts: 10
    • View Profile
Collision Detection Problems
« on: January 15, 2014, 03:12:21 am »
I've made a little toy program that draws a red and white square to the screen. The player controls the red square and when it collides with the white square I pick a random position on the screen to move the white square. It works when I collide with the white square once, but the second time I try to collide with it nothing happens. I think it might have something to do with the square's transformation and its bounding box or something, but I'm not familiar enought with sfml.

Here's the code for the collision:
bool collide(sf::RectangleShape cellOne, sf::RectangleShape cellTwo) {

        return cellOne.getGlobalBounds().intersects(cellTwo.getGlobalBounds());
}
 

Here's where it's called:
if (collide(redCell, whiteCell)) {
                whiteCell.setPosition(randomPosition());
        }
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Collision Detection Problems
« Reply #1 on: January 15, 2014, 03:15:12 am »
I don't see anything that stands out as being wrong. Could you possible post a complete and minimal example?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Collision Detection Problems
« Reply #2 on: January 15, 2014, 03:19:07 am »
It looks ok.
Maybe randomPosition() isn't as random as you think, and it gives you the same result as the first time they collides. So it "moves" to the same position.

It's only a guess since you didn't post a complete and minimal code which reproduces the problem. ;)

MrUnlucky

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Collision Detection Problems
« Reply #3 on: January 15, 2014, 03:30:04 am »
It looks ok.
Maybe randomPosition() isn't as random as you think, and it gives you the same result as the first time they collides. So it "moves" to the same position.

It's only a guess since you didn't post a complete and minimal code which reproduces the problem. ;)

Oh duh, I'm almost positive that's the problem. I didn't even think of that until you said something. I'm still trying to get used to the new <random> header and how everything works.  I'm seeding the engine with a number that I chose arbitrarily, when I think I'm supposed to be seeding with a random_device? Anyway, I have stuff to do right now, but I think I got it. Thanks.
« Last Edit: January 15, 2014, 03:32:30 am by MrUnlucky »

 

anything