3D Space AI

Blitz3D Forums/Blitz3D Programming/3D Space AI

Ryudin(Posted 2008) [#1]
I'm working on a space game, and the one thing I'm really worried about is AI. I have no idea how to do space AI.

Can anyone help with a few tips and pointers?


Mortiis(Posted 2008) [#2]
I have coded a 360 degrees space shooter, it's topdown and arcade style, though AI is similiar to a simulation game.

It goes like this;
If player ship not in radius, randomly move in space
If player ship in radius, turn the enemy ship to player with some delay(not directly or too fast)
if player ship is in attack radius throw whatever weapon you have
if you are attacked and health < 10 then turn 180 degrees and run away.

Very very simple AI really yet it can create the best dogfight experience. You can easily improve it if you code it in FSM style.


stayne(Posted 2008) [#3]
Dumb AI = randomly flying around with no purpose until you fly close enough (if they are hostile). Smart AI = patrolling, guarding, scouting, etc. If they are patrolling then set up waypoints. Use pivots (or cubes at first so you can see what's going on - maybe turn the cube a certain color when the enemy has decided to fly to it, then back to white when the enemy reaches it). Every pilot needs a waypoint eventually, a place to go, a purpose in life, something to fly to. Make sure the enemy doesn't fly straight into the player (unless the enemy is insane). Start out with one enemy parked in a dock guarding, one on patrol and one scout.

This VERY basic function will make an enemy patrol around space in a loop. When the enemy reaches the last waypoint it will go back to the first one and start over. I keep all non-player entities within the same type (asteroids, junk, enemy ships, etc.) and assign them a static or non-static flag. Might be a bad idea but it works so far. Make your AI waypoints a Type and give them some fields (or whatever works for you). Also keep in mind whenever an enemy breaks out of a waypoint loop to attack it will need to jump back into the loop once the battle is over. I think attack waypoints should be created on the fly in order to give the enemy a more human feel.

Function UpdateAI()

For a.ai = Each ai
    If a\static = False And a\angry = False ;if this AI actually moves in the game...and is not attacking
	For this.aiwaypoint = Each aiwaypoint
	    If this\num = a\nextwaypoint ;
		aivx# = EntityX( this\mesh, True ) - EntityX( a\mesh, True )
		aivy# = EntityY( this\mesh, True ) - EntityY( a\mesh, True )
		aivz# = EntityZ( this\mesh, True ) - EntityZ( a\mesh, True )
		AlignToVector( a\mesh, aivx#, aivy#, aivz#, 3, 0.03 )
					
                ;enemy has reached the destination...where to next?
		If EntityDistance(a\mesh,this\mesh) < 200
                        ;next waypoint choice logic here - time to bank maybe?  slow down? etc.
			a\nextwaypoint = a\nextwaypoint + 1
		EndIf
                ;last waypoint in the loop reached so go back to the first one
		If a\nextwaypoint = a\lastwaypoint Then a\nextwaypoint = 1
                ;Thrust logic
		MoveEntity a\mesh,0,0,a\thrust#

	    EndIf
	Next
    EndIf
Next
	
End Function


Try this basic function to keep your enemies from flying into things like the player, space junk, asteroids, other AI, etc.

Function avoid_entity( scr_entity, dest_entity, rate#, spd# )

	If EntityDistance(scr_entity,dest_entity) < 300 Then
		MoveEntity scr_entity,0,0,spd#
		dist1# = EntityDistance(scr_entity,dest_entity)
		MoveEntity scr_entity,0,0,-spd#
		TurnEntity scr_entity,0,rate#,0
		MoveEntity scr_entity,0,0,spd#
		dist2# = EntityDistance(scr_entity,dest_entity)
		MoveEntity scr_entity,0,0,-spd#
		TurnEntity scr_entity,0,-2*rate#,0
		MoveEntity scr_entity,0,0,spd#
		dist3# = EntityDistance(scr_entity,dest_entity)
		MoveEntity scr_entity,0,0,-spd#
		
		If dist1# > dist2# And dist1# > dist3# Then TurnEntity scr_entity,0,rate#,0
		If dist2# > dist3# And dist2# > dist1# Then TurnEntity scr_entity,0,2*rate#,0
		
		Return True
	Else
		Return False
	EndIf

End Function



Nate the Great(Posted 2008) [#4]
Hey!!! I remember writing that function (avoid entity) ... I had no Idea it would ever be useful!!! nice to know someone has found it :)


Ryudin(Posted 2008) [#5]
Thanks guys! I'll get this plugged into my game, add some little things in, and see how it works.


_PJ_(Posted 2008) [#6]
Depending on what sort of things you will need to consider, but a little addition from myself which I am developing for a project ( may or may not be the best approach, but here as a suggestion) is to add some pivots to the front and flanks and maybe above/below the spaceships or whatever, make these child entities of the spaceship. The local distance from 0 can be set dependant on speed of the ship.
If the FRONT pivot collides with a particular entity, and maybe the TOP/BOTTOM pivots too, then the ship can be made to alter pitch. Similarly, if the side pivots collide, then a banking motion can be introduced.

This gave some graceful turning motion á la Eve for AI ships flying through asteroids or 'busy' space.


Kryzon(Posted 2008) [#7]
You could also play games of the same genre you wanta to make and try to reverse engineer (by sheer interpretation) their AI steps. Do crazy things, see how they handle it.


Ratboy(Posted 2008) [#8]
The original Wing Commander AI was designed to fly "interestingly", which is, when being chased, the AI ships did cool looking maneuvers to try and shake you off their tail. I'd assume the actual motions were scripted, so that it'd play out Double-reverse Pogo Stick (note: made that name up on the spot), and when complete, it would check to see if you were still behind it. If not, it'd try to turn and attack, otherwise it'd pull another evasive maneuver out of its hat and run that one's script.

ISTR them having a decision tree based on relative positions of the player and enemy that governed chances of attacking, evading, or changing targets.


Heliotrope(Posted 2010) [#9]
some thinking
if enemyfores is bigger than your fores then run away
if your fores are bellow 50% to 40% of starting nnumber then take corage test


corage test


if corage test is fail then reterte
if corage test is epic fail then run away
if corage test is good then fight

thats all I can think of at the mounet>


Serpent(Posted 2010) [#10]
If I were you, first I would create some sort of pathfinding code so that you could tell a, say, spaceship to move to a point in 3D space. That way, you could set the ship to go anywhere and use the same function that smoothly rotates and moves the ship.

What would also be good is the use of types to set destinations. In this way, you could queue up a list of waypoints by creating a list of types, and then as the ship reaches each one it deletes the type and moves on to the next.

If you set up something like this, coding the AI will be made easier because all you have to do is figure out with your AI code where the ship should go to, and then the built-in functions for movement will take care of the realistic rotation, acceleration, etc.


EDIT: Didn't realise the post was more than a year old (only checked date on last post :P)