XNA:我有一个 1*1*1 盒子,想知道我从哪一侧点击它。 (我的世界克隆)
我目前正在创建一个 Minecraft 克隆(一款游戏,您可以在 3D 空间中创建或删除块来创建结构) 所以我几乎已经完成了我需要的所有工作。 如果我查看一个块,它会按照我想要的方式突出显示,如果我右键单击调用“RemoveBlock()”,则当前查看的块将被删除。 但现在我还想通过单击左键添加块。
所以我有的是: AddBlock(Vector3 LookingAt, Vector3 Direction) 其中“LookingAt”与我存储块数据的 3dArray 正确相关。方向是 Vector3.UnitX/Z/Y 和负数。如果我手动指定 Direction = Vector3.UnitY,则将添加 LookedAt 块上方的块。这就是我想要的。
但我如何知道我是从哪一侧看 Block.one? IE。确定方向: 我尝试的是在 LookedatBlock 周围创建 6 个球体,并从 CameraPossition 向 CameraDirection 绘制一条射线,然后尝试找出它首先与哪个球体碰撞。 6 个球体的中心始终位于立方体每条边的中间,半径为 0.5f。这对我来说很有意义。但这不起作用。我该怎么做才能让它发挥作用。
我需要结果是某种 Vector3.UnitX/Y/Z ,以便我可以将它与其他方法一起使用。
谢谢。
I am currently creating a Minecraft clone (a game where you are in a 3d space and create or delete blocks to create structures)
So I have got nearly everything work that i need.
If i look at a Block it gets highligthed the way i want it to, and if i call "RemoveBlock()" with a rightclick the currently looked at block is being removed.
But now i also want to add blocks by clicking leftclick.
So what i have is: AddBlock(Vector3 LookingAt, Vector3 Direction)
where "LookingAt" is being correctly related to the 3dArray in where i store my Blockdata. and the Direction is either Vector3.UnitX/Z/Y and the negatives. If i manually say that Direction = Vector3.UnitY, then a block above the LookedAt block will be added. which is what i want.
But how do i figure out from which side I am looking at the Block. ie. determin Direction:
what i've tried is to create 6 Spheres around around the LookedatBlock and from CameraPossition i drew a Ray to CameraDirection and then tried to find out which Sphere it collides with first. the 6 speres have their centers always in the middle of each side of the cube, and a radius of 0.5f. This makes sense to me. But it doesn't work. What can I do to get this working.
I need the result being some kind of Vector3.UnitX/Y/Z so that i can use it with my other methods.
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您可以通过检查 X、Y 和 Z 值并从立方体的原点向量中找到它们来确定您单击的是哪一侧。
例如,假设 100 像素立方体的左上角是一个具有 (0,0,0) 的 Vector3(当然,相对于立方体)。好吧,如果您单击立方体的右下角,坐标将是 (100, 100, 100),对吧?
使用它,您可以简单地尝试找到立方体上正方形的边界。例如,六个边将位于以下向量之间:
第 1 边:(0,0,0)、(100,0,0)、(0,0,100) 和 (100,0,100)
第 2 面:(0,100,0)、(100,100,0)、(0,100,100) 和 (100,100,100)
这些分别是立方体的顶边和底边,使用 XYZ 坐标系并选择一个起点。如果我弄乱了其中一个坐标,请原谅我,但它们应该是正确的。
因此,您可以确定您在立方体上单击的位置,精确定位您单击位置的 Vector3,通过简单地查找来找出它在哪一侧(好吧,如果这一侧的值为 (i, 100, j)...)。
这些有道理吗?我假设当您单击立方体时,会返回一些向量。
另外,添加一个调试工具可能会有所帮助,该工具显示鼠标当前指向的矢量等?
I would imagine that you could determine which side you're clicking is on by checking the X, Y, and Z values and finding what they are from the origin Vector of the cube.
For example, say the top-back-left of a 100 pixel cube is a Vector3 with (0,0,0) (relative to the cube, of course). Well, if you click on the bottom-front-right of the cube, the coordinates would be (100, 100, 100), right?
Using that, you could simply try to find the bounds of a square on the cube. For example, the six sides would be between the following vectors:
Side 1: (0,0,0), (100,0,0), (0,0,100), and (100,0,100)
Side 2: (0,100,0), (100,100,0), (0,100,100), and (100,100,100)
Those are the top and bottom sides of the cube, respectively, using an XYZ coordinate system and picking a point to start from. If I messed up on one of the coordinates, forgive me, but they should be about right.
So you could determine where you click on the cube, pinpoint the Vector3 of where you clicked to figure out what side it is on by simply finding (okay, if this side's value is (i, 100, j)...).
Does any of that make sense? I'm assuming that when you click the cube, some Vector is returned.
Also, it might help to add a debugging tool that shows what vector your mouse is currently pointed at, etc?