27Mar
[AS 3] AS Physics Engine (APE) - Swinging Platform
4 comments so farI have been wondering how the swinging platform was created, so I built this little demo.
Here is the main Document Class APESwingingPlatform:
package {
import org.cove.ape.*;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class APESwingingPlatform extends Sprite {
private var swingingPlatform:SwingingPlatform;
public function APESwingingPlatform() {
// Initialize the engine. The argument here is the time step value.
// Higher values scale the forces in the sim, making it appear to run
// faster or slower. Lower values result in more accurate simulations.
APEngine.init(1/4);
// set up the default diplay container
APEngine.container = this;
// gravity -- particles of varying masses are affected the same
APEngine.addMasslessForce(new Vector(0, 3));
// groups - this class extends group
var swingingPlatform:SwingingPlatform = new SwingingPlatform(Math.random() * 0xffffff);
APEngine.addGroup(swingingPlatform);
var capsule:Capsule = new Capsule(Math.random() * 0xffffff);
APEngine.addGroup(capsule);
capsule.addCollidableList(new Array(swingingPlatform));
addEventListener(Event.ENTER_FRAME, run);
}
private function run(evt:Event):void {
APEngine.step();
APEngine.paint();
}
}
}
I used a class that is in the working examples folder of the APE code, Capsule.as:
package {
import org.cove.ape.*;
public class Capsule extends Group {
public function Capsule(colC:uint) {
var capsuleP1:CircleParticle = new CircleParticle(350,0,14,false,1.3,0.4);
capsuleP1.setStyle(0, colC, 1, colC);
addParticle(capsuleP1);
var capsuleP2:CircleParticle = new CircleParticle(375,0,14,false,1.3,0.4);
capsuleP2.setStyle(0, colC, 1, colC);
addParticle(capsuleP2);
var capsule:SpringConstraint = new SpringConstraint(capsuleP1, capsuleP2, 1, true, 24);
capsule.setStyle(5, colC, 1, colC, 1);
addConstraint(capsule);
}
}
}
I modified another class to come up with the SwingingPlatform Class:
package {
import org.cove.ape.*;
public class SwingingPlatform extends Group {
public function SwingingPlatform(colE:uint) {
// setting collideInternal allows the arm to hit the hidden stoppers.
// you could also make the stoppers its own group and tell it to collide
// with the SwingDoor
collideInternal = true;
// the tip of the swing
var swingDoorP1:CircleParticle = new CircleParticle(350,10,7);
swingDoorP1.mass = 0.001;
swingDoorP1.setStyle(1, colE, 1, colE);
addParticle(swingDoorP1);
// the swing anchor
var swingDoorP2:CircleParticle = new CircleParticle(200,200,10,true);
swingDoorP2.setStyle(1, colE, 1, colE);
addParticle(swingDoorP2);
// connects the the above
var swingDoor:SpringConstraint = new SpringConstraint(swingDoorP1, swingDoorP2, 1, true, 13);
swingDoor.setStyle(2, colE, 1, colE);
addConstraint(swingDoor);
var swingDoorAnchor:CircleParticle = new CircleParticle(600,200,2,true);
swingDoorAnchor.visible = true;
swingDoorAnchor.collidable = false;
addParticle(swingDoorAnchor);
var swingDoorSpring:SpringConstraint = new SpringConstraint(swingDoorP1, swingDoorAnchor, 0.02);
swingDoorSpring.restLength = 40;
swingDoorSpring.visible = true;
addConstraint(swingDoorSpring);
}
}
}
Categories: APE - Actionscript Physics Engine, Actionscript 3
Thursday, March 27th, 2008 at 9:40 am and is filed under APE - Actionscript Physics Engine, Actionscript 3. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Great job,
But…
How can we manage to make a “flipper” with this swing ?
Pleaaaaase, could you make a tuto next time ?
Best regards.
David
Hi David,
I am a little confused with what you mean by “flipper” - If you could describe a little more detail I would like to help out!
Thanks for looking, Morgan
Hi Morgan,
Sorry for my bad english…
I mean a right (or left) flip on a pinball table.
O-> <-O
…and by the way, if you can explain on the same tuto : how making a “plunger” with A.P.E (the thing that can launch the ball)
Cheer from France !
Ah, I see what you mean now. I think that is an awesome idea to make a little game.. things are a little busy right now, I am thinking this will be a good thing to do over the next week (maybe I can draw it out this weekend). So I guess.. stay tuned..