APE – Actionscript Physics Engine – Test 1

actionscript 3, APE, fun, Math, Physics, simulation

Hi there, I started today playing with APE – Actionscript Physics Engine (Alpha 0.45) and it’s really cool stuff.

My intention would be creating a physical based little funny game, but I need to start with some simple experiments so why not post’em here…

There are issues I still don’t understand so don’t know if are bugs or simply I should read APIs deeper. One thing I cannot realize is why if I change runtime the height or width properties of a RectangleParticle or the radius property of a CircleParticle, collisions are calculated correctly upon the new values, but cannot really manage to force a correct repaint with the new dimensions.
In the code that follows I have trhee rotating RectangleParticles with the alwaysRepaint parameter set to true and I can correctly see the rectangles rotating, but if I change size runtime nothing changes (except the collision). Neither calling the particle.paint() method at everyframe, which is an alternative to alwaysRepaint method, works.

However everything else is really supercool and moving a lot of self colliding particles @60fps!

Here we go with my first test:


ape_test11.gif
Run the test

Simply set the import folders for external classes in your Flash or Flex project pointing to the folder where you downloaded APE and copy these few lines of code to the first frame in you timeline.

import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.filters.GlowFilter;
import org.cove.ape.*;
import com.oaxoa.components.FrameRater;

stage.frameRate = 60;
addEventListener(Event.ENTER_FRAME, run);

APEngine.init(1/4);
APEngine.container = this;

// masslessforce aka gravity
APEngine.addMasslessForce(new Vector(0,4));

var defaultGroup:Group = new Group();
defaultGroup.collideInternal = true;

// instance the three rectangles
var rp:RectangleParticle = new RectangleParticle(150,250,250,25,0,true);
var rp2:RectangleParticle = new RectangleParticle(400,250,250,25,0,true);
var rp3:RectangleParticle = new RectangleParticle(275,450,150,20,0,true);

// set some graphical style
rp.setFill(0x666666);
rp2.setFill(0x666666);
rp3.setFill(0x666666);
rp.setLine(0, 0xff0000, 0);
rp2.setLine(0, 0xff0000, 0);
rp3.setLine(0, 0xff0000, 0);

// fixed particles are not always repainted for speed issues, but these ones will
// be rotating, so we need to force the repaint at every frame
rp.alwaysRepaint=true;
rp2.alwaysRepaint=true;
rp3.alwaysRepaint=true;

// add rectangles
defaultGroup.addParticle(rp);
defaultGroup.addParticle(rp2);
defaultGroup.addParticle(rp3);

APEngine.addGroup(defaultGroup);

var a:Number=0;
var r:Number=15;
function run(evt:Event):void {
	// check for removal if the particle is offscreen
	for each (var p:AbstractParticle in defaultGroup.particles) {
		if (p.py>550+r || p.px<-r || p.px>550+r) {
			defaultGroup.removeParticle(p);
		}
	}
	
	// some rotating fun
	a+=4;
	var arad:Number=a/180*Math.PI;
	rp.angle=Math.cos(arad)*45;
	rp2.angle=-Math.cos(arad)*45;
	rp3.angle=a*2;
	
	// do the simulation and the render step
	APEngine.step();
	APEngine.paint();
	
}
// timer to add particles
var timer:Timer=new Timer(50);
timer.addEventListener(TimerEvent.TIMER, ontimer);
timer.start();
function ontimer(event:TimerEvent):void {
	var cp:CircleParticle = new CircleParticle(Math.random()*350+100,-20,Math.random()*r+1);
	cp.setFill(0x444444*Math.random()+0xbbbbbb);
	cp.setLine(0, 0xff0000, 0);
	defaultGroup.addParticle(cp);
}

// add the frame rater to check how smooth it is running
var fr:FrameRater=new FrameRater(0xcccccc);
addChild(fr);

Byez

7 Comments

5 Comments

  1. Vyper  •  Mar 22, 2008 @11:10 pm

    Very smooth! I like how the bottom rectangle sometimes ‘catches’ and swings around some of the particles :)

  2. Pierluigi Pesenti  •  Mar 23, 2008 @1:12 am

    Yeah, me too! Maybe sometimes is a little eccessive. Dunno if it is actually correct but looks cool :-D

  3. multitouch barcelona  •  Mar 25, 2008 @10:01 pm

    Hi!
    You might already know… but BOX2D physics engine people have recently translated her code form C++ to AS3.
    It’s a really powerful engine, even better than ape:
    give it a try :)

    http://box2dflash.sourceforge.net/

    Keep up with your blog!It’s nice! ( :

  4. Harry  •  Dec 31, 2008 @9:46 am

    I keep getting the error ’1046: Type was not found or was not a compile-time constant: IForce.’ – verified that IForce.as was there, and classpaths all check out …so ????

  5. bator  •  Sep 8, 2009 @5:40 pm

    to Harry:
    replace @ line 1046
    this:
    APEngine.addMasslessForce(new Vector(0,4));
    with this:
    APEngine.addForce(new VectorForce(false,0,4));

2 Trackbacks

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>