Sign in to follow this  
cdxrd

Help with collision detection problem!

Recommended Posts

cdxrd    205
Having problems editing.. grrr Ok, I've been working on this for a week or two now off and on. I know the code is probably ugly and downright archaic in some parts, but I've run into a problem. I posted a couple weeks ago about building a sprite manager, and using vectors, and so far, all of that has turned out great. Now Im stuck on some collision detection routines. I have 4 objects dropping, rain drops, bricks, lightning bolts and a rainbow. The drops you catch, the collision detection works great for that. Same with the rainbow, catch it, you get an extra life; and it works! However, when you get hit by lightning or a brick, for some reason the game crashes. The CD for the lightning and bricks is IDENTICAL to the detection for the rain and rainbow. Those dont crash it except on occasion, and that is something to do with the vector I'm betting. Here is my source (FYI, everytime i try to display the source, it wont allow me to save the change.. times out.. grrr.. so its linked): main.cpp Now, the main problem I assume is in check_collisions() . Its a switch passing the data to the collision_test() function. Aside from a few different x & y values to check, all 4 tests are identical. Why do the first two work, and the second two crash it? There is a link to the release version of it here that has the images if it helps. Any thoughts on how to help? I've been staring at this a couple hours at a time for a couple days, and I just don't see where I am going wrong! Linked PS: How do I make a good pause?!?! I thought the routine in there would work, but things still seem to be updating in the background! [Edited by - cdxrd on April 24, 2006 2:02:07 AM]

Share this post


Link to post
Share on other sites
DesignerX    174
I think your problem is in the for loop where you iterate your vector.
When you are hit by a drop you erase the drop from the vector and advance the vector iterator by one.
Now if the drop you are erasing is the last drop in the vector then after the erase and the incrementation your Current iterator will point to a non defined place which will cause a game crash.



Share this post


Link to post
Share on other sites
cdxrd    205
Quote:
Original post by DesignerX
I think your problem is in the for loop where you iterate your vector.
When you are hit by a drop you erase the drop from the vector and advance the vector iterator by one.
Now if the drop you are erasing is the last drop in the vector then after the erase and the incrementation your Current iterator will point to a non defined place which will cause a game crash.


OK, I thought about that as well, however if you download the attachment and run it, you will see this is not the case. When struck by lightning and the game crashes, there are almost always at least 5-10 other drops on the screen at once. The vector is almost never empty. And the code checking the drops and lightning is almost IDENTICAL, literally copy / paste, except for the X & Y of the players bounding box. Catching the drops doesnt crash it.

Share this post


Link to post
Share on other sites
mikeman    2942
Quote:

OK, I thought about that as well, however if you download the attachment and run it, you will see this is not the case. When struck by lightning and the game crashes, there are almost always at least 5-10 other drops on the screen at once.


As DesignerX pointed out, the crashes will occur when the drop you erase happens to be the last in the vector, not when the vector is empty. There can be 10 drops visible in the game, but if you hit the 10th, it will crash. The game crashes when you hit lightnings because, due to the high speed of the lightning drops, when you get hit by one it's almost certain that it's the last drop(the cloud hasn't got time to release another drop before the lightning hits you). The raindrops are much slower and take more time to hit the ground, so you are not likely to catch the last raindrop: by the time you catch one, another one will be spitted out by the cloud.

That code piece is wrong anyway. Drops.erase() returns the first element beyond the removed one, or end() if you erase the last element. But you also always increment it yourself by "Current++". That is, when a collision occurs, you increment it 2 times. If it happens to be the last element, with the first increasement you get to end(), and with the second you get beyond that, and crash. Just do something like:


if(collision)
{
Current = drops.erase(Current);//If collision, erase and increment
}
else Current++;//else, just increment


Share this post


Link to post
Share on other sites
cdxrd    205
Sorry, still a newb.. =) DesignerX, I see what your saying now thanks to the way Mikeman explained it, and it makes perfect sense. I hadn't thought of the fact that the lightning was dropping much quicker so it was the last item in the vector, being erased, then incremented past the end of the vector.

I knew it had to be something stupid and simple on my part, but I just couldnt see it. Thanks for the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this