Sign in to follow this  
suliman

avoid ships colliding?

Recommended Posts

suliman    1652

Hi

I have a topdown 2d ww2 naval games with warships (destroyers, cruisers etc) running around (Think "navyfield" for the scale).

You Control a ship and have ai-controlled support ships, and the enemy has their ships (might be 10 ships in a fight)

 

Its realtime and no tiles, ships move "physics-based" taking time acc/deaccelarating, turning sluggishly (especially larger ships) but scale is rather arcadish so you can easily get close ("ram") other ships. Also ships need to go close to get within range for their guns. Each ship is controlled by turning and engine power.

 

So how do i make ai-ships avoid crashing into eachother/enemy ships? When facing other ships, if one turn and the other turns the same way they may just still crash into eachother (and so on...)

 

Im looking for a simple solution that works acceptably! Dont need to be perfect.

 

Thanks

Erik

Share this post


Link to post
Share on other sites
Orymus3    18821

My simple system assumed an 'aura' around my ships. If it detected a ship within this range, it would seek the closest angle to rotate TOWARDS it, and move the opposite direction (overriding its regular steering until no ship was within that aura).

 

Some of the issues of this system:

- If there are two ships within range (one on either side of the helm), the ship would have a hard time breaking free, but assuming at least one of the others was also AI-driven (which it should, as this was a single player game), the other AI ship would break free first, giving the 2nd ship an angle of retreat.

 

- If there are just too many ships on the screen or the auras are too large, AI mayhem insues. So I had to balance the aura radius and the max amount of ships on screen accordingly.

 

Possibly far from the best solution to the problem, but it was simple and easy to devise, and it works good enough for my game (I don't think implementing anything better would have a better outcome and it is no longer bugging me).

Share this post


Link to post
Share on other sites
flodihn    281

I created a long collision box shooting out from the ship, if that box collider with another I knew the ship was in risk of colliding in the future. Then you can take the other ships speed and compare to yours with the length of the collision in mind and you have a pretty good guess if collision will happen or not.

 

I also used an aura mention earlier but I only used that for evading incoming fire, since is usually only good for detecting things really close. 

Edited by flodihn

Share this post


Link to post
Share on other sites
Mizmoon    182

I was having the exact same problem once, and my solution was to give each ship an offset vector which represented how much the ship had to deviate from its course. The vector was zeroed at the start of each AI pass, and then you go through all neighboring ships in range, and add the vector between them and your ship scaled by a factor determined by their distance to you. This means that if there is another ship at both your left and your right, and they are at equal distances, the net offset will be zero. If there is one behind you and one to your left, the offset will be to your upper right, etc. If you need the offset as an angle, it is quite easily done with some trigonometry, followed by adding the offset angle to the facing of your ship to get the new facing. You might have a problem in the case where two ships are on your front left and right, and the best path would be to pass between them (while the offset vector points backwards), but it should be possible to fix with some exceptions.

 

Sorry for the semi-necropost, i hope it works out for you.

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