Check colliding 1 unit with another

Hey people , i need your help .

I am making RTS i have 1 problem : how to check if 1 unit is colliding with another unit.

Give me idea how to solve this problem.

Thnk you.

Depending on the shape of the unit, you could do a rectangle vs. rectangle or a circle vs. circle collision check. These are the most basic ones to use.

rectangle vs rectangle

normally in RTS games the collision is handled by the path finding algorithm (A*, Dijkstra’s, etc) so its handled by some sort of grid. Other options are to use some sort of flocking algorithm or steering behaviour.

When I was doing something like this, I had an array that represented the map divided up into little sections (or tiles/ or something smaller than tiles). Each unit, when moving, “owned” 2 indexes in the array (the one it was on, and the neighboring one it was headed to). The indexes were marked by the ID’s of the units on them or moving to them. When the unit reached the destination index, the previous index was set to zero, and then it check to see if the next index was available (available = 0), if it is available it marks it with it’s ID and heads towards it… etc. Units could not collide because they would first check to see if the index that it wanted to move to was available, if not it would either wait or find an alternate route.

For recangle vs. rectangle, you can use java.awt.Rectangle/java.awt.geom.Rectangle2D.Double and use their intersects(…) method or build your own intersects method.