Flash obstacle avoidance prototype

Read all posts about "" game

Obstacle avoidance can be very important in Flash gaming because allows designers to create smart enemies.

Obstacle avoidance behavior gives a character the ability to maneuver in a cluttered environment by dodging around obstacles.

In this prototype, I’ll try to simulate everyday life.

In everyday life, you walk straight until an obstacle appears in your line of sight.

Then, you slow down, and choose a random direction to cross the obstacle. If necessary, you make two or three steps back and approach the obstacle again.

Well, I hope you don’t act this way in your real life but this is what we are going to do in this prototype.

It’s up to you to improve it and make a more realistic movement.

In the project there are 20 random obstacles placed in the same way as seen on Create a Flash Game like Nano War, and an object linked as runner running through them.

Runner’s behavior is explained in the commented script

// movieclip that will host all obstacles
createEmptyMovieClip("obstacles", 1);
// placing 20 obstacles and the player
for (x=1; x<=21; x++) {
	// can_place = true: I can place the obstacle / false: I can't
	// initializing it to false to force entering the while loop
	can_place = false;
	// while loop to look for an empty space where to place the obstacle
	while (!can_place) {
		// setting can_place to true
		// you can always place an obstacle until you can't...
		can_place = true;
		// generating random x and y positions, and random width
		px = Math.floor(Math.random()*400)+50;
		py = Math.floor(Math.random()*400)+50;
		// if I am at the end of the cycle, it's time to place the runner
		if (x == 21) {
			w = 20;
		} else {
			w = Math.floor(Math.random()*100)+10;
		}
		// scanning all already placed obstacles
		for (y=x-1; y>=1; y--) {
			// determining the distance between the y-th obstacle and the one I am trying to place
			dist_x = px-obstacles["sphere_"+y]._x;
			dist_y = py-obstacles["sphere_"+y]._y;
			distance = Math.ceil(Math.sqrt(dist_x*dist_x+dist_y*dist_y));
			// this is the minimum distance I want between two obstacles:
			// the sum of both radiuses plus 20 pixels
			min_distance = (w+obstacles["sphere_"+y]._width)/2+20;
			// if the new obstacle would be too close to an existing one...
			if (distancemax_speed) {
			speed = max_speed;
		}
		// turn = 0 means "next time I will find an obstacle I will have to decide where to go"
		turn = 0;
	}
	// updating speed
	x_speed = speed*Math.cos(angle);
	y_speed = speed*Math.sin(angle);
	//updating position
	this._x += x_speed;
	this._y += y_speed;
	if (this._x>500) {
		this._x -= 500;
	}
	if (this._y>500) {
		this._y -= 500;
	}
	if (this._x<0) {
		this._x += 500;
	}
	if (this._y<0) {
		this._y += 500;
	}
};

and here it is

As you can see, sometimes the runner is quite dumb, but it's a good start to develop a more complex AI script for obstacle avoidance

Download the source code and enjoy

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers