Hello,
The body "m_forearm" in the following code is not being created at the location I specify. Instead of m_forearm hanging below below the m_arm and within m_torso, m_forearm is outside out of m_torso. If I pick up the body and turn it around using a mouse joint, I am able to have m_forearm hang directly below m_arm. From what I can tell, I've set up the coordinates properly. Here is the code that creates the bodies and joints:
and here is the complete code:
Any help would be appreciated.
[edit]
Here's what it looks like:

And here's what I want:

I got that by swiveling the entire body around multiple times.
The body "m_forearm" in the following code is not being created at the location I specify. Instead of m_forearm hanging below below the m_arm and within m_torso, m_forearm is outside out of m_torso. If I pick up the body and turn it around using a mouse joint, I am able to have m_forearm hang directly below m_arm. From what I can tell, I've set up the coordinates properly. Here is the code that creates the bodies and joints:
Code:
public function createBody():void
{
var def:b2RevoluteJointDef = new b2RevoluteJointDef();
m_torso = createRectBody(new Point(5, 10), new Point(2, 5));
m_arm = createRectBody(new Point(6, 10), new Point(1, 2));
m_forearm = createRectBody(new Point(6, 12), new Point(1, 2));
def.Initialize(m_torso, m_arm, new b2Vec2(6.5, 10));
m_shoulderJoint = m_world.CreateJoint(def) as b2RevoluteJoint;
def.Initialize(m_arm, m_forearm, new b2Vec2(6.5, 12));
m_elbowJoint = m_world.CreateJoint(def) as b2RevoluteJoint;
}
// creates and returns body with top left coordinates at @pos
public function createRectBody(pos:Point, dim:Point):b2Body
{
var body:b2Body;
var bodyDef:b2BodyDef = new b2BodyDef();
var shapeDef:b2PolygonDef = new b2PolygonDef();
bodyDef.position.Set(pos.x + dim.x / 2, pos.y + dim.y / 2);
shapeDef.SetAsBox(dim.x / 2, dim.y / 2);
shapeDef.density = 1;
body = m_world.CreateBody(bodyDef);
body.CreateShape(shapeDef);
body.SetMassFromShapes();
return body;
}
{
var def:b2RevoluteJointDef = new b2RevoluteJointDef();
m_torso = createRectBody(new Point(5, 10), new Point(2, 5));
m_arm = createRectBody(new Point(6, 10), new Point(1, 2));
m_forearm = createRectBody(new Point(6, 12), new Point(1, 2));
def.Initialize(m_torso, m_arm, new b2Vec2(6.5, 10));
m_shoulderJoint = m_world.CreateJoint(def) as b2RevoluteJoint;
def.Initialize(m_arm, m_forearm, new b2Vec2(6.5, 12));
m_elbowJoint = m_world.CreateJoint(def) as b2RevoluteJoint;
}
// creates and returns body with top left coordinates at @pos
public function createRectBody(pos:Point, dim:Point):b2Body
{
var body:b2Body;
var bodyDef:b2BodyDef = new b2BodyDef();
var shapeDef:b2PolygonDef = new b2PolygonDef();
bodyDef.position.Set(pos.x + dim.x / 2, pos.y + dim.y / 2);
shapeDef.SetAsBox(dim.x / 2, dim.y / 2);
shapeDef.density = 1;
body = m_world.CreateBody(bodyDef);
body.CreateShape(shapeDef);
body.SetMassFromShapes();
return body;
}
and here is the complete code:
Code:
package
{
import Box2D.Collision.*;
import Box2D.Dynamics.*;
import Box2D.Common.Math.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.events.MouseEvent;
/**
* ...
* @author
*/
public class Main extends Sprite
{
public var m_aabb:b2AABB;
public var m_world:b2World;
public var m_dbgSprite:Sprite;
public var m_ground:b2Body;
public var m_torso:b2Body;
public var m_arm:b2Body;
public var m_forearm:b2Body;
public var m_shoulderJoint:b2RevoluteJoint;
public var m_elbowJoint:b2RevoluteJoint;
public var m_mouseJoint:b2MouseJoint;
public var m_mouseXWorldPhys:Number;
public var m_mouseYWorldPhys:Number;
public var m_mouseDown:Boolean;
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);
// entry point
m_mouseJoint = null;
m_mouseDown = false;
createWorld();
createGround();
createBody();
setUpEvents();
}
public function createWorld():void
{
var gravity:b2Vec2 = new b2Vec2(0, 10);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_dbgSprite = new Sprite();
m_aabb = new b2AABB();
m_aabb.lowerBound.Set();
m_aabb.upperBound.Set(100, 100);
m_world = new b2World(m_aabb, gravity, true);
addChild(m_dbgSprite);
m_dbgSprite.addChild(dbgSprite);
dbgDraw.m_sprite = m_dbgSprite;
dbgDraw.m_drawScale = 30;
dbgDraw.m_alpha = 1;
dbgDraw.m_fillAlpha = 0.5;
dbgDraw.m_lineThickness = 1;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
m_world.SetDebugDraw(dbgDraw);
}
public function createGround():void
{
var bodyDef:b2BodyDef = new b2BodyDef();
var shapeDef:b2PolygonDef = new b2PolygonDef();
bodyDef.position.Set(0, 15);
m_ground = m_world.CreateBody(bodyDef);
shapeDef.SetAsBox(100, .1);
shapeDef.friction = 1;
shapeDef.restitution = 0;
m_ground.CreateShape(shapeDef);
m_ground.SetMassFromShapes();
}
public function enterFrame(event:Event):void
{
updateMouseWorld();
dragBody();
m_world.Step(1/30, 10);
}
public function createBody():void
{
var def:b2RevoluteJointDef = new b2RevoluteJointDef();
m_torso = createRectBody(new Point(5, 10), new Point(2, 5));
m_arm = createRectBody(new Point(6, 10), new Point(1, 2));
m_forearm = createRectBody(new Point(6, 12), new Point(1, 2));
def.Initialize(m_torso, m_arm, new b2Vec2(6.5, 10));
m_shoulderJoint = m_world.CreateJoint(def) as b2RevoluteJoint;
def.Initialize(m_arm, m_forearm, new b2Vec2(6.5, 12));
m_elbowJoint = m_world.CreateJoint(def) as b2RevoluteJoint;
}
// creates and returns body with top left coordinates at @pos
public function createRectBody(pos:Point, dim:Point):b2Body
{
var body:b2Body;
var bodyDef:b2BodyDef = new b2BodyDef();
var shapeDef:b2PolygonDef = new b2PolygonDef();
bodyDef.position.Set(pos.x + dim.x / 2, pos.y + dim.y / 2);
shapeDef.SetAsBox(dim.x / 2, dim.y / 2);
shapeDef.density = 1;
body = m_world.CreateBody(bodyDef);
body.CreateShape(shapeDef);
body.SetMassFromShapes();
return body;
}
public function dragBody():void
{
var body:b2Body = getBodyAtMouse();
var mouseJointDef:b2MouseJointDef;
if(m_mouseDown && !m_mouseJoint)
if (body)
{
mouseJointDef = new b2MouseJointDef();
mouseJointDef.body1 = m_world.GetGroundBody();
mouseJointDef.body2 = body;
mouseJointDef.frequencyHz = 200;
mouseJointDef.dampingRatio = 0;
mouseJointDef.target.Set(m_mouseXWorldPhys, m_mouseYWorldPhys);
mouseJointDef.maxForce = 300.0 * body.GetMass();
mouseJointDef.timeStep = 1 / 30.0;
mouseJointDef.dampingRatio = 1;
m_mouseJoint = m_world.CreateJoint(mouseJointDef) as b2MouseJoint;
body.WakeUp();
}
if (!m_mouseDown)
if (m_mouseJoint)
{
m_world.DestroyJoint(m_mouseJoint);
m_mouseJoint = null;
}
if (m_mouseJoint)
{
m_mouseJoint.SetTarget(new b2Vec2(m_mouseXWorldPhys, m_mouseYWorldPhys));
}
}
public function getBodyAtMouse(includeStatic:Boolean = false):b2Body
{
var mousePVec:b2Vec2 = new b2Vec2();
// Make a small box.
mousePVec.Set(m_mouseXWorldPhys, m_mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(m_mouseXWorldPhys - 0.001, m_mouseYWorldPhys - 0.001);
aabb.upperBound.Set(m_mouseXWorldPhys + 0.001, m_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;
}
public function updateMouseWorld():void
{
m_mouseXWorldPhys = (mouseX)/30;
m_mouseYWorldPhys = (mouseY)/30;
}
public function mouseUp(event:MouseEvent):void
{
m_mouseDown = false;
}
public function mouseDown(event:MouseEvent):void
{
m_mouseDown = true;
}
public function setUpEvents():void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
addEventListener(Event.ENTER_FRAME, enterFrame);
}
}
}
{
import Box2D.Collision.*;
import Box2D.Dynamics.*;
import Box2D.Common.Math.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.events.MouseEvent;
/**
* ...
* @author
*/
public class Main extends Sprite
{
public var m_aabb:b2AABB;
public var m_world:b2World;
public var m_dbgSprite:Sprite;
public var m_ground:b2Body;
public var m_torso:b2Body;
public var m_arm:b2Body;
public var m_forearm:b2Body;
public var m_shoulderJoint:b2RevoluteJoint;
public var m_elbowJoint:b2RevoluteJoint;
public var m_mouseJoint:b2MouseJoint;
public var m_mouseXWorldPhys:Number;
public var m_mouseYWorldPhys:Number;
public var m_mouseDown:Boolean;
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);
// entry point
m_mouseJoint = null;
m_mouseDown = false;
createWorld();
createGround();
createBody();
setUpEvents();
}
public function createWorld():void
{
var gravity:b2Vec2 = new b2Vec2(0, 10);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
m_dbgSprite = new Sprite();
m_aabb = new b2AABB();
m_aabb.lowerBound.Set();
m_aabb.upperBound.Set(100, 100);
m_world = new b2World(m_aabb, gravity, true);
addChild(m_dbgSprite);
m_dbgSprite.addChild(dbgSprite);
dbgDraw.m_sprite = m_dbgSprite;
dbgDraw.m_drawScale = 30;
dbgDraw.m_alpha = 1;
dbgDraw.m_fillAlpha = 0.5;
dbgDraw.m_lineThickness = 1;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
m_world.SetDebugDraw(dbgDraw);
}
public function createGround():void
{
var bodyDef:b2BodyDef = new b2BodyDef();
var shapeDef:b2PolygonDef = new b2PolygonDef();
bodyDef.position.Set(0, 15);
m_ground = m_world.CreateBody(bodyDef);
shapeDef.SetAsBox(100, .1);
shapeDef.friction = 1;
shapeDef.restitution = 0;
m_ground.CreateShape(shapeDef);
m_ground.SetMassFromShapes();
}
public function enterFrame(event:Event):void
{
updateMouseWorld();
dragBody();
m_world.Step(1/30, 10);
}
public function createBody():void
{
var def:b2RevoluteJointDef = new b2RevoluteJointDef();
m_torso = createRectBody(new Point(5, 10), new Point(2, 5));
m_arm = createRectBody(new Point(6, 10), new Point(1, 2));
m_forearm = createRectBody(new Point(6, 12), new Point(1, 2));
def.Initialize(m_torso, m_arm, new b2Vec2(6.5, 10));
m_shoulderJoint = m_world.CreateJoint(def) as b2RevoluteJoint;
def.Initialize(m_arm, m_forearm, new b2Vec2(6.5, 12));
m_elbowJoint = m_world.CreateJoint(def) as b2RevoluteJoint;
}
// creates and returns body with top left coordinates at @pos
public function createRectBody(pos:Point, dim:Point):b2Body
{
var body:b2Body;
var bodyDef:b2BodyDef = new b2BodyDef();
var shapeDef:b2PolygonDef = new b2PolygonDef();
bodyDef.position.Set(pos.x + dim.x / 2, pos.y + dim.y / 2);
shapeDef.SetAsBox(dim.x / 2, dim.y / 2);
shapeDef.density = 1;
body = m_world.CreateBody(bodyDef);
body.CreateShape(shapeDef);
body.SetMassFromShapes();
return body;
}
public function dragBody():void
{
var body:b2Body = getBodyAtMouse();
var mouseJointDef:b2MouseJointDef;
if(m_mouseDown && !m_mouseJoint)
if (body)
{
mouseJointDef = new b2MouseJointDef();
mouseJointDef.body1 = m_world.GetGroundBody();
mouseJointDef.body2 = body;
mouseJointDef.frequencyHz = 200;
mouseJointDef.dampingRatio = 0;
mouseJointDef.target.Set(m_mouseXWorldPhys, m_mouseYWorldPhys);
mouseJointDef.maxForce = 300.0 * body.GetMass();
mouseJointDef.timeStep = 1 / 30.0;
mouseJointDef.dampingRatio = 1;
m_mouseJoint = m_world.CreateJoint(mouseJointDef) as b2MouseJoint;
body.WakeUp();
}
if (!m_mouseDown)
if (m_mouseJoint)
{
m_world.DestroyJoint(m_mouseJoint);
m_mouseJoint = null;
}
if (m_mouseJoint)
{
m_mouseJoint.SetTarget(new b2Vec2(m_mouseXWorldPhys, m_mouseYWorldPhys));
}
}
public function getBodyAtMouse(includeStatic:Boolean = false):b2Body
{
var mousePVec:b2Vec2 = new b2Vec2();
// Make a small box.
mousePVec.Set(m_mouseXWorldPhys, m_mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(m_mouseXWorldPhys - 0.001, m_mouseYWorldPhys - 0.001);
aabb.upperBound.Set(m_mouseXWorldPhys + 0.001, m_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;
}
public function updateMouseWorld():void
{
m_mouseXWorldPhys = (mouseX)/30;
m_mouseYWorldPhys = (mouseY)/30;
}
public function mouseUp(event:MouseEvent):void
{
m_mouseDown = false;
}
public function mouseDown(event:MouseEvent):void
{
m_mouseDown = true;
}
public function setUpEvents():void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
addEventListener(Event.ENTER_FRAME, enterFrame);
}
}
}
Any help would be appreciated.
[edit]
Here's what it looks like:

And here's what I want:

I got that by swiveling the entire body around multiple times.