Handling collisions

Started by
1 comment, last by geo2004 16 years, 9 months ago
I just finished writing up a simple collision detection algo for my 2d game. the problem is, I'm not sure what to do once I know 2 units are colliding. I need to find some efficient and fail-safe way to determine which way the unit needs to move once it collides with another unit (or building,tree,rock, whatever). For example Unit A is going from its starting position to its final one, but Unit B is in the way. Once they collide, I need to know which way for A one to go to get around B:

 Final Destination
**

                            *1
                        _Unit B
                       |_| _
                     *2   |_| Unit A
                                **Started Here
Does A go to 1 or 2? And how would it decide? [Hopefully this isn't to confusing] Is the only fail-safe way to do this is implement some pathfinding routine like A*? Any help or useful sites would be much appreciated. Jeff
Advertisement
This has little to do with collision-detection, or even handling. Collision detection only checks if objects collide. Collision handling pushes them out if they're both solid, and/or notifies them of the collision, the low-level work that needs to happen after a collision.

Where A should go is a higher-level decision. Pathfinding would be more appropriate here, yeah. You may also want to look into flocking behaviour, especially how to make objects repel each other. Which approach you should take - perhaps a mix of these, or a totally different approach - depends on what you want to use this for.


EDIT: This just came to my mind, you could project B onto a normal of the line between A and A's destination, and check on which side it sticks out least - you then set that corner as an intermediate destination for A, and repeat the process untill A has arrived safely. I don't know if that will work for every shape or every situation, but it's an idea. Or you could use this to generate a nodegraph and then do pathfinding on it... just some idea's.
Create-ivity - a game development blog Mouseover for more information.
Thanks for the reply and the input.

I had thought about doing your recomendation of setting a corner of the barrier as an intermediate goal. I will need to implement some kind of pathfinding technique eventually, probably A*. I thought about doing this:
When a Unit's destination is set, the Unit will move directly to that spot. If it hits a barrier, then it will compute the best path. I'm kinda hoping this will eliminate the number of computations needed to be done, as many Unit's moves in an RTS can be done without going around something.

However, it seems to me that Unit to Unit collision should be handled differently. I think for now I'll try to find which side of the destination the barrier lies on, and then move accordingly.

Thanks!
Jeff

This topic is closed to new replies.

Advertisement