Saturday, October 18, 2008

Glaze Hello World

{{en|This is a screen capture animation from t...Image via WikipediaI've been looking all over the place for a good tutorial, example, whatever for Glaze. I am wanting to use Glaze because it appears to have the best performance of all the Flash 2D physics engines out there. Unfortunately there is no documentation, and the only example which you can view source for (in the demos package) is a bit complex. So I spent a little time looking through it and I figure out how to create what I consider a 2D physics "Hello World". Basically a ball falling to the floor and bouncing.

package {
import flash.display.Sprite;
import flash.events.Event;

import org.rje.glaze.engine.collision.shapes.Circle;
import org.rje.glaze.engine.collision.shapes.GeometricShape;
import org.rje.glaze.engine.collision.shapes.Polygon;
import org.rje.glaze.engine.dynamics.Material;
import org.rje.glaze.engine.dynamics.RigidBody;
import org.rje.glaze.engine.dynamics.joints.Joint;
import org.rje.glaze.engine.math.Vector2D;
import org.rje.glaze.engine.space.BruteForceSpace;
import org.rje.glaze.engine.space.Space;

/**
* The document class for my Glaze "Hello World" application
*
* @author scott
*/
public class GlazeTestDoc extends Sprite {

/** The space that all the physics objects are created in */
private var space : Space;

/** The dimensions of the space */
private var dim : Vector2D;

/** Color for static objects */
private var staticColour : uint = 0xE8EC95;

/** Whether to show the bounding box or not */
private var debug : Boolean = false;

/**
* Constructor function
*/
public function GlazeTestDoc() {

// Initialize the space
space = new BruteForceSpace(60, 90, null);
space.masslessForce.setTo(0, 200);

// Set dimensions
dim = new Vector2D(550, 400);

// Create the floor
createFloor();

// Create the ball
createCircle();

// Get things rolling
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

public function createFloor() : void {
var staticShape : GeometricShape = new Polygon(Polygon.createRectangle(dim.x, 40), new Vector2D(dim.x / 2, dim.y));
staticShape.fillColour = staticColour;
space.defaultStaticBody.addShape(staticShape);
}

private function createCircle() : void {
var cirBody : RigidBody = new RigidBody(RigidBody.DYNAMIC_BODY, 10, 100);
cirBody.p.setTo(300, 0);
var circ : GeometricShape = new Circle(18, Vector2D.zeroVect, new Material(55, 0.9, 1));
cirBody.addShape(circ);
space.addRigidBody(cirBody);
}

/**
* Triggered on the enter frame event. This is what makes the physics engine do its thing
*/
private function enterFrameHandler(pEvent : Event) : void {

// Clear the stage graphics (everything is drawn here for speed at the moment)
this.graphics.clear();

// Step the engine n times. The higher n, the more accurate things get.
space.step();

// Draw all the active shapes
var shape : GeometricShape = space.activeShapes;
while (shape) {
shape.draw(this.graphics, debug);
shape = shape.next;
}

// Draw all the static shapes
shape = space.staticShapes;
while (shape) {
shape.draw(this.graphics, debug);
shape = shape.next;
}

// Draw all the joints
var joint : Joint = space.joints;
while (joint) {
joint.draw(this.graphics, debug);
joint = joint.next;
}
}
}
}

1 comments:

Koen said...

Thanks man, very helpful!