forked from: QuickBox2D でぽにょぽにょさせてみた
/**
* Copyright aobyrne ( http://wonderfl.net/user/aobyrne )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/nsxS
*/
// forked from Akiyah's QuickBox2D でぽにょぽにょさせてみた
package {
import flash.events.Event;
import flash.display.*;
import com.actionsnippet.qbox.*;
import flash.geom.*;
import Box2D.Common.Math.b2Vec2;
import com.bit101.components.PushButton;
import com.bit101.components.TextArea;
public class FlashTest extends MovieClip {
private const frequencyHz:Number = 3;
private const distance:Number = 1;
private const dotNum:int = 4 + 1;
private var sim:QuickBox2D;
private var bGravity:Boolean;
private var t:TextArea;
public function FlashTest() {
sim = new QuickBox2D(this, { debug:true });
sim.gravity = new b2Vec2;
var center:Point = new Point(2*distance, 2*distance);
var dots:Array = [];
var i:int;
var j:int;
var dot:QuickObject;
for (j = 0; j < dotNum; j++) {
var line:Array = [];
dots.push(line);
for (i = 0; i < dotNum; i++) {
dot = sim.addCircle({x:center.x + i*distance,
y:center.y + j*distance,
radius:distance/2 * Math.sqrt(2),
groupIndex:-1,
fixedRotation:true,
friction:0.1,
density:1,
linearDamping:1,
allowSleep:false});
line.push(dot);
}
}
for (j = 0; j < dotNum; j++) {
for (i = 1; i < dotNum; i++) {
sim.addJoint({a:dots[j][i-1].body, b:dots[j][i].body, frequencyHz:frequencyHz});
sim.addJoint({a:dots[i-1][j].body, b:dots[i][j].body, frequencyHz:frequencyHz});
}
for (i = 2; i < dotNum; i++) {
sim.addJoint({a:dots[j][i-2].body, b:dots[j][i].body, frequencyHz:frequencyHz});
sim.addJoint({a:dots[i-2][j].body, b:dots[i][j].body, frequencyHz:frequencyHz});
}
}
for (j = 1; j < dotNum; j++) {
for (i = 1; i < dotNum; i++) {
sim.addJoint({a:dots[j-1][i-1].body, b:dots[j][i].body, frequencyHz:frequencyHz});
sim.addJoint({a:dots[j-1][i].body, b:dots[j][i-1].body, frequencyHz:frequencyHz});
}
}
for (j = 2; j < dotNum; j++) {
for (i = 2; i < dotNum; i++) {
sim.addJoint({a:dots[j-2][i-2].body, b:dots[j][i].body, frequencyHz:frequencyHz});
sim.addJoint({a:dots[j-2][i].body, b:dots[j][i-2].body, frequencyHz:frequencyHz});
}
}
sim.createStageWalls();
sim.start();
sim.mouseDrag();
new PushButton(this,0,0,"g",gravityToggle);
t = new TextArea(this, 200,0,"");
}
private function cTrace(msg:String):void
{
t.text +="\n"+msg;
}
private function gravityToggle(e:Event):void
{
bGravity=!bGravity;
sim.stop();
if(bGravity)
{
cTrace('true');
//sim.w.SetGravity(new b2Vec2(0,10));
sim.gravity = new b2Vec2(0,10);
}
else
{
cTrace('false');
//sim.w.SetGravity(new b2Vec2(0,0));
sim.gravity = new b2Vec2(0,0);
}
sim.start();
}
}
}