In my last tutorial post, which was a while ago i made a simple example using APE just with solid objects. In this next tutorial i will show you how to make floppy bridges.
Here is what we will be making:
You can see that the physics simulator is pretty accurate, i really like the rope bridges that can be made and i used them a lot in the SteerWheels games. But i have created a function that automatically makes a rope bridge from some simple parameters. I have made this function for the tutorial and i wish i had made it for the steerwheels games.
To start off open a Flash CS3 document and set up the file like i showed you for the last tutorial, here.
The ActionScript is as follows:
-
stage.frameRate = 30;
-
import org.cove.ape.*;
-
import flash.events.Event;
-
addEventListener(Event.ENTER_FRAME, run);
-
var apeholder:MovieClip = new MovieClip();
-
apeholder.graphics.drawRect(0, 0, 550, 400);
-
addChild(apeholder);
-
var ballArray:Array = new Array();
-
APEngine.init(1/3);
-
APEngine.container = apeholder;
-
APEngine.addForce(new VectorForce(false,0,2));
-
var examplelevel:Group = new Group();
-
examplelevel.collideInternal = true;
-
var leftwall:RectangleParticle = new RectangleParticle(5,200,10,600,0,true);
-
leftwall.setStyle(3, 0x000000, 1, 0xFFFFFF,1);
-
examplelevel.addParticle(leftwall);
-
var rightwall:RectangleParticle = new RectangleParticle(545,200,10,600,0,true);
-
rightwall.setStyle(3, 0x000000, 1, 0xFFFFFF,1);
-
examplelevel.addParticle(rightwall);
-
for (var ballnum:int = 0; ballnum<20; ballnum++) {
-
var ball:CircleParticle = new CircleParticle(Math.random()*550,-50-Math.random()*150,15,false,2);
-
ball.setStyle(1, 0x00CC00, 1, 0x00CC00,1);
-
examplelevel.addParticle(ball);
-
ballArray.push(ball);
-
}
-
createBridge(530, -50, 6, 60, false);
-
createBridge(20, 50, 6, 60, true);
-
createBridge(530, 150, 6, 60, false);
-
createBridge(20, 250, 6, 60, true);
-
APEngine.addGroup(examplelevel);
-
function run(event:Event):void {
-
APEngine.step();
-
APEngine.paint();
-
for (var ballstocheck:int = 0; ballstocheck<ballArray.length; ballstocheck++) {
-
if (ballArray[ballstocheck].py>400) {
-
ballArray[ballstocheck].py = -50;
-
}
-
}
-
}
-
function createBridge(startx:Number, starty:Number, ballnum:int, ropedist:Number, right:Boolean) {
-
var bridgeArray:Array = new Array();
-
var reverse:int = 1;
-
if (right == true) {
-
reverse = 1;
-
} else {
-
reverse = -1;
-
}
-
for (var bn:int = 0; bn<ballnum; bn++) {
-
var bparticle:CircleParticle = new CircleParticle(startx+(bn*ropedist*reverse),starty+(bn*ropedist/2),5,false,2);
-
bparticle.setStyle(1, 0x0066CC, 1, 0x0066CC,1);
-
examplelevel.addParticle(bparticle);
-
bridgeArray.push(bparticle);
-
}
-
for (var cn:int = 0; cn<bridgeArray.length-1; cn++) {
-
var bridgeConn:SpringConstraint = new SpringConstraint(bridgeArray[cn], bridgeArray[cn+1], 1, true, 10, 1);
-
bridgeConn.setStyle(1, 0x0066CC, 1, 0x0066CC);
-
examplelevel.addConstraint(bridgeConn);
-
}
-
bridgeArray[0].fixed = true;
-
bridgeArray[bridgeArray.length-1].fixed = true;
-
}
Code explanation:
Lines 1-25: This is how i set up my APE examples, i have explained these lines in detail in this tutorial.
Lines 26-29: these are my createBridge functions. I will get back to these after i have explained the actual functions. For some reason i always put my functions at the bottom of my code.
Line 30: Adding the group to the Ape engine.
Lines 31-39: This is The function that makes the simulation move, it is exactly the same as it was in this tutorial.
Line 40: Finally, my create Bridge function. It has 5 parameters; startx and starty are the coordinate for the first particle in the bridge, it is always fixed. The next is ballnum which is the number of joints in the bridge. Ropebridge is the distance between the joints and the last parameter, right, is the direction in boolean form, true = right, false = left.
Line 41: This is an array that holds all the joints of the rope bridge.
Line 42: Setting up an number that is either 1 or -1 and will be used to send the bridge either left or right.
Lines 43-47: This is an if statement that checks to see if the boolean "right", mentioned earlier is true or false. If it is true than the number from line 42 will be 1 otherwise it will be -1.
Line 48: Setting up a for statement with that goes up to ballnum that we set when calling the function.
Line 49: Creating a circle particle with an x coordinate of startx+(bn*ropedist*reverse) which just means that for every particle created they will always be ropedist apart. if reverse is 1 than the bridge will go from left to right and if it is -1 than the reverse will happen. The y coordinate of the circle particle is starty+(bn*ropedist/2) which is similar to the x but it will be only half of the ropedist vertically and will only ever go downwards.
Line 50: Colouring the particle blue.
Line 51: Adding the particle to the group.
Line 52: Adding the particle to the array that holds all the joints.
Line 54: Setting up another for statement to handle the springs in between the joints. There is always going to be 1 less spring than there is joint so all we need to do is get the length of the joint array and take away 1.
Line 55: Creating a springConstant between 2 particles in the joint array. cn and cn + 1 . This is the reason we needed to minus 1 from the joint array length so that the springs can join to the joints.
Line 56: Setting the springs to the same blue colour.
Line 57: Adding the constraints, the springs, to the group.
Lines 59-60: Setting both ends of the bridge to fixed positions so that they don't move.
You should now be able to understand lines 26-29 where i create the bridges.
I hope this helps some people and post stuff that you make with it in the comments.





