新华社 - 获取鼠标采用了2D照相机系统旋转/缩放/平移坐标 [英] XNA - Getting mouse coordinates with a 2d Camera system with rotation/zoom/translation

查看:170
本文介绍了新华社 - 获取鼠标采用了2D照相机系统旋转/缩放/平移坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D相机使用这种转换矩阵:

I have a 2d Camera with this translation matrix:

        Transform = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
                     Matrix.CreateRotationZ(Rotation) *
                     Matrix.CreateScale(new Vector3(Scale, Scale, 0)) *
                     Matrix.CreateTranslation(new Vector3((GraphicsDevice.Viewport.Width * 0.5f), (GraphicsDevice.Viewport.Height * 0.5f), 0));

这对于旋转/缩放,其中的起源是相机的中心工作。

Which works for Rotation/Zoom where the origin is the center of the camera.

现在我试图让世界上的鼠标坐标。

Now I am trying to get the mouse coordinates in the world.

我尝试了使用逆变换,但只是导致NaN的错误。 我猜我需要建立另外一个平移矩阵为鼠标坐标,当前的一个反向的,但我无法弄清楚这是怎么设置

I tried just using an inverse transformation, but that just resulted in NaN errors. I am guessing I need to set up another translation matrix for the mouse coordinates, a reverse of the current one, but I can't figure out how this is set up

我都这样了,

MousePosition =新Vector2((Mouse.GetState()X - DrawTransform.Translation.X)*(1 /游戏code.Camera1.Scale),(Mouse.GetState()Y - DrawTransform.Translation .Y)*(1 /游戏code.Camera1.Scale));

MousePosition = new Vector2((Mouse.GetState().X - DrawTransform.Translation.X) * (1 / Gamecode.Camera1.Scale), (Mouse.GetState().Y - DrawTransform.Translation.Y) * (1 / Gamecode.Camera1.Scale));

但是,这并没有考虑到轮换

But that doesn't take into account rotation

推荐答案

通常更容易和更强大的找到你的矩阵的逆,而不是计算客户端与世界上的地位的手。

Generally it's easier and more robust to find the inverse of your matrix, than to calculate the client-to-world position by hand.

该问题与您的矩阵 - 你不能逆的原因,是你缩放Z轴下降到零

The problem with your matrix - the reason you cannot get the inverse, is that you're scaling the Z axis down to zero.

Matrix.CreateScale(new Vector3(Scale, Scale, 0))

这意味着你已经夷为平地的单一轴的整个空间。所以,当你尝试采取在该空间(客户端空间)单点,并返回到原来的空间(世界空间),你会得到什么实际上是一个线沿Z轴延伸。这就是为什么你的NaN回来。 (或者你可以说 - 你零已经推出了一个部门)

This means that you've flattened your entire space on a single axis. So when you try to take a single point in that space (client space) and return it to the original space (world space) what you get back is actually a line stretching along the Z axis. This is why you get NaNs back. (Or you could just say - you've introduced a division by zero.)

该矩阵求逆函数不知道,你只是用一个平面,二维平面。 XNA的矩阵是3D。

The matrix inverse function doesn't know that you're just using a flat, 2D plane. XNA's matrices are 3D.

解决方法是你的矩阵改成这样:

The fix is to change your matrix to this:

Matrix.CreateScale(Scale)

然后尝试求逆。

Then try finding the inverse.

这篇关于新华社 - 获取鼠标采用了2D照相机系统旋转/缩放/平移坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆