# An algorithm for steering a spaceship?

## Recommended Posts

I apologize in advance as I'm sure this question is answered somewhere, but I haven't been able to find it. I am making a space fighter game and I can't figure out a way to let the AI know how to steer to hit a waypoint. My current system is just to find the angle to the destination and fly the ship full tilt while turning to face the point. However this leads to the ship sliding right past the waypoint and eventually going into orbit. From flying a ship myself I see that the correct thing to do is to tell the AI to turn further then it needs to go, but I don't know how much to oversteer. Also, I need to be able to recognize situations where the ship is just going too fast and needs to slow down. It's worth noting that in order to make this game playable I have built in a magic "space friction" that opposes velocity not in the direction the ship is facing. This cuts down on the sliding, but does not stop it completely. Also, my physics engine is complete enough so I have access to all the velocities, forces, torqus, masses etc. I'm pretty sure I have all the numbers I need, I just have no idea how to combine them. For a more detailed version of the question (complete with stunning MS Paint diagrams) please check out: http://www.jameswells.net/turning.html Thanks in Advance.

##### Share on other sites
something you can try, a "look ahead". Basically, calculate the state of the spaceship (postion, speed, orientation), say 1 seconds forward in time. Then calculate the inputs from that state, not from your current state. It should be quite quick to test, and you'll be surprised by the results.

Equally, apply the look ahead to the target. so, if it's a missile, calculate the missile state 1 seconds forward, and the target as well.

EDIT : make it one second.

##### Share on other sites
alternatively, you can have a look at [google] Boids and Steering Behaviour. Very cool automated, navigation for A.I.

http://www.red3d.com/cwr/steer/gdc99/

##### Share on other sites
Thanks so much for taking the time answer. I'll give the simple option a try first :) If it turns out to be that simple I'll kick myself, but as long as it gets finished I'm happy. Thanks again.

##### Share on other sites
Just choose a diameter around the waypoint. If your ship intersects with this diameter then cut the engines, turn your nose and coast into the waypoint.

Alternately, you could shoot for a different waypoint that is between your original position and the real waypoint. You can judge this distance by your current velocity and your distance away from the real waypoint.

##### Share on other sites
I haven't checked that link yet but have an idea anyway.
First of all it is very bad to pursuit the target 1 sec after it's current location (tried it).. The result of such AI is a space ship orbiting about a distance from its target (which is worse than it was)..

An alternative is to make it pursuit (orient it to) the target after a time t. This time t is collision time approximation. One simple approximation is calculating the distance between the target and yourself (ship, missile, etc) and divide it over some value.. You can even make your own robust approximation by giving it an analytic vectorial thought!

HAVE FUN!

##### Share on other sites
If you've got the steering going already all you need is to vary the thrust! Simply scale the thrust based on the angle needed to turn and the distance you are from the target.

Make the ship fly slow if your close to your target or have a long way to turn. If your far away from your target and are facing it then max thrust. A few if() statements and scaling calculations should have you going in no time with a minimum of effort or changes to your code.

##### Share on other sites
it's the relation between speed and distance, so the time it takes to reach the target, but like any interception algorithm, you need to be careful when the distance is small anyway. But that should cure the oversteer and understeer in a quick and simple way.

##### Share on other sites
If you want an exact solutin for your "intercept" time you need strong math skills because there are some differential equations to be solved. But you can simplify your model: you can add a small force to your ship to compensate for the unwanted drift (of course when you're heading to the waypoint) so, in a few seconds you'll loose the drift. It's something like a "custom" friction on the unwanted drift. (this one worked for me).
Also your stearing speed should decrease (or even reverse) when you approach you're approaching the desired angle. If the stearing speed is linearly accelerated is a simple equation to find the stop time.
The way point should be far enough so the ship will have time to adjust the trajectory otherwise you may want to reduce speed (or even reverse).
This way you'll be able to orient the ship, but perhaps you'll have other problems when you reach a waypoint, because the ship will pass trough the waypoint and then it will reorient for the next waypoint. You may want to start reorienting prior reaching the waypoint.
In this case an easy solution is precalculation (or testing :) ) because you can presume your ship will move at max speed so, you should find a "good" "orient distance".

Hope this helps

c0mas.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628291
• Total Posts
2981862

• 11
• 10
• 10
• 11
• 17