I know this is both a strange question and a stupid question. But this is an issue I have dealt with in every single game I've made, and I have yet but to find a solution. I can't be the only one who's run into this problem. My google-fu must suck though, because I can't find any information on how to fix this.
Here's the situation:
- A dynamic object is moving in a certain direction.
- This dynamic object then hits a static object.
- This dynamic object now changes the direction, and heads the other way until it hits an object, and then it changes the direction once again. Kind of like paramecia movement, but not exactly. Seems simple enough.
Now here's the issue. I detect the collision, but I can simply inverse the velocity. Using OOP, I have my own class for this object, which performs all movement and actions. So, instead, I have a boolean in this class. A simple boolean that asks whether I'm moving left. If I am, my velocity is negative. I may also need to flip the sprite. If, however, the boolean is false, my velocity is positive. So, all is fine and dandy.
Now, here is where the common issue comes in. I detect collision, and inverse the boolean(object.movesLeft != object.movesLeft
). This is performed EVERY time it detects a collision. The issue now comes in. I inverse the boolean and start to move it in the opposite direction; however, it is not quick enough, and before it gets moving, it still detects the collision, reversing the boolean once again, turning in back into its original value.
I have yet to find a good solution to this. I have found workarounds, but often times I still get this issue.
Currently, I am using Box2D for collision detection, so my flexibility for solutions are constrained. Box2D provides a method for when specific collisions are happening, and when the specific collisions end. I tried only changing the boolean when a collision ends, but there was a pretty big issue. It would only run when a collision ended, obviously. It didn't have to be the moving object that I'm trying to inverse, it could be any dynamic object. The obvious issue is that I need some kind of collision to end before the object will inverse the direction.
So anyways, this is the issue I have faced in even simple games such as pong, and really, really need to find a way to solve it. I can't be the only one of us who have run into this issue.
If anybody understands the issue I'm having, or has experienced it themselves, and has a solution, help would be greatly appreciated. Thanks in advance.