如何检测 3d 立方体的哪一侧被点击?
我正在画布中编写一个小型 3D“引擎”..并不是真正的引擎,而是更多基于 Eric Pascarello 的演示(点击数字“7”几次可以看到它旋转)
一切都非常好,我什至添加了按每个平面(“侧面”)的平均 z 值排序 - 所以我可以使用由多个立方体组成的主体,以便最后绘制接近相机的形状。
现在,我想检测当鼠标单击画布时单击了哪一侧。 类似这个
提示:
- 每条边由 4 个(x,y,z)角定义
- 每个角都使用我认为常规的“透视投影”绘制到画布上
这是我的代码 - 很酷:使用鼠标旋转
//edit: see the code in my answer below
I am writing a small 3d "engine" in canvas.. not really an engine, but more of an application based on code from Eric Pascarello's demo (hit number "7" a few times to see it rotating)
it's all going very nice, I even added sorting by average z-value for each plane ("side") - so that I can use a body made of several cubes so that close to camera shapes are drawn last.
now, I want to detect which side was clicked, when a mouse clickes the canvas.
something like this
hints:
- each side is defined by 4 (x,y,z) corners
- each corner is drawn to canvas using what I believe is regular "perspective projection"
here's my code - it's cool: use the mouse to rotate
//edit: see the code in my answer below
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完美的!我想我需要问这个问题,这样我才能回答自己:-)
该解决方案基于我发现的一些令人惊奇的片段,用于检测多边形中点的存在。这是二维函数,但是嘿......我的画布也是如此 - 愚蠢的我。
这是完整的、跨浏览器的:
哦...我不太介意文档类型,但感谢您的评论。
perfect! guess I needed to ask this question so I could answer myself :-)
the solution is based on some amazing snippet i found for detecting presence of point in polygon. this is 2d function, but hey... so is my canvas - silly me.
here it is, complete and cross-browser:
oh... and I don't mind much for the doctype, but thanks for commenting.