Libgdx,box2d:如何制作旋转连接?

时间:2015-08-22 18:32:47

标签: android libgdx box2d

我想要这些身体连接和旋转 (我不是真的需要这个代码,但它仅用于学习目的)

private void ColorRect(float size,float R,float G,float B)
{
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(0,0);
    PolygonShape polygonShape=new PolygonShape();
    polygonShape.setAsBox(size/2,size/2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape=polygonShape;
    fixtureDef.density=1;
    fixtureDef.restitution=0.5f;
    fixtureDef.friction=0.5f;
    Body thebrick=world.createBody(bodyDef);
    thebrick.createFixture(fixtureDef);
    sprite = new Sprite(new Texture("android2.png"));
    sprite.setColor(R,G,B,1);
    sprite.setSize(size,size);
    sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
    thebrick.setUserData(sprite);
}

第二个身体:

private void ColorBall(float radius,float R,float G,float B)
{
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(0,0);
    CircleShape circleShape=new CircleShape();
    circleShape.setRadius(radius/2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape=circleShape;
    fixtureDef.density=1;
    fixtureDef.restitution=0.8f;
    fixtureDef.friction=0.5f;
    Body theBall=world.createBody(bodyDef);
    theBall.createFixture(fixtureDef);
    sprite = new Sprite(new Texture("ball2.png"));
    sprite.setColor(R,G,B,1);
    sprite.setSize(radius,radius);
    sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
    theBall.setUserData(sprite);
    worldBodies = new Array<Body>();
    createMouseJointDefinition(theBall);
}

1 个答案:

答案 0 :(得分:0)

好的,我自己找到了:

private void Joint(Body A,Body B)
{
    RevoluteJointDef jointDef = new RevoluteJointDef();
    jointDef.bodyA = A;
    jointDef.bodyB = B;
    jointDef.type = JointDef.JointType.RevoluteJoint;
    jointDef.collideConnected = true;
    jointDef.localAnchorA.set(5, 0);
    jointDef.localAnchorB.set(5,0);
    jointDef.collideConnected=false;
    jointDef.enableMotor=true;
    jointDef.motorSpeed=1;
    jointDef.maxMotorTorque=50;
    world.createJoint(jointDef);
}

身体:

私人Body Brick()     {

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(position);
    bodyDef.type = BodyType.DynamicBody;
    PolygonShape polygonShape=new PolygonShape();
    polygonShape.setAsBox(width, height);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape=polygonShape;
    fixtureDef.density=1f;
    fixtureDef.restitution=0.5f;
    fixtureDef.friction=0.5f;
    Body thebrick=world.createBody(bodyDef);
thebrick.createFixture(fixtureDef);

返回布里克;     }

private Body Ball()
{
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(position);
    bodyDef.type = BodyType.DynamicBody;
    CircleShape circleShape=new CircleShape();
    circleShape.setRadius(radius);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape=circleShape;
    fixtureDef.density=1;
    fixtureDef.restitution=0.5f;
    fixtureDef.friction=0.5f;
    Body theball=world.createBody(bodyDef);
    theball.createFixture(fixtureDef);
    Return theball;

}

在create()

Joint(Ball(),Brick()):