[QuickBox2D] Bizarre object
http://wonderfl.net/code/898980cb5e16ada8d1909eb8aa32e677696fc10b を作ろうとして失敗したもの。
@author paq89
/**
* Copyright paq ( http://wonderfl.net/user/paq )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/io6h
*/
package
{
import Box2D.Common.Math.b2Vec2;
import com.actionsnippet.qbox.objects.CircleObject;
import com.actionsnippet.qbox.QuickBox2D;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
/**
* http://wonderfl.net/code/898980cb5e16ada8d1909eb8aa32e677696fc10b を作ろうとして失敗したもの。
* @author paq89
*/
[SWF(backgroundColor="0xFFFFFF", width=465, height=465, frameRate=60)]
public class Main extends Sprite
{
private var _qbox:QuickBox2D;
private var _ball:/*Ball*/Array = [];
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_qbox = new QuickBox2D(addChild(new MovieClip()) as MovieClip, {gravityY:0});
_qbox.createStageWalls();
_qbox.mouseDrag();
_qbox.start();
_ball[0] = new Ball(_qbox, { x:1, y:1 } );
for (var i:int = 1; i < 7; i++)
{
_ball[i] = new Ball(_qbox, { x:i + 1, y:1 }, _ball[i - 1]);
_qbox.addJoint( { type:QuickBox2D.PRISMATIC, a:_ball[0].body, b:_ball[i].body } );
}
_ball[0].target = _ball[_ball.length-1];
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
for each (var ball:Ball in _ball)
{
ball.update();
}
}
}
}
import Box2D.Common.Math.b2Vec2;
import com.actionsnippet.qbox.QuickBox2D;
import com.actionsnippet.qbox.objects.CircleObject;
import com.actionsnippet.qbox.QuickObject;
class Ball extends CircleObject
{
private var vx:Number = 10;
private var vy:Number = 10;
private var spring:Number = 0.1;
private var friction:Number = 0.9;
private var vec:b2Vec2;
public var target:QuickObject;
public function Ball(qbox:QuickBox2D, params:Object, target:QuickObject = null)
{
super(qbox, params);
this.target = target;
vec = new b2Vec2();
}
public function update():void
{
if (!target) return;
var targetPos:b2Vec2 = target.body.GetPosition();
var thisPos:b2Vec2 = body.GetPosition();
var dx:Number = targetPos.x - thisPos.x;
var dy:Number = targetPos.y - thisPos.y;
var ax:Number = dx * spring;
var ay:Number = dy * spring;
vx += ax;
vy += ay;
vec.x = vx;
vec.y = vy;
body.ApplyForce(vec, thisPos);
vx *= friction;
vy *= friction;
}
}