从box2d中的旋转关节射出身体后如何使身体具有相同的位置和旋转?

时间:2014-02-25 22:23:46

标签: libgdx box2d

我有一个旋转关节,它连接一个旋转器身体和一个箭头体,但我坚持如何使箭头每次都有相同的位置。需要将箭头体设置为与之前触发的角度相同的角度。

这是我的代码:

我用一个按钮来点燃我的箭头

FIREBUTTON.addListener(new InputListener()
    {
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
        {
            world.destroyBody(rotatorBody);
            arrowBody.applyLinearImpulse(300 * MathUtils.cos(joint.getJointAngle()), 300 * MathUtils.sin(joint.getJointAngle()), arrowBody.getWorldCenter().x, arrowBody.getWorldCenter().y, true);
                return true;
        }

        public void touchUp(InputEvent event, float x, float y, int pointer, int button)
        {
            reload();
        }
    });

这是我的重装()

public void reload()
{
    arrowSprite = new Sprite(new Texture(Gdx.files.internal("sprites/arrow3.png")));
    arrowSprite.setSize(4f, 2f);                                //setting the size of the sprite
    arrowSprite.setOrigin(arrowSprite.getWidth()/2,arrowSprite.getHeight()/2); //setting the centre of the sprite to the centre of the body

    /***ARROW BODY***/

    //DEFINE the definition of the arrow body
    arrowBodyDef = new BodyDef();
    arrowBodyDef.type = BodyType.DynamicBody;


    arrowBodyShape = new PolygonShape();
    arrowbodyPolygon = new Polygon(new float[]
                                    {
                                        -0.125f*3, -0.25f*3,
                                        0.125f*3, -0.25f*3,
                                        0.25f*3, 0.25f*3,
                                        0*3, 0.5f*3,
                                        -0.25f*3, 0.25f*3
                                    });     
    //arrowbodyPolygon.setOrigin(0*3, 0.5f*3);
    arrowbodyPolygon.rotate(-90);
    arrowBodyShape.set(arrowbodyPolygon.getTransformedVertices());
    arrowBody = world.createBody(arrowBodyDef);
    arrowBody.createFixture(arrowBodyShape, 2.0f);
    arrowBody.setUserData(arrowSprite);

    /***ROTATOR CIRCLE BODY***/

    rotatorBodyDef = new BodyDef();
    rotatorBodyDef.type = BodyType.KinematicBody;
    rotatorBodyDef.position.set(-90,-15);

    rotatorCircleShape = new CircleShape();
    rotatorCircleShape.setRadius(0.5f);
    rotatorBody = world.createBody(rotatorBodyDef);
    rotatorBody.createFixture(rotatorCircleShape, 1.0f);


    /*** REVOLUTE JOINT THAT JOINS THE ROTATOR BODY TO THE ARROWBODY ***/
    revoluteJointDef = new RevoluteJointDef();
    revoluteJointDef.bodyA = rotatorBody;
    revoluteJointDef.bodyB = arrowBody;
    revoluteJointDef.localAnchorA.set(Vector2.Zero);
    revoluteJointDef.localAnchorB.set(Vector2.Zero);
    revoluteJointDef.maxMotorTorque = 250;
    revoluteJointDef.enableMotor = true;

    joint = (RevoluteJoint) world.createJoint(revoluteJointDef);
}

我真的被困在如何让箭头重新加载到与以前相同的位置,任何想法?

我添加了一个图像,所以基本上第一张图片显示了创建关节和物体时会发生什么,它开始时箭头指向右边,如果我将箭头向左旋转45度,如图2所示,那么我点燃箭头重新加载箭头,但这次箭头指向上方。我希望创建箭头并指向上次离开的相同角度。现在更有意义吗? enter image description here

1 个答案:

答案 0 :(得分:0)

您需要在触发箭头后存储RevoluteJoint的角度。问题是setJoingAngle()没有RevoluteJoint,但你可以设置它来创建身体,这样身体本身就会自动旋转而不是关节。

private float arrowAngle;

// in your listener
arrowAngle = joint.getJoingAngle(); // store it here
world.destroyBody(rotatorBody);
arrowBody.applyLinearImpulse(300 * MathUtils.cos(joint.getJointAngle()), 300 * MathUtils.sin(joint.getJointAngle()), arrowBody.getWorldCenter().x, arrowBody.getWorldCenter().y, true);
            return true;

// in your reload() method
arrowBodyDef = new BodyDef();
arrowBodyDef.type = BodyType.DynamicBody;
arrowBodyDef.angle = arrowAngle; // set it on your body