Search Unity

Space ship steering AI

Discussion in 'Scripting' started by Ahnuld, Nov 20, 2013.

  1. Ahnuld

    Ahnuld

    Joined:
    Oct 20, 2012
    Posts:
    15
    Hi all,

    For a small hobby project I'm trying to create a basic space game in unity. I was wondering if I could recreate parts from games like Nexus: The Jupiter incident, freelancer and the X series.

    Now I kind of hit a large brick wall when I tried to implement AI ship behaviour. My idea for this was to have a simple state machine controlling the ship, and an autopilot doing whatever movement must be done. Mostly stuff like flying towards waypoints while avoiding obstacles and other ships. The autopilot is what I can't get working property.

    My first guess was that there would be libraries or assets for this. And there are things like OpenSteer and UnitySteer, but they don't seem to be able to do what I want out of the box. And now I have to choose between modifying for example unitysteer or starting from scratch. What should I do? When starting from scratch with a steering AI, can anyone recommend a good read on how to do this, that goes beyond the very basics?

    I love the way capital ships move in Nexus: The jupiter incident. They actually appear to be using thrusters for rotation, main engines for speed and secondary engines for slowing down. This method could be used for indirect control (just setting a waypoint and have the AI move towards it) or direct control (thrusters firing depending on mouse input and controlling the main engine with the keyboard). But I'd be happy with any halfway realistic looking movement which is easy to control. The same control method could be used for smaller ships, but because of their lower mass they would be much more agile.

    I'm looking for a way of controlling the steering that looks cool, but is also reasonably easy to control. Any recommendations on where to look are very welcome. Thanks for reading.
     
    siddharth3322 likes this.
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,463
    I'm working on a physics based AI Agent controller. So far i've been happy with the results but I'm not coding it, I'm using playMaker to design it.

    I use an anchor point and a local movement target to stimulate movement. The Anchor is the destination of the agent and the local target is use to drive him around. I use (currently) three FSM's that are driven by a behavior system that I haven't implemented yet. 1)Brain, 2)Aim, 3)Thrust. The Brain FSM immediately places to local target on the Anchor point. The Aim FSM uses torque to turn the ship to aim at the local target constantly. The Thrust FSM applies force to the agent along the Z to push it forward.

    So after it starts moving toward the Anchor the Brain FSM starts raycasting and looking for obstacles, when it hits one it kicks on the local avoidance, gets the normal and places the local target away from the surface which makes the agent try to aim at it, changing the trajectory of the ship. I just keep moving the target when the raycast hits surfaces and he avoids things pretty well. Using the normal helps add diversity to his flight direction, but I'm working on more random movements and weighting toward the target. When there's nothing being hit then it tries to aim at the anchor point again.

    It's basically "fly to the anchor point until you hit something, then turn away until you clear it, then try to look at the anchor again."

    So far it works but it's a fairly narrow minded AI for the moment and large obstacles present a pathfinding issue as he currently doesn't care how he avoids locally in respect to his final destination.

    I emailed several people that have published pathfinding assets and they generally agree that either a node-based pathfinding system with strong local avoidance via raycasts or a robust local avoidance system alone would be a good solution since you can't really scale traditional methods efficiently in a 3d space as large as you need for a normal 'outer space' 3d game.
     
  3. Ahnuld

    Ahnuld

    Joined:
    Oct 20, 2012
    Posts:
    15
    Thanks for the reply! Why do you use the normal to determine where to go? You could use the normal to know in which direction to push the ship away from the obstacle, but what if the object you're approaching is a cube, and the normal is pointing in the opposite direction from you? And wouldn't there be jerky movement when trying to avoid a sphere? Does it still work under these circumstances?

    Some more (probably useless) thoughts on how I'm approaching this. I put them here to show how confusing this subject is to me, feel free to burn them down.

    A probably silly approach I had in mind was that after detecting a potential collision I could use further raycasting in other directions as a kind of radar to find out where the nearest open space is, and then fly in that direction until you detect that there is no more obstacle in the direction of the waypoint, and resume flight towards the waypoint.

    This would cause trouble when navigating through an asteroid field, as there would be obstacles in all directions.

    This could maybe be solved by ging a raycast hit a 'repelling force' depending on the distance of the obstacle. If the obstacle that the ship has in front is very close, the ship would stop and rotate toward the raycast with the highest collision distance. If the object in front is far away, the ship would maintain speed and slowly rotate towards the nearest free point in space.

    But also this has a problem, because if there's a much closer object between the potential far away collision and the free point in space, the ship would turn right into it at full speed. So you'd have to be sure the way is all clear.

    My brain is melting. :eek:
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,463
    I multiply the normal with the distance I want to be away from the object, yes. It does not work if the planes align but the likelyhood of being perfectly in alignment with the normal along the Z axis is something that would only ever happen in a controlled test. Either way though, you have a point and I've been looking for a simple solution to get a position that is clear nearby, its just difficult for me since I'm struggling with understanding vectors.
    Good idea, a couple of concerns come to mind. One being that raycasts are expensive and firing a bunch in random directions to look for open space probably isn't a good idea, Iguess that you could do something like this and simply choose the ray that is the longest and assume that it would be the most clear route. These sorts of things need to be tested I think to find the best solutions amongst them. I think a good background behavior system would drive the choice of the direction as well and needs to be operating in conjunction with any LA system you get going.

    And yes, it's a tough and confusing subject! Aside from there being zero 3d flight AI solutions there is minimal information regarding the subject at all. I've made several topics in AI forums and here and get minimal or no response regarding fundamental ideas on the subject - now low level problems are always helped, but the main idea of 3d flight ai is not so it seems we need to prototype on our own here. I highly recommend using playmaker as it's a full-blown FSM AI making machine.

    The other thing I had been having trouble with is making the same system work in extremely dense areas of small objects as well as when running into larger objects. One problem that persists is that the ray emits from the center of the ship and it doesn't detect objects that are very close. If its following the wall of an asteroid it seems to get closer and closer to it as the shape falls behind the raycasting 'wave' triangle closer to its origin.

    Another thread with some information on it:
    http://hutonggames.com/playmakerforum/index.php?topic=5406.0

    WebPlayer:
    http://dl.dropboxusercontent.com/u/38606641/AI/AI-test/AI-test.html

    I've made some tweaks since I posted that webplayer. It's much more interesting when the ship flies about 4x that speed and projects its targetpoint furhter away. There was also some collider bugs and persistent "multi-objects nearby but i only avoid one" problems.
     
    siddharth3322 likes this.
  5. 99thmonkey

    99thmonkey

    Joined:
    Aug 10, 2012
    Posts:
    522
    Nice little demo. At one point the ship picks a circle and keeps going at that so I stopped the demo. Any chance for dropping more info on specifics of what you did?
     
  6. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    760
    Thank you friends for this kind of discussion over here. I got many important points from this discussion (Y)

    Now I have one question for same topic. You have discussed about - how to save spaceship from different obstacles!!

    But what to do if I want to save spaceship from bullets or missile? I want to make spaceship AI stronger for this as well.
    So share your side point of view regarding this as well.
     
unityunity