在 2D 游戏中使用 RayCasting
在我的一个早期问题中,我询问了有关 onTouchEvents 的问题在OpenGL中。我们的想法是创建一个简单的应用程序,在您按下屏幕的地方绘制一个正方形。一段时间后,我推出了一个应用程序,它可以按照应有的方式完成所有操作,但正方形没有重新绘制,它只是从屏幕上消失了。
有人告诉我,要解决这个问题,我需要从 2D 角度将光线发送到 OpenGL 的 3D 环境中,但我无法理解为什么,因为当我阅读光线投射时,它似乎被使用主要是为了灯光效果。光线投射到底是什么?当我需要改变方块位置的只是 x 和 y 轴时,无论触摸寄存器的深度如何,都可以找到坐标,为什么我还需要 z 轴?
In one of my earlier questions I asked about onTouchEvents in OpenGL. The idea was to create a simple app that drew a square where you pressed the screen. After a while I came out with an app that did everything the way it should have, but the square didn't redraw, it just vanished from the screen.
I have been told that to solve this I need to send a ray into the 3D environment of OpenGL from my 2D perspective, but for the life of me I can't understand why, because when I read up on raycasting it seemed to be used primarily for lighting effects. What exactly is raycasting? Why would I need the z-axis at all when all I need to change in the squares position is the x and y axis from which coodinates can be found no matter what depth the touch registers at?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从视图向屏幕发送一条光线,才能知道在世界的哪个位置绘制正方形。虽然这涉及投射光线,但它并不是将光线投射作为一种渲染技术。除非您正在正交工作,即使在一定程度上,也需要完成从屏幕到世界或从世界到屏幕的转换数学,以便找到基元的正确点。
You need to send a ray from the view to the screen to know where in the world to draw your square. While this involves casting a ray, it's not ray casting as a rendering technique. Unless you are working ortho, and even then to an extent, the math for converting from screen to world or world to screen needs to be done in order to find the correct point for your primitive.