使用LIBGDX的子弹弹道

使用LIBGDX的子弹弹道,libgdx,Libgdx,我对子弹的轨迹有问题,当我点击鼠标时,子弹没有准确地射向屏幕上的鼠标光标 这是我的代码: @Override public boolean mouseMoved(int screenX, int screenY) { touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); gamecam.unproject(touchPos); return false;

我对子弹的轨迹有问题,当我点击鼠标时,子弹没有准确地射向屏幕上的鼠标光标

这是我的代码:

 @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);

}

你正在从脉冲向量中移除身体位置,这导致你的身体与目标不对齐

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