# Moving units out of the way...

## Recommended Posts

celledor    100
Hey, I have made a few test games (using truevision3d, c# .net), one that reached open beta of sort and is now working on an RTS, but I have ran into a problem. I got pathfinding up and running, multi-selecion and movement along the paths but the problem I'm having is getting the units to move out of the way of each other in the simplest and fastest way. Now if two (or more) are colliding while moving they will most of the time get stuck and jamming the road for all others that are following. My current system checks for collisions in front of the unit and they stop and wait for a short time if hitting something, this works rather well when they all are moving in the same direction (even though there might be a lot of static obstacles in the way). When they move towards each other or must cross others path now that’s another story. So how is this done in a good way, have tried to search but haven't found anything good that I actually can use (much on pathfinding and movement of single or small amount of units but not several group at the same time). Some good general rules perhaps that works both when two units crosses each other path and moving a single unit through a big group on units that isn't moving but need to move out of the way of the single unit (or determine if it is faster for the single unit to move around the group perhaps). It’s late and I hope I explain what I need help with.

##### Share on other sites
karwosts    840
What you probably want to do is continue to do collision detection, but you don't want the unit to "stop" whenever it collides with something. What you ideally would like to do is to find the vector that corresponds to the overlapping region between the units collision radius. This vector will be in the direction from one units center to the other unit's center, and its length with be the amount of overlap in their collision circles.

When you have this vector you would like to "push" the units in opposite directions along this vector. You can choose how much you would like to push each unit based on a weight factor.

Assume you have two units and their attempted movement vectors V1 and V2, weights W1 and W2, and the collision vector C.

Let the real movement vector then be something like MoveVector1 = V1 + C * (W2), and MoveVector2 = V2 - C * (W1)

If you want unit 1 to push unit 2 out of the way completely with no impedance to its own path, than make its weight 1 and the other units equal to zero. If you want to treat all units equally than make them each 0.5 and they will smoothly slide around each other while moving.

If you want to do pathfinding around a group of units that's another story, but this should allow units to push their way through a group and not get "stuck" everytime it bumps into something.

##### Share on other sites
Codeka    1239
This site is a great resource for implementing this kind of thing. In particular, look at the obstacle avoidance one or the more advanced crowd path-following example. The crowd path-following one is a combination of a few behaiours, though, so make sure you look through everything :)

For me, the hardest part has actually been when the units all arrive at their destination, they end up circling around each other cause they can't all be in the same place at the same time... that's another problem, though :)

##### Share on other sites
celledor    100
karwosts:
I'm not sure I follow but I will give it a try however wounldn't this look like they where pushed away rather then that they are driving out of the way. In this rts all units are vehicles.

Also if two units are colliding head on, which one would push?

Codeka:
I have looked at red3d.com before but had problems correctley implementing it, it would be great to have it though. Perhaps I will give it another try as I seems to end up back at that site time and time again.

##### Share on other sites
karwosts    840
Quote:
 I'm not sure I follow but I will give it a try however wounldn't this look like they where pushed away rather then that they are driving out of the way. In this rts all units are vehicles.

I was not aware about the vehicles, when I was describing my situation I wasn't thinking about cars, and to be honest I was thinking it would look like they were being pushed out of the way.

However technically you could make it look like whatever you want it to. All I'm really describing is a way to determine a location where the "in the way" unit needs to get to to get out of the way. If you just move it to that location it might look like it was pushed, or if you turn the vehicle in that direction, animate some smoke out the back and make a "vroom" sound it might look like it was driving out of the way.

To be honest I'm not sure how realistic your vehicle physics is supposed to be, so this might not be the best way to go. I was imagining something like a person trying to push through a room of people when I described this, so I'm not sure how it would look for something that has to realistically turn and can only move in a relatively straight line.

##### Share on other sites
celledor    100
Well the vehicle physics wasn't planed to be that realistic just enough for the player to feel that they are vehicles, it more important that they move in an intelligent way.

My fear of using full steering behaviours is that it might be slow for large numbers (read a few hundred as a max) of unit but as this is complex stuff all solutions will probably take time.

I can still try you way and make them find and move to the "out of the way" location and it might look okey. I'm more into getting the right functionality rather than visual stuff, that can always come later.