AdvancED Flex 4 is now available to pre-order on Amazon.com. I had the privilege to work on this book with Elad Elrom and Shashank Tiwari. Once you get a copy please be sure to let us know what you think.
Charlie Schulze
Papervision2.com – Best resource for Papervision3d tutorials
AdvancED Flex 4 is now available to pre-order on Amazon.com. I had the privilege to work on this book with Elad Elrom and Shashank Tiwari. Once you get a copy please be sure to let us know what you think.
Charlie Schulze
This week's example / tutorial is how to mix Papervision with as3isolib and A* (pronounced A Star) for a nice 3D isometric pathfinding experience. The version of A* that I am using comes straight out of Keith Peters book AdvancedEd ActionScript 3.0 Animation. Anyone looking to cover advanced animation topics in Flash should buy a copy of this book. It is excellent.
This tutorial will not cover how to create the A* classes but rather show you how to blend 3 things together; Papervision, as3isolib, and A* (the Keith Peters version). If you're just getting started using as3IsoLib take a look at these great tutorials and download the code: http://code.google.com/p/as3isolib/w/list
We will cover a few of the things that you need to successfully merge the pathfinding and 3D experience.
First item is our camera setting. We are setting ortho to true because this actually gives us a true isometric camera feel.
Inside of our makeGrid function we are setting up the groundwork for our pathfinding and isometric world. This grid has no information about the cellsize, just which parts of it are walkable and not walkable.
Next we move on to drawGrid() which begins to connect the pathfinding and as3isolib
One of the great thing about as3isolib which is similar to papervision is that it has native objects that you can skin, such as boxes and polygons.
Here inside of drawGrid we run through the columns and rows that we setup in our makeGrid function. From the information in these loops we can get back the node information of the grid and check if an item is walkable or not walkable. If it is walkable we create a standard size box with a height of 0, if it is not walkable we create that same standard box but with a height of 40 so we can start to define our available paths. If the box is walkable we also want to set a mouseEvent so that we can select it. Finally we add these items to our isoScene.
Another item within the drawGrid function is our playerHelper. We are only using this playerHelper so that we can get it's coordinates to follow along in papervision. We set him to the same size as our box items.
The next important step is setting up a way to have our viewport inside of our isoScene. We first create a nice wrapper for our viewport which is a native IsoSprite object. We then associate our viewport as one of the sprites inside of our isoSprite.
Notice that we add our sprites kind of like we add filters for a movieclip. We are passing it an array.
Now lets see what happens when a grid item is clicked. First we gather the information about where we want to go. We gather that by getting the item that was clicked and getting its x & y positions divided by the cellSize. Then we give pathGrid.setEndNode those coordinates. Next we gather the information about where our playerHelper currently is divided by the cellSize and pass that information to pathGrid.setStartNode.
Finally we call findPath(). This is the fun part. First we create a new instance of AStar and gather the path information that we gathered when we ran the onGridItemClick function.
We can now run through that loop and get each x and y portion of that loop. We simply add a tween with a delay that is the same time as our speed. This will allow you to see each move and when it is complete it will start it's next move.
We now just need to look at how to sort the papervision with the as3isolib objects
Inside of our onRenderTick function which is an override from BasicView we use this to render our isoScene which is part of as3isolib and change the x,y coordinates of our sphere. We are basically following around the screenX and screenY of our playerHelper. The next and very important part of this is the depth sorting. We are pulling the depth at all times from the playerHelper object and assigning our isoSprite (which has our viewport in it) to that depth. Now we can watch as our 3D objects find their pretty little paths and sort without issue.
Here is the full code:
As a simple addition to yesterday's coverflow post, I wanted to show the same example but with loading images via XML. For simplicity sake the XML is loaded and parsed all in the main file.
The structure for our XML is very simple:
We simply load our XML file with BulkLoader
Parse the results, now adding the images to the BulkLoader:
When our images are ready we can continue the process of setting up our coverflow. But now using the images we just loaded for our materials.
It's that simple. Be sure to let us know if you find this useful and are able to use it in a project.
Here is the full code:
There are several great AS3 / Papervision Coverflows out there but today I set out to create one in it's simplest form. There are no bells and whistles, just a stripped down coverflow with it's core functionality. It's up to you to add an XML feed, Flickr feed, or setup your images in an array and load them the way you want to. This is only meant to be a clean jumping off point.
In the download I have included 2 versions. One with left / right buttons and one without. Both versions you can select the items in the coverflow to navigate.
The heart of this coverflow app is a lot like some of the others. We need to calculate the x and rotation of the center item, left items, and right items are, then animate accordingly. This is the same way that John Dyer animates his coverflow.
For this simple version of coverflow you have two ways of navigating to items.
Selecting a plane to jump right to that item
Or using left / right buttons to navigate one item at a time:
On to the full source code. This version includes the left / right buttons.
That's it. This should provide a good base for you to build out your own unique coverflow.