子弹轨迹使用LIBGDX

时间:2017-04-14 01:07:48

标签: libgdx

我的子弹轨迹出现问题,当我点击时,子弹在屏幕上的鼠标光标上没有准确触发。

enter image description here

这是我的代码:

 @Override
    public boolean mouseMoved(int screenX, int screenY) {

        touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
        gamecam.unproject(touchPos);

        return false;
    }



     public void shoot() {

     bodyDef = new BodyDef();
     bodyDef.type = BodyDef.BodyType.DynamicBody;
     bodyDef.position.set(x,y);
     body = world.createBody(bodyDef);

     polygonShape = new PolygonShape();
     polygonShape.setAsBox(0.2f / 2.0f, 0.01f /2.0f);

     fixtureDef2 = new FixtureDef();
     fixtureDef2.shape = polygonShape;         

     float calc= (float) (Math.atan2(touchPos.y-(body.getPosition().y),touchPos.x-body.getPosition().x));

     body.setTransform(body.getWorldCenter(),calc);
     body.applyLinearImpulse((touchPos.x-body.getPosition().x)*3, (touchPos.y-body.getPosition().y)*3, 0, 0, true);

}

1 个答案:

答案 0 :(得分:0)

你正在从冲动矢量中移除身体位置,导致你的身体与你的目标不对齐。

body2.applyLinearImpulse(touchPos.x, touchPos.y, 0, 0, true);