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

Author Topic: Error with collisioncheck against a vector  (Read 2764 times)

0 Members and 1 Guest are viewing this topic.

martinw30

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Error with collisioncheck against a vector
« on: August 10, 2012, 02:46:06 pm »
I have added my sprites into a vector and am passing this vector and a shape object into a collision function. I need the function to check every element of the vector to see if the shape has collided with it, yet it only returns true if it collides with the first element of the vector. How can I get this function to return true if the shape collides with ANY element of the vector, not just the first:

bool CollisionCheck(const sf::CircleShape& shape, vector<sf::Sprite>& sprite){
                // return true if a shape collides with an element of the vector
                for (vector<sf::Sprite>::iterator it = sprite.begin(); it != sprite.end(); it++)
                {
                        // get the current position of the sprite
                        if (shape.getGlobalBounds().intersects(it->getGlobalBounds()))
                        {
                                return true;
                        }
                }
                return false;
        }

Any help will be greatly appreciated  :)

martinw30

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Error with collisioncheck against a vector
« Reply #1 on: August 13, 2012, 05:00:14 pm »
Can anybody reply to this query?  :-\

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error with collisioncheck against a vector
« Reply #2 on: August 13, 2012, 05:05:26 pm »
This code looks good (except all these missing "const").

You should take your debugger and use it to run your code step by step; if you watch carefully the execution flow as well as the values of the variables involved, the mistake should show up quickly.
Laurent Gomila - SFML developer

martinw30

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Error with collisioncheck against a vector
« Reply #3 on: August 13, 2012, 05:13:20 pm »
thanks for the reply Laurent.
 ;D
What do you mean by missing Consts? :o

edit : I have also stepped through my code. The function only returns true when the shape collides with the first element of my vector. And whilst it is colliding it rapidly iterates through the vector. So when it collides with sprite 1 in my vector it collides with them all.

Man I'm stumped....
« Last Edit: August 13, 2012, 05:28:48 pm by martinw30 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error with collisioncheck against a vector
« Reply #4 on: August 13, 2012, 07:34:25 pm »
Quote
What do you mean by missing Consts?
I mean this:
bool CollisionCheck(const sf::CircleShape& shape, const vector<sf::Sprite>& sprite){
                // return true if a shape collides with an element of the vector
                for (vector<sf::Sprite>::const_iterator it = sprite.begin(); it != sprite.end(); it++)
                {
                        // get the current position of the sprite
                        if (shape.getGlobalBounds().intersects(it->getGlobalBounds()))
                        {
                                return true;
                        }
                }
                return false;
        }
(it's not a member function so there's one less const than I though)

Quote
I have also stepped through my code. The function only returns true when the shape collides with the first element of my vector. And whilst it is colliding it rapidly iterates through the vector. So when it collides with sprite 1 in my vector it collides with them all.
When it's the second sprite that is supposed to collide, what happens on the second iteration? Have you checked both rectangles, and found out why the test returns false?
Only you and your debugger can find out what really happens, from looking at these few lines there's nothing more we can do to help you.
Laurent Gomila - SFML developer

martinw30

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Error with collisioncheck against a vector
« Reply #5 on: August 14, 2012, 10:02:07 am »
Thanks for the reply Laurent :D

The sprites that are in my vector are part of my main screen and menu, so i have a shape bouncing around the screen colliding off those individual sprites. But before that happens I move the sprites from my Vector into position. So I've animated the logo of my game (Just to add some flair) which is broken up into 8 parts (hence the vector).

Maybe I should position them first then check for collision?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error with collisioncheck against a vector
« Reply #6 on: August 14, 2012, 10:15:29 am »
I don't know, you should rather show the corresponding code.

But... why don't you just debug your function further? If it doesn't behave as expected, you should easily find out why by stepping into the function and watching the variables involved in the results. Do you have any problem with this?
Laurent Gomila - SFML developer

martinw30

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Error with collisioncheck against a vector
« Reply #7 on: August 14, 2012, 10:18:35 am »
Hi Laurent,

I'm afraid I do, it isnt something Im comfortable doing neither is it something I was taught at University :/

Are there any helpful tutorials or links to help me understand it further?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error with collisioncheck against a vector
« Reply #8 on: August 14, 2012, 10:24:40 am »
Learning how to use the debugger is crucial, it is the only way to properly debug your code and solve all kinds of problems. Trying to fix code just by staring at it is not really efficient ;)

I don't know which IDE you use, but you should easily find debugger tutorials for the most common ones (Visual Studio, Code::Blocks, ...). To debug your function, you only need simple things:
- run with debugger attached
- break at the beginning of the function
- run the function step by step
- whenever a collision test occurs, watch both the shape's and sprites' bounding rects (you'll probably need to store them in local variables so that your debugger can read them easily)
- find out why it doesn't return the expected result :)
Laurent Gomila - SFML developer

martinw30

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Error with collisioncheck against a vector
« Reply #9 on: August 14, 2012, 12:21:19 pm »
again many thanks Laurent :D

I am using VS10 and found a tutorial which will help. Ive linked it below for anyone else using visual studio

http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

EDIT:

OK after stepping through my code carefully, I have assigned both the shape and the element of the vector to a local floatrect and I am finding that when I call the getGlobalBounds() of the shape I receive values for left, top, width and height but my vector only gives me width and height, the left and top values from the sprites of my vector are 0.

Should this be so?
« Last Edit: August 14, 2012, 02:33:28 pm by martinw30 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error with collisioncheck against a vector
« Reply #10 on: August 14, 2012, 11:03:31 pm »
What values are you supposed to get for left and top? How are they assigned to the sprite?
Laurent Gomila - SFML developer