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

Author Topic: Collision checking with an object and a map  (Read 1525 times)

0 Members and 1 Guest are viewing this topic.

martinw30

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Collision checking with an object and a map
« on: August 02, 2012, 04:46:03 pm »
I am attempting to check collision between two objects. First is the shape passed to my function, second is the map containing one of the many sprites I've loaded into them. The problem I am having is the function only ever checks against the first element. I need to function to check if the ball has collided with a valid item of the map, which one it has collided with, report true then move onto direction change. Here is the function so far:

bool bounceCheck(const sf::CircleShape& object1, map<int, sf::Sprite>& sprite){
                // returns true if a ball collides with an element of the map
                for (map<int, sf::Sprite>::iterator it = sprite.begin(); it != sprite.end(); it++){
                        search.push_back(it->second.getGlobalBounds());
                        if (object1.getGlobalBounds().intersects(search[it->first])){
                                cout << "Collided with " << it->first << endl;
                                return true;
                        }
                }
                return false;
        }

I am assinging integers to the sprites I have loaded into my map for easy referal. Search is a vector I created to add each sprites bounds. Im guessing my logic isnt quite right somehow. Any help?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Collision checking with an object and a map
« Reply #1 on: August 02, 2012, 05:26:13 pm »
I don't see anywhere a decleration of search but I'll assume it's a vector...
A map exists to keep object assosiated with an arbitraty key, so you won't have to have any order like in a vector where every number from 0 to N must be present. Now if you take the objects of the map and push it back on a vector and then try to access the vector with the key of the map, then this is very dangerous. If you always number your sprites from 0 to N and never delete one in the middel then use a vector instead of a map.

Additionally what's the sense in saving the global bounds in a vector? Why don't you just use it?
if (object1.getGlobalBounds().intersects( it->second.getGlobalBounds() )

In short don't use one loop to walkthrough two diffrent datatypes with possible diffrent sizes...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

martinw30

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Collision checking with an object and a map
« Reply #2 on: August 02, 2012, 05:48:00 pm »
OK using a vector is fine, I can make that change.

What I need to now is how I check that the shape has collided with an object that is in the vector as the iterator starts at vector.begin() so the loop wont start checking until the first element has been checked.

How can I do a check that the shape has collided with an element from the vector and report back back which one it has without iterating?