画布游戏摄像机 - 应用画布缩放导致位置翻译值混乱

发布于 2024-11-30 23:44:42 字数 2057 浏览 1 评论 0原文

我正在开发一款 Android 游戏,目前正忙于游戏相机类型的组件。 最好的方法似乎是平移画布(可能使用矩阵)以移动整个画布,使其像游戏摄像机一样。 (如果我错了,请纠正我)。

这一切都进展得很好,游戏摄像机正在移动,并且摄像机的 x 和 y 值被用作平移画布位置的指导方针:

    public void onMove(float distanceX, float distanceY){

          Camera.x += (int) distanceX;
          Camera.y += (int) distanceY;
     }

     protected void onDraw(Canvas canvas) {

             canvas.translate(Camera.x, Camera.y);

             level.draw(canvas);

             //the text is a ui component so shouldn't move with the level
             canvas.restore();
             canvas.drawText(fps + " fps", 0, 32, textPaint);
       }

当尝试与世界交互时,我只需将摄像机的坐标添加到触摸的坐标即可获取世界上的正确点:

      public void onSingleTap(MotionEvent event) {

        float mouseX = event.getX() - Camera.x;
        float mouseY = event.getY() - Camera.y;
     }

但是,用户还可以选择缩放屏幕,缩放到世界上的某个点。

case MotionEvent.ACTION_MOVE:

    if (MODE == ZOOM && event.getPointerCount() == 2) {

        float newDist = spacing(event);

        if (newDist > 10f) {

            Camera.zoom = newDist / PrevMultiDist;

            //the midpoint for the zoom
            midPoint(midpoint, event);
            Camera.setPivot(midpoint.x, midpoint.y);

            translateMatrix.postScale(Camera.zoom, Camera.zoom, Camera.pivotX, Camera.pivotY);
        }
    }
 break;

由于视图正在移动以缩放所选位置,因此缩放会弄乱世界触摸平移坐标。我认为如果我从相机位置中减去变焦偏移值,我将再次获得世界上的正确位置。

所以 touchevent 变成这样:

      public void onSingleTap(MotionEvent event) {
      //ideally the zoomoffset might be added in the camera's location already?
        float mouseX = event.getX() - Camera.x - zoomOffsetX;
        float mouseY = event.getY() - Camera.y - zoomOffsetY;
     }

我已经找到了这个问题: Android 在画布上获取位图矩形(左、上、右、下) 但它没有提供我想要的 awsner不知道他的计算是否可以帮助我。

我觉得我错过了一些非常简单的东西。希望有人可以帮忙!

I'm working on an android game and am currently busy with a gamecamera type of component.
The best method seems to be translating the canvas (possibly with a matrix) to move the entire canvas to act like a gamecamera. (please correct me if I'm wrong).

this is all getting along pretty nicely, the gamecamera is moving and the x and y values of the camera are being used as guidelines to translate the canvas location:

    public void onMove(float distanceX, float distanceY){

          Camera.x += (int) distanceX;
          Camera.y += (int) distanceY;
     }

     protected void onDraw(Canvas canvas) {

             canvas.translate(Camera.x, Camera.y);

             level.draw(canvas);

             //the text is a ui component so shouldn't move with the level
             canvas.restore();
             canvas.drawText(fps + " fps", 0, 32, textPaint);
       }

When trying to interact with the world I simply add the camera's coordinates to the touch's coordinates to get the correct point in the world:

      public void onSingleTap(MotionEvent event) {

        float mouseX = event.getX() - Camera.x;
        float mouseY = event.getY() - Camera.y;
     }

However, The user also has the choice to scale the screen, zooming to a point on the world.

case MotionEvent.ACTION_MOVE:

    if (MODE == ZOOM && event.getPointerCount() == 2) {

        float newDist = spacing(event);

        if (newDist > 10f) {

            Camera.zoom = newDist / PrevMultiDist;

            //the midpoint for the zoom
            midPoint(midpoint, event);
            Camera.setPivot(midpoint.x, midpoint.y);

            translateMatrix.postScale(Camera.zoom, Camera.zoom, Camera.pivotX, Camera.pivotY);
        }
    }
 break;

The zoom is messing up the world-touch translation coordinates since the view is being moved to zoom on the chosen spot. I think if I substract the zoom offset value from the camera location that I will get the correct position in the world again.

So the touchevent becomes something like this:

      public void onSingleTap(MotionEvent event) {
      //ideally the zoomoffset might be added in the camera's location already?
        float mouseX = event.getX() - Camera.x - zoomOffsetX;
        float mouseY = event.getY() - Camera.y - zoomOffsetY;
     }

I already found this question: Android get Bitmap Rect (left, top, right, bottom) on a Canvas but it doesn't provide the awsner I want and I can't figure out if his calculations can help me.

I feel like I'm missing something really simple. Hope someone can help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文