Archive for the ‘source code’ Category

First steps with Flint Particles

Friday, January 16th, 2009

I’ve been meaning for a while to have a look at Flint and so here are my first steps with it.

First off, I like the way it is arranged. It is incredibly modular. Whilst this probably has implications as far as optimisation (although it seems to handle a whole load of particles pretty nicely), it does mean that any kind of particle system can be plugged together easily.

There are 2 basic objects that need to be instantiated – an emitter and a renderer. There are a few different types of renderer, for different types – Bitmap, DisplayObject and Pixel.

Once you have your emitter created, then the behaviour is adjusted by adding 4 things.

Initialisers.
These are used when a particle is created, so things like colour, mass, size, image to use etc.

Actions.
These are applied to the particle every frame. Things like gravity, collisions etc.

Zones
This an area of the screen, commonly a point, line, rectangle or circle. Zones can be used for some initialisers, for example position and velocity. A Zone used for position will be used to randomly place a particle in that area. The velocity zone creates the velocity based on the time taken for the particle to travel from [0,0] to the randomly selected point. Zones can also be used as actions. For example, a DeathZone is an area that removes particles that enter (or leave) it, and a Jet zone is one that adds a velocity to the particle if it is inside.

Activities
These modify the behaviour of the emitter every frame, for example making it follow the mouse or rotating.

Most of these have a 2D and 3D counterpart.

Right. Enough explanation. Here’s the first experiment:
(Please note there is a bug (in my code) that occasionally comes up where the particles are duplicated endlessly. You have been warned!) This has now been fixed :)
(more…)

Dynamic Framerates

Wednesday, November 5th, 2008

One thing that I’ve started using in most of my projects where the processor could get hit heavily is a dynamic rendering system. This seems to be working really well, so I thought I’d share it.

I came across the idea from this post over at 8 bit rocket. The idea is pretty simple. A timer is set to run as fast as possible. Every time the timer executes, it runs a render cycle and a code cycle. It then checks how long the total cycle took and does 1 of 2 things. Either it sleeps for a certain amount of time, so that the swf doesn’t run too fast, or the next render cycle is dropped so the player can catch up. It’s a little more involved than that so if you want to know the finer details, head on over there.

I made some crucial adjustments to the code that’s presented there however. The biggest one was to make the code into a class, so that I could use it more easily. I’ve set up a system for registering renderers and updaters so they can be added and removed from the timer, and also added some events that get dispatched on render and update cycles. It works really well if you have an MVC setup, since the model and the view are already separate.

The source code is available here. Documentation after the jump.
(more…)

Lightwave -> Papervision3D

Thursday, October 30th, 2008

In this post, I’m going to go through how to get a textured model from Lightwave into Papervision3D. It’s really not as easy as I should be because of the lack of a collada exporter in Lightwave. The title should actually be more like: Lightwave -> wavefront obj file -> Blender -> dae file -> Papervision3D.

I use Lightwave for no other reason than I used to use it back in my Amiga days and since I rarely need to do any 3d work, there seems little point in learning another 3d package when I have so much else to learn :) They have a ‘discovery’ (ie free) edition too.

There are many, many pitfalls in this process, but once you’re aware of them, it doesn’t take long to do. I’m going to assume that you know nothing about Blender (as I did 8 hours ago) but do know how to use Lightwave. If you don’t know either, you might be better off either learning Blender straight off (or Maya). Props to Altered Egg for the details on what to do in Blender. He’s got pictures and stuff, if you need them :) I’m using Lightwave 9 and Blender 2.48
(more…)

Never sort, always compare

Friday, September 26th, 2008

I had an instance today where I needed to find the boundaries of a collection of points. All of the points were in an array, so I thought, “OK, I’ll do a sortOn for x and then get the top and bottom values and then do the same for y. This worked, but was really slow. After doing a few tests, I realised the quicker way was to loop through the array and compare to an initial value. An added bonus is that I only need to loop through the array once for any number of values. Here’s the code:


var xLo:Number =
var xHi:Number = points[0].x;
var yLo:Number =
var yHi:Number = points[0].y;

var len:int = points.length;
for (var i:int = 0; i < len; i++)
{
var pt:Point = points[i] as Point;
if (pt.x < xLo) xLo = pt.x;
if (pt.x > xHi) xHi = pt.x;
if (pt.y < yLo) yLo = pt.y;
if (pt.y > yHi) yHi = pt.y;
}

Back To The Trees! Source

Wednesday, September 24th, 2008

Here’s the source from my last tree experiment. Please note that it’s the source folder from Flex project, but of course you can just use the classes…

If you do use it in a Flex Project, you’ll need to import the playerglobal.swc from Flash for it to work (it uses BezierSegment from the fl.motion package)

get the source code

I should add that this code is far from optimised. The bezier curve class I’m using wasn’t really designed for this, so it is very slow when smoothing as it iterates over all of the points. I’m going to be optimising this soon so keep an eye out!