Yangbo's Blog!

Artificial Intelligence Board Game.

Programming game AI(Steering behaivor) by (as3)example

November29

Steering behavior :?:

Example:

First off,let's overview this simple as3-based Steering behavior System:
1.Vehicle;
2.Steering Vehicle;
3.Behaviors;

Next,how to construct Steering Behavior using AS3?

Code snippet:


IVehicle;

/**
	 * The interface of vechicle type.
	 * all kinds of monster's type is vechicle.
	 *
	 * @author Knight.zhou
	 *
	 */
	public interface IVehicle
	{
		/**
		 * Sets / gets what will happen if character hits edge.
		 */
		function set edgeBehavior(value:String):void;
		function get edgeBehavior():String;
		/**
		 * Sets / gets mass of character.
		 */
		function set mass(value:Number):void;
		function get mass():Number;
		/**
		 * Sets / gets maximum speed of character.
		 */
		function set maxSpeed(value:Number):void;
		function get maxSpeed():Number;
		/**
		 * Sets / gets position of character as a Vector2D.
		 */
		function set position(value:Vector2D):void;
		function get position():Vector2D;
		/**
		 * Sets / gets velocity of character as a Vector2D.
		 */
		function set velocity(value:Vector2D):void;
		function get velocity():Vector2D;
		/**
		 * Sets x position of character. Overrides Sprite.x to set internal Vector2D position as well.
		 */
		function set x(value:Number):void
		/**
		 * Sets y position of character. Overrides Sprite.y to set internal Vector2D position as well.
		 */
		function set y(value:Number):void
		/**
		 * update vehicle 's properties.
		 */
		function update():void;
	}


ISteeringVehicle;

/**
	 *
	 * @author Knight.zhou
	 *
	 */
	public interface ISteeredVehicle extends IVehicle
	{
		function set maxForce(value:Number):void;
		function get maxForce():Number;

		function set inSightDist(value:Number):void;
		function get inSightDist():Number;

		function set tooCloseDist(value:Number):void;
		function get tooCloseDist():Number;

		function inSight(vehicle:VehicleSample):Boolean;
		function tooClose(vehicle:VehicleSample):Boolean;
	}


Behaviors;

IArrive

/**
	 *
	 * @author Knight.zhou
	 *
	 */
	public interface IArrive
	{
		function set arriveThreshold(value:Number):void;
		function get arriveThreshold():Number;

		function arrive(target:Vector2D):void;
	}


Supplmentary interpretation


Arrival

//----------------------------------
		//  arrive(implement IArrive)
		//----------------------------------
		/**
		 *
		 * @param target type is Vector2D
		 *
		 */
		private var _maxForce:Number = 100;
		private var _steeringForce:Vector2D = new Vector2D();
		public function arrive(target:Vector2D):void
		{
			var desiredVelocity:Vector2D = target.subtract(_position);
			desiredVelocity.normalize();

			var dist:Number = _position.dist(target);
			if(dist > _arriveThreshold)
			{
				desiredVelocity = desiredVelocity.multiply(maxSpeed);
			}
			else
			{
				desiredVelocity = desiredVelocity.multiply(maxSpeed * dist / _arriveThreshold);
			}

			var force:Vector2D = desiredVelocity.subtract(_velocity);
			_steeringForce = _steeringForce.add(force);
		}
		//----------------------------------
		//  update(override)
		//----------------------------------
		override public function update():void
		{
			_steeringForce.truncate(_maxForce);
			_steeringForce = _steeringForce.divide(mass);
			velocity = velocity.add(_steeringForce);
			_steeringForce = new Vector2D();

			//Handles all basic motion. Should be called on each frame / timer interval.

			// make sure velocity stays within max speed.
			velocity.truncate(maxSpeed);

			// add velocity to position
			_position = _position.add(_velocity);

			// handle any edge behavior
			if(edgeBehavior == WRAP)
			{
				wrap();
			}
			else if(edgeBehavior == BOUNCE)
			{
				bounce();
			}

			// update position of sprite
			x = position.x;
			y = position.y;

			// rotate heading to match velocity
//			rotation = _velocity.angle * 180 / Math.PI;
		}
		//----------------------------------
		//  bounce(private)
		//----------------------------------
		/**
		 * Causes character to bounce off edge if edge is hit.
		 */
		private function bounce():void
		{
			if(stage != null)
			{
				if(position.x > stage.stageWidth)
				{
					position.x = stage.stageWidth;
					velocity.x *= -1;
				}
				else if(position.x < 0)
				{
					position.x = 0;
					velocity.x *= -1;
				}

				if(position.y > stage.stageHeight)
				{
					position.y = stage.stageHeight;
					velocity.y *= -1;
				}
				else if(position.y < 0)
				{
					position.y = 0;
					velocity.y *= -1;
				}
			}
		}
		//----------------------------------
		//  wrap(private)
		//----------------------------------
		/**
		 * Causes character to wrap around to opposite edge if edge is hit.
		 */
		private function wrap():void
		{
			if(stage != null)
			{
				if(position.x > stage.stageWidth) position.x = 0;
				if(position.x < 0) position.x = stage.stageWidth;
				if(position.y > stage.stageHeight) position.y = 0;
				if(position.y < 0) position.y = stage.stageHeight;
			}
		}
This page is wiki editable click here to edit this page.
posted under AI, Flex/AS3/Flash
3 Comments to

“Programming game AI(Steering behaivor) by (as3)example”

  1. Avatar January 24th, 2010 at 8:52 latisse Says:

    good stuff for lashes


  2. Avatar March 22nd, 2010 at 14:46 Антон Павлович Says:

    Next,how to construct Steering Behavior using AS3? Code snippet: IVehicle; /** * The interface of vechicle type…..

    Этого я не говорил….


  3. Avatar April 17th, 2010 at 8:32 Устинов Says:

    По-моему это очевидно. Рекомендую поискать ответ на Ваш вопрос в google.com


Email will not be published

Website example

Your Comment:

 

BLOG CALENDAR

September 2010
M T W T F S S
« Apr    
 12345
6789101112
13141516171819
20212223242526
27282930