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

Author Topic: collision detection with point reduction help  (Read 3616 times)

0 Members and 1 Guest are viewing this topic.

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
collision detection with point reduction help
« on: September 09, 2012, 04:36:18 pm »
In my asteroid game, where you control a ship and try to avoid asteroids. I want to make you lose points, if you hit an asteroid. However, in my collision detection, it returns true a lot of times per second, so they keep on losing points really fast. Like if they hit one asteroid, they lose maybe 50 points, when it should only be -1 point. What should I do to prevent that from happening. I tried using bools to set it equal to true and if its true then subtract 1 point, but that makes them lose lots of points as well.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: collision detection with point reduction help
« Reply #1 on: September 09, 2012, 04:47:24 pm »
Well what happens if the ship hits the astroid the first time? Will the astoid disappear? Will it break? Will it just go through it?

If the astroid just stays there and the ship goes through it, the collision detection will go on for as long as the ship touches the astroid. Now depending on your implementation, you'll have to make it the way, that points get only subtracted on the first impact. If the astroid is represented as a class, you can add a boolean variable and a getter/setter functions. On initialization it gets set to true (= alive) [or false (= not hit yet)] and if it gets hit, you set it to false (= dead) [or true (= hit)] and at the collision test you check if the astroid that intersects with the ship is already dead [or hit] and don't run it anymore if so. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: collision detection with point reduction help
« Reply #2 on: September 09, 2012, 04:55:39 pm »
Oh ok that would make sense. Right now, they don't get destroyed on impact.

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: collision detection with point reduction help
« Reply #3 on: September 09, 2012, 05:59:35 pm »
My asteroids are in a vector, so on impact, I tried deleting them, but every time I do, I get an out of range at memory location error.
to delete them I do

if(smallHit == true)
      asteroidV.erase(asteroidV.begin()+smallHitNum);
 

smallHit is a bool that is set to true if the ship hits an asteroid and smallHitNum stores the location of the asteroid that was hit.

I also tried
if(smallHit == true)
                        {
                                for(int e=0;e<5;e++)
                                {
                                        if(smallHitNumA[e] == e)
                                                asteroidV.erase(asteroidV.begin()+smallHitNumA[e]);
                                }
                        }
 
There is more than 1 asteroid, so if one of them was hit, then i loop and check if smallHitNumA[e] == e.
smallHitNumA[] is an array of 5 with all the value initialized to -1. If an asteroid is hit, then the number of the one that hit it, get stored in the corresponding element in the array.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: collision detection with point reduction help
« Reply #4 on: September 09, 2012, 06:06:14 pm »
Well if you delete/erase an element of the vector the vector size will obviously be smaller, thus your loop with the constant 5 will get out of bound (the vector holds only 4 elements...). ;)
Never use constants when iterating over a dyanmic structur. Here you should use smallHitNumA.size().
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: collision detection with point reduction help
« Reply #5 on: September 10, 2012, 01:55:59 am »
Ok, it now works, but now i want to refill the asteroids that were deleted so that there are 5 again.
How do iterate through the vector?
std::vector<asteroidV*>::iterator iter;
......
if(asteroidV.size() < 5)
                {
                        for(iter = asteroidV.end(); iter<5; iter++)
                        {
                                asteroidV.push_back(sf::Sprite());
                                asteroidV.at(i).SetImage(asteroid);
                                asteroidV.at(i).SetPosition((float)sf::Randomizer::Random(50, 780), (float)sf::Randomizer::Random(-5, -50));
                                asteroidV.at(i).SetCenter((float)asteroid.GetWidth()/2, (float)asteroid.GetHeight()/2);
                        }
                }
 

I get an error when I make the iterator, it says that asteroidV is not a type name. Do I have to use a class in there?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: collision detection with point reduction help
« Reply #6 on: September 10, 2012, 02:30:29 am »
There are 4 errors in this code, at least.
Iterator declaration is same like the vector it's going to iterate over + ::iterator at the end.
Use prefix increase (++ in front) with iterators.
You don't use at() to acess elements with iterator.
Iterator acts like a pointer -> accesses the element like through a pointer and * can dereference it like a pointer but they are not pointers.
The start value(for iterating over whole vector) should be begin().
The check should be ' iter inequal to end()'.
std::vector<sf::Sprite> vector;
...//push something into vector
for(std::vector<sf::Sprite>::iterator iter=vector.begin();iter!=vector.end();++iter)
{
iter->SetImage(asteroid);
iter->SetPosition(200.f,150.f);
...//ect.
}
 
This is the correct way to iterate through vectors and few other stl containers.
Back to C++ gamedev with SFML in May 2023

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: collision detection with point reduction help
« Reply #7 on: September 13, 2012, 05:04:41 am »
Ok thanks!
Now I put my asteroid in its own file and its own class, but for one the functions, i need to use the view.
Should I make a class just for the view? But I was thinking that there wouldn't be much in it. I was then going to make it a friend of the asteroid class.

Right now, the view is in my main.cpp


Edit: view also needs App, so should I make a class for App and view together?
« Last Edit: September 14, 2012, 11:29:33 pm by rasen58 »

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: collision detection with point reduction help
« Reply #8 on: September 15, 2012, 04:37:32 am »
?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: collision detection with point reduction help
« Reply #9 on: September 15, 2012, 11:15:51 am »
Yeah don't edit posts to add new questions, we won't notice the edit... ;)

We dom't know how your classes interact with each other so it's hard to say what you should do...

Obviously if you want to change the view, you'll somehow have to pass it to the window, thus there needs to be an interface for that somewhere. ;)
« Last Edit: September 15, 2012, 11:17:53 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything