Mar 13

APE Tutorial/Example 2 - Flash Physics Engine: Adding walls and multiple collisions

Category: APE Tutorials, Physics, AS3, CS3

DOWNLOAD SOURCE

This example just builds off the last example but I was just having a little more fun. I added a few more walls and dynamically added wheels to the APEngine to see multiple wheel collisions. I also modified the default elasticity property of the wheel to make the collisions more bouncy. I know it’s not a whole lot different from the last one but is it an example of progression and elaboration. Once again you can plug in the code below on the first frame of a timeline and it should work as long as you are linked to the APE source files.// Imports
import org.cove.ape.*;
import flash.events.Event;
import flash.display.MovieClip// listeners
addEventListener(Event.ENTER_FRAME, run);// Create an instance of the APEngine.
APEngine.init(1/3);
APEngine.container = this;
APEngine.addForce(new VectorForce(false,0,2));

// Create group
var group1:Group = new Group();
group1.collideInternal = true;

// Create rectangle to serve as the floor.
var rect1:RectangleParticle = new RectangleParticle(280, 350, 130, 10, 0, true) // Bottom Right
var rect2:RectangleParticle = new RectangleParticle(120, 350, 130, 10, 0, true) // Bottom Left
var rect3:RectangleParticle = new RectangleParticle(60, 200, 10, 300, 0, true) // Left
var rect4:RectangleParticle = new RectangleParticle(340, 200, 10, 300, 0, true) // Right

// Add to group
group1.addParticle(rect1);
group1.addParticle(rect2);
group1.addParticle(rect3);
group1.addParticle(rect4);

// Add group to engine.
APEngine.addGroup(group1);

// Constantly run these functions to keep the engine running.
function run(evt:Event):void {
APEngine.step();
APEngine.paint();
}

// Add button
mcStart.addEventListener(MouseEvent.CLICK, mcStart_CLICK);
mcStart.buttonMode = true
function mcStart_CLICK(e:MouseEvent = null):void
{
var wheel:WheelParticle = new WheelParticle(50, 0, 10);
wheel.elasticity = .6
wheel.addForce(new VectorForce(false,100,0))
group1.addParticle(wheel);
}
mcStart_CLICK()

1 Comment so far

  1. Vern April 10th, 2008 5:19 am

    another nice tutorial…however, when I ran both examples on this page, APE did not know what a VectorForce(boolean,int,int) was. Maybe the most recent version of APE (at this time) has a different API from your version or VectorForce is a custom class of your own. Both of your examples worked fine if i replaced VectorForce(bool,int,int) with Vector(px,py)

Leave a comment