Archive for June, 2007

Fast Tile Engine

We have a few projects going on right now at Electrotank and more than one of them is tile based. So we have spent a good bit of time working on a flexible tile engine (in AS2) rather than writing just enough to get the job done…again.

We used every performance trick we know while still trying not to lose flexibility. What we didn’t plan on is how well it actually would end up performing! I have tested it with 800+ tiles and hundreds of on-screen items, in Isometric view, with depth sorting, and it will run 95-100 fps as a SWF and around 65fps in the browser!

I haven’t tried to see how big or how many items I can get in there. But nothing so far has dropped the frame rate. This goes for animated tiles, animated items, and even playing video on in-world items.

After all of that hype I wish I had a prettier link to show but I can’t show our current projects at the moment. All I can show is this fairly empty example. There are around 600+ tiles in this and maybe 20-30 buildings. It is set to run at 30 fps.

Check it out


Read Full Post »

In the fl.motion package there is a class called MatrixTransformer. It provides a bunch of methods that will come in handy on occasion. For instance, have you ever needed to rotate a movieclip around some point? I have, and I usually spend 30 minutes figuring it out each time I need to use it.

Well MatrixTransformer provides an easy way for you to do this with 1 line of code! In the fewest lines possible, here is how you can rotate a movie clips 45 degrees around the mouse position:

import flash.geom.Matrix;
import fl.motion.*;
var mat:Matrix = clip.transform.matrix;

The code above gets the clip’s current matrix, modifies it, and reapplies it back to the clip. Pretty slick! The MatrixTransformer class provides a few other useful methods. The only thing that I find a bit odd is that it takes degrees instead of radians.

Here is a more practical example use of this code. (Practical from a game programmer’s point of view.) Just click and hold on an object. It will hold that point it place as the object simulates a realistic rotation.

click to view example

Read Full Post »

I recently finished a small series of posts on the AI topic of emergent behavior. Now I will be posting a few entries on the AI topic of search algorithms, aka pathfinding.

Almost everything I program is from a game programmer’s perspective. As such, search algorithms are pathfinding algorithms as far as I’m concerned. In the most generic sense though a search algorithm finds…something. Path, state, item, what ever. During my posts I plan to cover at least Depth-first (that is this post), A*, and Dijkstra’s algorithm.

Depth-first search is an algorithm used to search tree structures. Let’s call the node you’re looking for the goal. Generally with a depth-first search you’re looking for either the location the goal or the path to it.

Here is the pseudocode for this algorithm:

find(n, goal)   if n == goal      end and return path   else      for all children of n          find(child, goal))

I am no pseudocode expert so I hope I formatted that in an understandable way. The concept though is this:

  • Write a function that takes a node and a goal node
  • If the node is the goal node, stop and return the path
  • If the node is not the goal node, then run this function for all children of the node

In my experience depth-first searches are suited well in games for two purposes:

  1. Generating perfect mazes. A perfect maze is a maze in which there exists exactly one unique path between any two cells.
  2. Finding a path in a very constricted environment, such as a maze.

I have used depth-first a few times and it has always been for mazes!

I have created an ActionScript 3 demo. This demo creates a perfect maze using depth-first search. It also finds a path between a start node and where ever you place your mouse real-time. This search also uses depth-first search.

Here is the logic for creating a maze:

  • Create a grid pattern of some size (mine is 20 X 20)
  • All nodes are initialized with all 4 walls turned on.
  • Pick any node to start with and run the following pseudocode

expand(n)  set n as visited  randomize order of n's neighbors  for all neighbors of n      if neighbor not visited          knock down wall between n and neighbor          expand(neighbor)

The logic above will touch every tile and create a unique looking maze. To find a path within the maze you can run a similar search from a starting node until you reach the destination node. In that search though you only consider neighbors of a node that have open pathways (eg doorways).


click to view example

Read Full Post »

Class Killer

I just spent over an hour trying to figure out why new instances of a class I wrote are coming back as undefined. That should never happen!

I’ll just jump to the punchline. All you have to do to kill your class is reference a class property in the constructor call (even a function called within the constructor) without using that variable reference. This class will compile fine but return ‘undefined’ when you create a new instance of it. Note that this is ActionScript 2.

class Test {
private var testVar:String;
public function Test() {

Murder most foul!

Just comment out the ‘testVar‘ line and everything works. Sure it is a code bug, but I sure didn’t expect this behavior.

Read Full Post »

ActionScript 3 provides a convenient way to solve quadratic equations. To refresh your memory, a quadratic equation takes this form:

a*x^2 + b*x+c = 0

Where a, b, and c are constants.

Solutions of a quadratic equation (its roots) may be real or imaginary. When getting the roots in ActionScript only real roots are considered. So, there can be 1, 2, or no roots found.

Here’s how you use it:

import fl.motion.*
var results:Array = BezierSegment.getQuadraticRoots(a, b, c);

I ran some speed tests of the above against solving it manually. The built-in way is about 40% faster. Here is how I tested it. I ran each way 1,000,000 times. I think its pretty amazing that we can do this 2,000,000 times in like 3 seconds.

function getQuadraticRoots(a:Number, b:Number, c:Number):Array {
var results:Array = new Array();
var sqt:Number = Math.sqrt(Math.pow(b, 2) - 4*a*c);
var root1:Number = (b+sqt)/(2*a);
var root2:Number = (b-sqt)/(2*a);
if (!isNaN(root1)) {
if (!isNaN(root2)) {
return results;
function testCustomFunction() {
var startTime:Number = new Date().getTime();
for (var i:int=0;i<1000000;++i) {
var results:Array = getQuadraticRoots(100*Math.random(), 100*Math.random(), 100*Math.random());
var endTime:Number = new Date().getTime();
trace("Custom time: "+(endTime-startTime));

import fl.motion.*;
function testBuiltInFunction() {
var startTime:Number = new Date().getTime();
for (var i:int=0;i<1000000;++i) {
var results:Array = BezierSegment.getQuadraticRoots(100*Math.random(), 100*Math.random(), 100*Math.random());
var endTime:Number = new Date().getTime();
trace("Built-in time: "+(endTime-startTime));

Read Full Post »

A few weeks ago I posted about a new game we created for MTV called Crowd Magnet. The goal of that game is to drive around and allow people to crowd around your car, and then to lead them to a dance club. To make this behavior at least somewhat realistic and interesting you need to try do the following things:

  • Attract people to the rear of the car
  • Repel people from the front of the car (to avoid being run over) if close enough
  • Allow people to stand close but not to collide with each other
  • Make a person flee from the car if they get annoyed (i.e. they are no longer attracted)

I’ve come up with a simple demo that shows most of this behavior at the link below. This demo is not interactive – just let it play.

This is my final post (for a while at least) on emergent behavior. I have some more ideas to explore on the subject when the time comes.

Next to come is a series of posts on search algorithms. As applied to games of course. This means pathfding, maze solving, etc.

Read Full Post »