I have just read the first 20 or so pages of the good book on AI Artificial Intelligence for Games
and I though I was going to try the first easy algorithms that are explained there. So here I will introduce a little bit of the simplest way to develop the seek and arrive algorithm.
I will develop the example in Ruby using the gosu library to draw some simple characters.
The idea of the seek algorithm we are going to implement is very easy, we will control a character in the screen and another computer controlled character will seek our character on the screen and destroy him when he arrives to our character.
So the first thing to know is what is involved in a very simple movement. In the example we will not consider acceleration forces so we will have what is known as Kinematic movement. We will need 3 variables to express the characters static data at moment in time. So we can start with a Ruby module like this:
- module Kinematic
- attr_reader :velocity, :orientation, :position
- end
where all three elements are vectors. The velocity vector will give us the direction and speed of the characters, the orientation vector, in our case will simply be oriented towards the direction, so it will be the velocity vector normalized, and the position is the place where our characters are.
The orientation will also be expressed as a angle in radians using the atan2(-x,y) formula, where y and x are the corresponding coordinates in the velocity vector.
So the position and orientation will be both a function of the velocity like this:
- orientation = velocity.normalize
- position = velocity * time
where time is a small unit of time that will be a function of the frame rate we have. When the frame rate is bigger, the update time is smaller. It is calculated in our case something like this:
Let’s suppose our character travels at 2 meters per second, and let the frame rate be 60fps in a particular moment. then our time multiplier will be 1/60 which multiplied by 2 will be 1/30 that will be the length of our vector to be summed to the position vector in each frame. However we will change the value and adjusted to some value that makes the movement look good.
Ok so that is the basic movement, but now we need to implement the seek behaviour. The AI character will need to chase our own character in the screen, in the algorithm terminology we’ll be the target of the seek and arrive algorithm. So logically for implementing this algorithm we need both the character and the target kinematic data. We also need to specify a radius of contact (where the character catches the target) and a speed for our velocity vector.
So to our module we add this max_speed
- module Kinematic
- attr_reader :velocity, :orientation, :position, :max_speed
- end
- class Target
- include Kinematic
- end
- class Character
- include Kinematic
- end
Both character and target include the module, but only the character will be AI controlled, the target will be controlled by ourselves.
So we will create the seek_and_arrive algorithm on the character, in a method that receives the character it is chasing.
First the seek part will be simply to create a velocity of speed ‘max_speed’ and direction pointing to the target’s position. Now for the arrive part we will use a ’radius of impact’ that determines when the character has actually reached the target. We will include this radius as information on the target character. So modifying the algorithm we now have:
- def seek_and_arrive(target)
- @position += @velocity * @time_update
- @velocity = target.position - position
- if @velocity.magnitude < target.radius
- EventHandler::add_event(:capture,self,target)
- end
- @velocity = @velocity.normalize
- @velocity *= @max_speed
- @orientation = @velocity.normalize
- end
As we see we are simply adding a condition and then sending an event saying that the target has been captured by the character. This event will be handled in the main loop of the game where it will show Game Over.
We will now create the graphics for the game with Gosu, I won’t explain much here as it is not the focus of the post.
The first thing, we create a character wrapper for our characters that will know about gosu, that way our original class remains graphics framework independent:
- class DrawableCharacter
- attr_reader :character
- def initialize(character,window,character_img)
- @image = Gosu::Image.new(window, character_img, false)
- @character = character
- end
- def draw
- @image.draw_rot(@character.position[0], @character.position[1], 1, @character.orientation_in_radians)
- end
- end
then we create a class for the controlled character and one for the AI Character:
- class ControllableCharacter < DrawableCharacter
- def move(side)
- @character.move_ahead if side==:front
- change_velocity_according_to_side(side)
- end
- def change_velocity_according_to_side(side)
- return if side == :front
- if side == :right
- sin_radians = Math::sin 0.1
- cos_radians = Math::cos 0.1
- else
- sin_radians = Math::sin -0.1
- cos_radians = Math::cos -0.1
- end
- velocity_x = @character.velocity[0]*cos_radians - @character.velocity[1]*sin_radians
- velocity_y = @character.velocity[0]*sin_radians + @character.velocity[1]*cos_radians
- @character.velocity = Vector[velocity_x,velocity_y]
- @character.velocity = @character.velocity.normalize * (@character.max_speed+1)
- end
- end
- class AICharacter < DrawableCharacter
- def seek_and_arrive(target)
- @character.seek_and_arrive(target)
- end
- end
The Controllable character will move depending on input from the keyboard that is captured on the main Game class. The AICharacter delagates its movement to the Character class that contains the seek_and_arrive algorithm.
Now the main Game class:
- class Game < Gosu::Window
- def initialize
- super 1024, 768, false
- self.caption = "Seek and Arrive"
- @target = ControllableCharacter.new(Target.new(10, 10), self, 'target.gif')
- @character1 = AICharacter.new(Character.new(500, 500), self, 'character.gif')
- @game_state = :game_started
- end
- def manage_ai_characters
- @character1.seek_and_arrive(@target.character)
- end
- def manage_controllable_character
- if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft then
- @target.move :left
- end
- if button_down? Gosu::KbRight or button_down? Gosu::GpRight then
- @target.move :right
- end
- if button_down? Gosu::KbUp or button_down? Gosu::GpButton0 then
- @target.move :front
- end
- end
- def manage_events
- EventHandler::each do |event|
- if event[0]==:capture
- @game_state = :game_over
- end
- end
- end
- def update
- manage_events
- if @game_state != :game_over
- manage_ai_characters()
- manage_controllable_character()
- end
- end
- def draw
- if @game_state != :game_over
- @target.draw
- @character1.draw
- else
- Gosu::Image.new(self, "game_over.gif", true).draw(0, 0, 0);
- end
- end
- end
- window = Game.new
- window.show
The full source code of the example is in github, just download and run the game.rb ruby file.
Of course this introduction is the simplest of the simplest in Game AI, but it is important information to have and very entertaining to learn.
Also of course there are libraries and frameworks that do most of the work for us, but I did this example (and hopefully some following ones) to learn the basics of how it works.