b2RevoluteJointの使い方が分からない
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
[SWF(width = 465, height = 465, backgroundColor = 0x000000, frameRate = 60)]
public class Main extends Sprite
{
public var m_sprite:Sprite;
public var m_world:b2World;
public var m_physScale:Number = 30.0;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
public var m_mouseJoint:b2MouseJoint;
static public var mouseXWorldPhys:Number;
static public var mouseYWorldPhys:Number;
static public var mouseXWorld:Number;
static public var mouseYWorld:Number;
private var mousePVec:b2Vec2 = new b2Vec2();
private var mouseDown:Boolean = false;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, initialize);
}
public function initialize(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, initialize);
//コンテナ
m_sprite = new Sprite();
addChild(m_sprite);
stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.CLICK, onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeave);
//WORLD
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000.0, -1000.0);
worldAABB.upperBound.Set(1000.0, 1000.0);
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
//デバック用
var dbgDraw:b2DebugDraw= new b2DebugDraw();
var dbgSprite:Sprite= new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite= dbgSprite;
dbgDraw.m_drawScale= 30.0;
dbgDraw.m_fillAlpha= 0.3;
dbgDraw.m_lineThickness= 1.0;
dbgDraw.m_alpha=1.0;
dbgDraw.m_xformScale=1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit;
m_world.SetDebugDraw(dbgDraw);
createWall();
createObject();
}
private function createWall():void
{
var wallObject:b2PolygonDef = new b2PolygonDef();
wallObject.density = 0.0;
var wallDef:b2BodyDef = new b2BodyDef();
var wallBody:b2Body;
//Left
wallDef.position.Set(10/2 / m_physScale, stage.stageWidth/2 / m_physScale);
wallObject.SetAsBox(10/2 / m_physScale, stage.stageWidth/2 / m_physScale);
wallBody = m_world.CreateBody(wallDef);
wallBody.CreateShape(wallObject);
wallBody.SetMassFromShapes();
//Right
wallDef.position.Set((stage.stageWidth-10/2) / m_physScale, stage.stageWidth/2 / m_physScale);
wallObject.SetAsBox(10/2 / m_physScale, stage.stageWidth/2 / m_physScale);
wallBody = m_world.CreateBody(wallDef);
wallBody.CreateShape(wallObject);
wallBody.SetMassFromShapes();
//Top
wallDef.position.Set(stage.stageWidth/2 / m_physScale, 10/2 / m_physScale);
wallObject.SetAsBox(stage.stageWidth/2 / m_physScale, 10/2 / m_physScale);
wallBody = m_world.CreateBody(wallDef);
wallBody.CreateShape(wallObject);
wallBody.SetMassFromShapes();
//Bottom
wallDef.position.Set(stage.stageWidth/2 / m_physScale, (stage.stageWidth-10/2) / m_physScale);
wallObject.SetAsBox(stage.stageWidth/2 / m_physScale, 10/2 / m_physScale);
wallBody = m_world.CreateBody(wallDef);
wallBody.CreateShape(wallObject);
wallBody.SetMassFromShapes();
}
private function createObject():void
{
var bd:b2BodyDef;
var box:b2PolygonDef = new b2PolygonDef();
box.SetAsBox(5 / m_physScale, 20 / m_physScale);
box.density = 1.0;
box.friction = 0.4;
box.restitution = 0.3;
bd = new b2BodyDef();
bd.position.Set(250 / m_physScale, 250 / m_physScale);
var boxbd:b2Body = m_world.CreateBody(bd);
boxbd.CreateShape(box);
boxbd.SetMassFromShapes();
createInsect(5)
}
public function createInsect(size:uint = 5):void
{
var circ:b2CircleDef = new b2CircleDef();
var bd:b2BodyDef = new b2BodyDef();
var body:b2Body;
var jd:b2RevoluteJointDef = new b2RevoluteJointDef();
circ.radius = size / m_physScale;
circ.density = 1.0;
circ.friction = 0.8;
circ.restitution = 0.0;
bd = new b2BodyDef();
bd.position.Set(50 / m_physScale, 100 / m_physScale);
var circbd:b2Body = m_world.CreateBody(bd);
circbd.CreateShape(circ);
circbd.SetMassFromShapes();
jd.enableMotor = true;
jd.motorSpeed = 7;
jd.maxMotorTorque = 10000;
for(var i:uint = 0;i<10;i++){
bd.position.Set((100 + i * 50) / m_physScale, 100 / m_physScale);
var circbd2:b2Body = m_world.CreateBody(bd);
circbd2.CreateShape(circ);
circbd2.SetMassFromShapes();
jd.lowerAngle = 90 / (180/Math.PI);
jd.upperAngle = 40 / (180/Math.PI);
jd.Initialize(circbd, circbd2, circbd.GetWorldCenter());
m_world.CreateJoint(jd);
circbd = circbd2;
}
}
private function onMouseDown(event:MouseEvent):void
{
mouseDown = true;
}
private function onMouseUp(event:MouseEvent):void
{
mouseDown = false;
}
private function onMouseMove(event:MouseEvent):void
{
if (mouseDown != event.buttonDown){
mouseDown = event.buttonDown;
}
}
public function onMouseLeave(e:Event):void
{
mouseDown = false;
MouseDrag();
}
//======================
// Update
//======================
private function update(event:Event):void
{
m_world.Step(m_timeStep, m_iterations);
m_sprite.graphics.clear();
UpdateMouseWorld();
MouseDrag();
// joints
for (var jj:b2Joint = m_world.m_jointList; jj; jj = jj.m_next){
DrawJoint(jj);
}
}
//======================
// Update mouseWorld
//======================
public function UpdateMouseWorld():void
{
mouseXWorldPhys = (m_sprite.mouseX)/m_physScale;
mouseYWorldPhys = (m_sprite.mouseY)/m_physScale;
mouseXWorld = (m_sprite.mouseX);
mouseYWorld = (m_sprite.mouseY);
}
//======================
// Mouse Drag
//======================
public function MouseDrag():void
{
// mouse press
if (mouseDown && !m_mouseJoint){
var body:b2Body = GetBodyAtMouse();
if (body)
{
var md:b2MouseJointDef = new b2MouseJointDef();
md.body1 = m_world.GetGroundBody();
md.body2 = body;
md.target.Set(mouseXWorldPhys, mouseYWorldPhys);
md.maxForce = 300.0 * body.GetMass();
md.timeStep = m_timeStep;
m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
body.WakeUp();
}
}
// mouse release
if (!mouseDown){
if (m_mouseJoint)
{
m_world.DestroyJoint(m_mouseJoint);
m_mouseJoint = null;
}
}
// mouse move
if (m_mouseJoint)
{
var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
m_mouseJoint.SetTarget(p2);
}
}
//======================
// GetBodyAtMouse
//======================
public function GetBodyAtMouse(includeStatic:Boolean = false):b2Body
{
// Make a small box.
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
// Query the world for overlapping shapes.
var k_maxCount:int = 10;
var shapes:Array = new Array();
var count:int = m_world.Query(aabb, shapes, k_maxCount);
var body:b2Body = null;
for (var i:int = 0; i < count; ++i)
{
if (shapes[i].GetBody().IsStatic() == false || includeStatic)
{
var tShape:b2Shape = shapes[i] as b2Shape;
var inside:Boolean = tShape.TestPoint(tShape.GetBody().GetXForm(), mousePVec);
if (inside)
{
body = tShape.GetBody();
break;
}
}
}
return body;
}
//======================
// Draw Joint
//======================
public function DrawJoint(joint:b2Joint):void
{
var b1:b2Body = joint.m_body1;
var b2:b2Body = joint.m_body2;
var x1:b2Vec2 = b1.m_linearVelocity;
var x2:b2Vec2 = b2.m_linearVelocity;
var p1:b2Vec2 = joint.GetAnchor1();
var p2:b2Vec2 = joint.GetAnchor2();
m_sprite.graphics.lineStyle(1,0x44aaff,1/1);
switch (joint.m_type)
{
case b2Joint.e_distanceJoint:
case b2Joint.e_mouseJoint:
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
break;
case b2Joint.e_pulleyJoint:
var pulley:b2PulleyJoint = joint as b2PulleyJoint;
var s1:b2Vec2 = pulley.m_groundAnchor1;
var s2:b2Vec2 = pulley.m_groundAnchor2;
m_sprite.graphics.moveTo(s1.x * m_physScale, s1.y * m_physScale);
m_sprite.graphics.lineTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.moveTo(s2.x * m_physScale, s2.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
break;
default:
if (b1 == m_world.m_groundBody){
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x2.x * m_physScale, x2.y * m_physScale);
}
else if (b2 == m_world.m_groundBody){
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x1.x * m_physScale, x1.y * m_physScale);
}
else{
m_sprite.graphics.moveTo(x1.x * m_physScale, x1.y * m_physScale);
m_sprite.graphics.lineTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x2.x * m_physScale, x2.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
}
}
}
}
}