从天上掉下来的箭身吊Libgdx Box2d

时间:2019-03-03 04:54:45

标签: libgdx box2d

我正在按照this tutorial在游戏中创建箭头。但是,我不知道为什么我的箭头大多在掉下来后会挂在天空上。

我的身体创建代码如下:

BodyDef def = new BodyDef();
    FixtureDef fixtureDef = new FixtureDef();

    def.type = BodyDef.BodyType.DynamicBody;
    def.position.set( 0 , 5);

    PolygonShape polygonShape = new PolygonShape();
    Vector2 vertices[] = new Vector2[]{
            new Vector2(-this.getWidth()/2 ,0), // create width of half length
            new Vector2(getWidth()/2 - arrowHeadLength ,-getHeight()/5f),
            new Vector2(getWidth()/2 ,0),
            new Vector2(getWidth()/2 - arrowHeadLength ,getHeight()/5f)};

    polygonShape.set(vertices);
    fixtureDef.shape = polygonShape;
    fixtureDef.density = 1f;
    Body box = world.createBody(def);
    box.createFixture(fixtureDef);
    polygonShape.dispose();

    body = box;
    body.setTransform(body.getPosition().x,body.getPosition().y,0 * MathUtils.degreesToRadians);
    body.setAngularDamping(1);
    body.setAwake(false);

更新部件代码:

 pointingDirection = body.getWorldVector(arrowPoitningDirection); //arrowPoitningDirection = new Vector2(1,0);
    flightDirection = body.getLinearVelocity();
    float flightSpeed = flightDirection.nor().len();//normalizes and returns length


    //float dot = Vector2.dot(flightDirection.x,flightDirection.y,pointingDirection.x,pointingDirection.y);
    float dot = flightDirection.dot(pointingDirection);
    float dragForceMagnitude = (1 - Math.abs(dot) * flightSpeed * flightSpeed * dragConstant * body.getMass());

    arrowTailPos = body.getWorldPoint(arrowTailPointingPos); // arrowTailPointingPos = new Vector2(-getWidth()/2 , 0);
    body.applyForce( flightDirection.scl(-dragForceMagnitude), arrowTailPos, false);

我不知道我在哪里错。请帮忙。

0 个答案:

没有答案