形状光线投射:确定不与某物相交的最后位置的最佳方法?
我正在使用 Box2D (特别是 jBox2D),并且我正在尝试进行类似俄罗斯方块的块碰撞(即在它们彼此重叠之前停止,以便它们无缝互锁)
确定沿“射线”的最后位置的最简单方法是什么' 在形状与另一个形状相交之前? (即确定块在特定方向行进时应处于什么位置,以便与其他块顺利互锁)Box2D 似乎提供通用的光线投射功能,但我不知道如何使用提供的功能轻松做到这一点。
(注意:一般来说,块游戏可以建模为网格,这大大简化了碰撞/目的地检测,因为您不必依赖实际的 2D/3D 属性。但是不可能使用这样的网格在我的情况下)
I'm using Box2D (jBox2D specifically) and I'm trying to do a tetris-like block collision (i.e. stopping just before they overlap each other so they seamlessly interlock)
What is the simplest method to determine the last position along a 'ray' before the shape intersects another? (i.e. determine what position the block should end up in if it travels in a specific direction so that it interlocks smoothly with other blocks) Box2D seems to provide generic raycasting functions but I cannot see how to do this easily with the functions provided.
(Note: In general a block game can be modelled as a grid, and this greatly simplifies collision/destination detection, since you don't have to rely on actual 2D/3D properties. However it is not possible to use a grid like this in my situation)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我有办法解决这个问题,至少对于我的情况来说 - 一旦我像平常一样计算出光线投射的交点,我就炮制了一些可以确定的函数(基于对象的大小和形状 相交 + 相交) 对象应该“到达”以与另一个块很好地啮合的位置。所以我基本上解决了同样的问题,但不是直接使用光线投射本身。有一些极端情况,它的效果不是很好,但对于我的项目来说已经足够了,
我仍然感兴趣,但是,对于原始问题是否有确切的答案:是否有一种方法或变体正常的光线投射可以确定形状相交之前的最后一个“良好”位置?
@iforce2d - 也许我误解了,但 Box2D 给我的信息肯定只会是交叉点所在的位置以及距离起点有多远?两者都不能完全满足我的要求 - 我想要相交之前的最后点,两个形状可以定位在该点上以免相交(即尽可能靠近)可能不相交)
OK, I think I have a way around this problem, at least for my case - once I work out the intersection point from the raycast as normal, I have concocted a few functions which can determine (based on the size and shape of the object intersected + intersecting) the position where the object should 'arrive' to mesh nicely with the other block. So I basically solve the same problem but not directly with the raycast itself. There are a few corner cases where it doesn't work that great, but it's sufficient for my project at the moment
I am still interested, however, whether there is an exact answer to the original question: Is there a method or variant atop of normal raycasting which can determine the last 'good' position of a shape before it intersects?
@iforce2d - Maybe I've misunderstood, but surely the information that Box2D give me will only ever be where the intersection is and how far it is from the start? Neither of which fully do what I want - I want the last point before intersection where the two shapes can be positioned so as not to intersect (i.e. as close together as they can possibly get without intersecting)