陈童的博客's Archivers

From everyinch on 2011-10-03 17:54:18

box2d滑轮关节(Pulley Joint)

滑轮关节通过地面彼此连接在一起。当一个物体抬起,则另一个物体落下;滑轮绳索的长度为构造函数中配置的常量
length1 + length2 == constant
length1 + ratio * length2 == constant
<img src="http://www.everyinch.net/wp-content/uploads/2011/10/pj1.jpg" alt="" title="pj1" width="237" height="213" class="alignnone size-full wp-image-365" />
基本示例代码如下:
[code lang="as3"]
package{
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Collision.Shapes.b2Shape;
import Box2D.Collision.b2AABB;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.Joints.b2MouseJoint;
import Box2D.Dynamics.Joints.b2MouseJointDef;
import Box2D.Dynamics.Joints.b2PulleyJoint;
import Box2D.Dynamics.Joints.b2PulleyJointDef;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2Fixture;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

[SWF(width="800",height="600",frameRate="32",backgroundColor="0x000000")]
public class PulleyJoint extends Sprite{
private var world:b2World;
private var timeStep:Number;
private var iterations:uint;
private var pixelsPerMeter:Number = 30;
private var mouseJoint:b2MouseJoint;
private var mousePVec:b2Vec2 = new b2Vec2();

public function PulleyJoint(){
createWorld();
makeDebugDraw();
createWall();
createJoint();
addEventListener(Event.ENTER_FRAME,onEnterframe);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}

private function createWorld():void{
var gravity:b2Vec2 = new b2Vec2(0.0,9.8);
var doSleep:Boolean = true;
world = new b2World(gravity,doSleep);
world.SetWarmStarting(true);
timeStep = 1.0/30.0;
iterations = 10;
}

private function makeDebugDraw():void{
var debugDraw:b2DebugDraw = new b2DebugDraw();
var debugSprite:Sprite = new Sprite();
addChild(debugSprite);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(30.0);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
}

private function createWall():void{
var bodyDef:b2BodyDef = new b2BodyDef();
// Body的中心点
bodyDef.position.Set(400/pixelsPerMeter,585/pixelsPerMeter);
var body:b2Body = world.CreateBody(bodyDef);
var shape:b2PolygonShape = new b2PolygonShape();
// 几何体的半宽和半高
shape.SetAsBox(400/pixelsPerMeter,15/pixelsPerMeter);
body.CreateFixture2(shape);
}

private function createJoint():void{
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(420/pixelsPerMeter,300/pixelsPerMeter);
var boxShape1:b2PolygonShape = new b2PolygonShape();
boxShape1.SetAsBox(45/pixelsPerMeter,45/pixelsPerMeter);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = boxShape1;
fixtureDef.density = 5;
var box1:b2Body = world.CreateBody(bodyDef);
box1.CreateFixture(fixtureDef);

bodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(300/pixelsPerMeter,300/pixelsPerMeter);
var boxShape2:b2PolygonShape = new b2PolygonShape();
boxShape2.SetAsBox(45/pixelsPerMeter,45/pixelsPerMeter);
fixtureDef.shape = boxShape2;
var box2:b2Body = world.CreateBody(bodyDef);
box2.CreateFixture(fixtureDef);

var jointDef:b2PulleyJointDef = new b2PulleyJointDef();
var anchor1:b2Vec2 = box1.GetWorldCenter();
var anchor2:b2Vec2 = box2.GetWorldCenter();
var groundAnchor1:b2Vec2 = new b2Vec2(anchor1.x,anchor1.y-200/pixelsPerMeter);
var groundAnchor2:b2Vec2 = new b2Vec2(anchor2.x,anchor2.y-200/pixelsPerMeter);
var ratio:Number = 1.0;
jointDef.Initialize(box1,box2,groundAnchor1,groundAnchor2,anchor1,anchor2,ratio);
jointDef.maxLengthA = 300/pixelsPerMeter;
jointDef.maxLengthB = 300/pixelsPerMeter;
var joint:b2PulleyJoint = world.CreateJoint(jointDef) as b2PulleyJoint;
}

private function onMouseDown(e:MouseEvent):void{
var body:b2Body = getBodyAtMouse();
if(body){
var mouseJointDef:b2MouseJointDef = new b2MouseJointDef();
mouseJointDef.bodyA = world.GetGroundBody();
mouseJointDef.bodyB = body;
mouseJointDef.target.Set(mouseX/pixelsPerMeter,mouseY/pixelsPerMeter);
mouseJointDef.maxForce = 30000;
mouseJoint = world.CreateJoint(mouseJointDef) as b2MouseJoint;
}
}

private function onMouseUp(e:MouseEvent):void{
if(mouseJoint){
world.DestroyJoint(mouseJoint);
mouseJoint = null;
}
}

private function getBodyAtMouse(includeStatic:Boolean = false):b2Body{
var mouseXWorldPhys:Number = mouseX/pixelsPerMeter;
var mouseYWorldPhys:Number = mouseY/pixelsPerMeter;
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);
var body:b2Body = null;
var fixture:b2Fixture;
function GetBodyCallBack(fixture:b2Fixture):Boolean{
var shape:b2Shape = fixture.GetShape();
if(fixture.GetBody().GetType() != b2Body.b2_staticBody || includeStatic){
var inside:Boolean = shape.TestPoint(fixture.GetBody().GetTransform(),mousePVec);
if(inside){
body = fixture.GetBody();
return false;
}
}
return true;
}
world.QueryAABB(GetBodyCallBack,aabb);
return body;
}

private function onEnterframe(e:Event):void{
world.Step(timeStep,iterations,iterations);
world.ClearForces();
world.DrawDebugData();
if(mouseJoint){
var xpos:Number = mouseX/pixelsPerMeter;
var ypos:Number = mouseY/pixelsPerMeter;
var v2:b2Vec2 = new b2Vec2(xpos,ypos);
mouseJoint.SetTarget(v2);
}
}
}
}
[/code]
<img src="http://www.everyinch.net/wp-content/uploads/2011/10/PulleyJoint-300x206.jpg" alt="" title="PulleyJoint" width="300" height="206" class="alignnone size-medium wp-image-354" />

查看完整版本: box2d滑轮关节(Pulley Joint)

From 汤圆 on 2013-05-12 13:09:29

我不太理解那个滑轮的锚点的设置,是根据body1,body2两个物体的锚点然后转换比例设置的吗? PulleyJointDef pjd=new PulleyJiointDef(); Vec2 ga1 =new Vec2(anchor1x/RATE, anchor1y/RATE); Vec2 ga2 =new Vec2(anchor2x/RATE,anchor2y/RATE); pjd.initailize( body1 ,body2 ,ga1, ga2,body1.getWorldCenter(),body2.getWorldCenter(),1f); 滑轮的锚点ga1和ga2不太理解!!!!!!

From 汤圆 on 2013-05-12 23:09:03

@汤圆首先理解错了,后来自己看懂了!!嘻嘻~~~~

Tags: Box2D, Box2DFlash, 滑轮关节


©陈童的博客