Basic Example

12 views
Skip to first unread message

Scottae

unread,
Oct 18, 2008, 4:47:09 PM10/18/08
to glaze Engine
Hey, I am digging the performance of the Glaze Engine examples. Very
nice. But looking through the source code of the example is a bit
intimidating. And I am not an ActionScript noob either. So I was
wondering if someone could post a Hello World code example with
Glaze. Basically, what is the code I need to get the simplest example
going. I am thinking a ball falling and bouncing on the floor. That
is a Hello World example for a physics engine. What are the essential
things I need to set up a Glaze project?

Thanks,
Scott

Scottae

unread,
Oct 18, 2008, 10:16:13 PM10/18/08
to glaze Engine
Well, forget it. I figured it out myself. For those of you that
might be curious......here's my code:



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;
}
}
}
}
Reply all
Reply to author
Forward
0 new messages