0

我正在尝试将身体从其初始位置移动到 onTouchEvent x 和 y。问题是身体正在朝着错误的方向移动。有人能告诉我我的错误在哪里吗?

到目前为止,这是我的代码:

public boolean onPlayerTouchEvent(final Scene pScene,
                    final TouchEvent pSceneTouchEvent) {

            final float pValueX = pSceneTouchEvent.getX();
            final float pValueY = pSceneTouchEvent.getY();
            final float rotation = (float) Math.atan2(pValueX, pValueY);
            final float xComp = (float) Math.sin(rotation);
            final float yComp = (float) Math.cos(rotation);

            if (pSceneTouchEvent.isActionDown()) {
                    this.mPlayerBody.setLinearVelocity(new Vector2(xComp, yComp));
            } else if (pSceneTouchEvent.isActionMove()) {

            } else if (pSceneTouchEvent.isActionUp()) {
                    this.mPlayerBody.setLinearVelocity(new Vector2(0, 0));
            }

            return true;
    }
4

1 回答 1

2

你的旋转(角度)方程错误。

试试下面的代码。// touchX 和 touchY 全局变量

public boolean onPlayerTouchEvent(final Scene pScene,
                final TouchEvent pSceneTouchEvent) {

     if (pSceneTouchEvent.isActionDown()) {
        touchX = pSceneTouchEvent.getX();
        touchY = pSceneTouchEvent.getY();

    } else if (pSceneTouchEvent.isActionMove()) {
        final float pValueX = pSceneTouchEvent.getX()-touchX ;
        final float pValueY = pSceneTouchEvent.getY()- touchY;
        final float rotation = (float) Math.atan2(pValueY, pValueX);
        final float xComp = (float) Math.cos(rotation);
        final float yComp = (float) Math.sin(rotation);
        this.myBall.getBallBody().setLinearVelocity(
                new Vector2(xComp, yComp));
    } else if (pSceneTouchEvent.isActionUp()) {
        myBall.getBallBody().setLinearVelocity(new Vector2(0, 0));
    }

        return true;
}
于 2013-03-12T12:39:29.333 回答