forked from: 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/vAXS
*/
// 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 JointGridOriginalMultiple extends MovieClip {
private const frequencyHz:Number = 0;
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 JointGridOriginalMultiple() {
sim = new QuickBox2D(this, { debug:false });
sim.gravity = new b2Vec2;
createSoftness(new Point(2*distance, 2*distance),1,0,0,0);
createSoftness(new Point(2*distance, 8*distance),0,1,0,0);
createSoftness(new Point(8*distance, 2*distance),0,0,1,0);
createSoftness(new Point(8*distance, 8*distance),0,0,0,1);
sim.createStageWalls();
sim.start();
sim.mouseDrag();
new PushButton(this,0,0,"g",gravityToggle);
t = new TextArea(this, 200, 0, "");
t.visible = false;
}
private function createSoftness(center:Point,firstSet:int=1,secondSet:int=1,thirdSet:int=1,fourthSet:int=1):void
{
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:0.1,
groupIndex:-1,
fixedRotation:true,
friction:0.1,
density:1,
linearDamping:1,
allowSleep:true,
fillAlpha:0.5});
line.push(dot);
}
}
sim.setDefault({type:QuickBox2D.REVOLUTE});
for (j = 0; j < dotNum; j++) {
for (i = 1; i < dotNum; i++) {
if(firstSet)sim.addJoint({a:dots[j][i-1].body, b:dots[j][i].body, frequencyHz:frequencyHz});
if(firstSet)sim.addJoint({a:dots[i-1][j].body, b:dots[i][j].body, frequencyHz:frequencyHz});
}
for (i = 2; i < dotNum; i++) {
if(secondSet)sim.addJoint({a:dots[j][i-2].body, b:dots[j][i].body, frequencyHz:frequencyHz});
if(secondSet)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++) {
if(thirdSet)sim.addJoint({a:dots[j-1][i-1].body, b:dots[j][i].body, frequencyHz:frequencyHz});
if(thirdSet)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++) {
if(fourthSet)sim.addJoint({a:dots[j-2][i-2].body, b:dots[j][i].body, frequencyHz:frequencyHz});
if(fourthSet)sim.addJoint({a:dots[j-2][i].body, b:dots[j][i-2].body, frequencyHz:frequencyHz});
}
}
}
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();
}
}
}
//public class JointGridOriginal