avoid ships colliding?

Started by
6 comments, last by Mizmoon 9 years, 4 months ago

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

Advertisement

Google search:

http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-collision-avoidance--gamedev-7777

http://rocketmandevelopment.com/blog/steering-behaviors-flocking/

One of the ai game programming wisdom books has a chapter on boats and steering, as well. One of the earlier ones, 1-4, not the crazy expensive 5th one.

Cue Craig Reynolds

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

Cool!

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).

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.

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.

This topic is closed to new replies.

Advertisement