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

Author Topic: Advise on collision detection using std::vector  (Read 4188 times)

0 Members and 1 Guest are viewing this topic.

PapaSmurf

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Advise on collision detection using std::vector
« on: March 16, 2015, 12:08:09 am »
Hi,

I am unsure which method to use to handle my enemies in SFML.

The idea that I am trying to recreate is to spawn enemies every so many seconds. Initially I wanted to do this using a vector such as std::vector<sf::Sprite> SpriteVector; The advantages of this method would be that in theory it is easy to add and remove my enemies from the vector. Using the push_back and erase method.

However the error with this at the moment is that when I am trying to check whether the sprite or the arrows are hitting an enemy within my vector it will not work as I am getting the following error.

19
IntelliSense: no instance of overloaded function "sf::Rect<T>::intersects [with T=float]" matches the argument list
            argument types are: (sf::Sprite)
            object type is: sf::FloatRect
h:\Game Alpha 1.2\Game Alpha 1.2\Game.cpp
461 16
Game Alpha 1.2

Error 17 error C2664: 'bool sf::Rect<T>::intersects(const sf::Rect<T> &) const' : cannot convert parameter 1 from 'sf::Sprite' to 'const sf::Rect<T> &' h:\game alpha 1.2\game alpha 1.2\game.cpp 461 1 Game Alpha 1.2


I know that I could possibly use an array to store the enemies then set their state when hit to false however I am not sure how this will effect spawning my enemies. The functionality of being able to add to a vector is what I think I will need. Otherwise I am sure not sure how to get around the issue of having a certain number of enemies within the array then spawning more over a series of time.



Could you please advise as I am really stuck on this?

Kind Regards

Knucklehead

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Advise on collision detection using std::vector
« Reply #1 on: March 16, 2015, 07:54:28 am »
in regards to the intersect function
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Rect.php

-the function sf::Rect::Intersect requires a parameter of type(const Rect< T > &rectangle)

-what you are doing is giving a type sf::Sprite

-what you want to do is call your sprite then call the function get localBounds() like:
sprite.getLocalBounds(); this might not return the box in view space it might be in model so try:
sprite.getGlobalBounds(); if the above doesn't work


-with regards to your vector question yes vectors are super fast to add nodes to, however if you are removing a lot of nodes from the vector you are going to be pushing and popping a lot

-consider looking into is linked lists
http://www.cprogramming.com/tutorial/lesson15.html

-if you are new to programming I would recommend sticking with vectors.



Josh_M

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Advise on collision detection using std::vector
« Reply #2 on: March 16, 2015, 08:01:07 am »
Linked lists are almost completely redundant with modern processors:

There's a reason c#'s List structure is actually a dynamic array internally  ;)

If your vector is unsorted, then if deletion performance is an issue you can move the last element of the vector into the index that you wish to delete, thus saving shifting every element after it in the array.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Advise on collision detection using std::vector
« Reply #3 on: March 16, 2015, 08:28:42 am »
-consider looking into is linked lists
http://www.cprogramming.com/tutorial/lesson15.html

-if you are new to programming I would recommend sticking with vectors.
I would recommend sticking with a vector at all times and more or less forget that std::list exist. A linked list is just about the worst datastructure you can use with modern processors since you are constantly chasing pointers to get to the next element which (unless you are lucky - and you won't be lucky for long) will incur a cache miss on every access and that really kills performance and other reasons. A vector will beat the performance of a list for just about anything.

But don't take my word for it, the experts say the same. See these references:




(start roughly at the 45:30 mark)

 

anything