Here is the somewhat-polished version of my pathfinding/game application, all heavily commented in tutorial format for your analyzing pleasure. New to the demo is graphics displaying the internal workings of the pathfinding and collision detection, less brain-dead AI and the ability to add stuff to the map.
Here is the application: Create a few “guys,” click to select, click elsewhere to move. Or add a tower near the guys. Ctrl+Click to add multiple units; Escape to cancel character selection or drop the current “tool.”
I have dubbed my primitive A* pathfinder “AStarOrthogonal” to make it clear: this pathfinder does not work diagonally.
I’m still working out some glitches in the “dynamic” collision handling (between mobile units, mainly on head-on collisions). Sometimes the movement isn’t quite right, and sometimes characters end up coinciding. This isn’t a problem with the pathfinder, it’s with the “game” code–specifically the Character “state machine.”
The first red cell leading character movement is the square currently tracked by the pathfinder. The leading-leading square is not used at the moment, but I’m experimenting with better collision avoidance by utilizing that future location.
If you’re going for something along the lines of the typical, ubiquitous “Tower Defense” game, I’ve already written half the code for you. : )
A couple of technical notes:
This is a learning process for me; keep that in mind. I am but a lowly game programming “hobbyist.” So, if you’re learning from this, don’t assume that everything I’ve done was done the “best” way.
If you’re using Flash, CS4 is required by the POLYGONAL data structures library. If you don’t use another IDE (i.e. Flex) and you don’t have CS4, I have at this point only used Singly Linked Lists and Doubly Linked Lists from the library, so it should be easy to find another library that does the trick.
Resources:
The code download includes the POLYGONAL data structure library from http://code.google.com/p/polygonal/wiki/DataStructures. I’ve included the library for convenience, but you might want to check the project home to be sure and have the latest version.
As in all my demos, the non-original graphics are from, or are derived from, the work of Azure Flame.
Well, this is kind of sad – like saying goodbye to a long-time friend for good.
BoxCAD will be back – but only in name. As the complexity of the application has grown, it’s shaky foundation is proving extremely inefficient and bug-prone to work with.
BoxCAD was my first proper AS3 application, and my first non-trivial application in general. Eight months ago, when I started the project – I knew very little of AS3 or design patterns – and it shows.
So I am starting from scratch. My plans for the final product are as big as ever, but I’m taking a step back at the moment to do it right.
One note of interest – I will be developing much of the functionality as independant classes in a library that can be applied to other similar applications. Taken as a whole, it will almost be a game framework – except that the objects will have no interdependence. Updates, of course, will be posted here.
Regarding the source code – please read the readme at /BoxCAD/readme.txt!
Well, after many moons of delay, BoxCAD now generates pristine AS3 code. Only for fundamental objects and constraints (none of the fancy stuff yet, like particle engines/controllers/etc), but still a very useful subset of Box2D’s functionality.
Here’s a clip of the code output from the MoonTank file:
// New Polygon Shape & Body.polygonDef = new b2PolygonDef();polygonDef.vertexCount = 4;polygonDef.vertices[0].Set(0.6000000000000005, -0.19999999999999996);polygonDef.vertices[1].Set(0.6000000000000005, 0.19999999999999996);polygonDef.vertices[2].Set(-0.6000000000000005, 0.19999999999999996);polygonDef.vertices[3].Set(-0.6000000000000005, -0.19999999999999996);polygonDef.friction = 0.2;polygonDef.restitution = 0.4;polygonDef.density = 1;polygonDef.filter.groupIndex = -1;bodyDef = new b2BodyDef();bodyDef.position = new b2Vec2(4.056582535099032, 18.045220905705396);bodyDef.angle = 0.41509286801864664;newBody = m_world.CreateBody(bodyDef);newBody.CreateShape(polygonDef);newBody.SetMassFromShapes();arrBodyList["30075"] = newBody;// New Polygon Shape & Body.polygonDef = new b2PolygonDef();polygonDef.vertexCount = 4;polygonDef.vertices[0].Set(0.6000000000000005, -0.19999999999999996);polygonDef.vertices[1].Set(0.6000000000000005, 0.19999999999999996);polygonDef.vertices[2].Set(-0.6000000000000005, 0.19999999999999996);polygonDef.vertices[3].Set(-0.6000000000000005, -0.19999999999999996);polygonDef.friction = 0.2;polygonDef.restitution = 0.4;polygonDef.density = 1;polygonDef.filter.groupIndex = -1;bodyDef = new b2BodyDef();bodyDef.position = new b2Vec2(3.08373731317662, 17.90830170541888);bodyDef.angle = -0.1395542297981954;newBody = m_world.CreateBody(bodyDef);newBody.CreateShape(polygonDef);newBody.SetMassFromShapes();arrBodyList["29906"] = newBody;// New Revolute Joint.rJointDef = new b2RevoluteJointDef();rJointDef.Initialize(arrBodyList['30118'], arrBodyList['30120'], new b2Vec2(9.062264928353713, 20.28247519314447));rJointDef.enableLimit = true;rJointDef.lowerAngle = 0;rJointDef.upperAngle = 0;m_world.CreateJoint(rJointDef);// New Revolute Joint.rJointDef = new b2RevoluteJointDef();rJointDef.Initialize(arrBodyList['30114'], arrBodyList['30118'], new b2Vec2(3.3360183210518954, 19.479990925312997));rJointDef.enableLimit = false;rJointDef.lowerAngle = 0;rJointDef.upperAngle = 0;m_world.CreateJoint(rJointDef);
That is two tread segments and two revolute joints. Imagine doing 20 tread segments by hand….
To make the development of flash physics apps even easier – here is a basic application skeleton. It supplies a convenient home, with basic mouse interaction, for BoxCAD’s output.