简体   繁体   English

二维碰撞分辨率

[英]2D Collision Resolution

I am currently working on a user interface, in which it is possible, to add objects by clicking on the screen. 我目前正在使用一个用户界面,可以在其中通过单击屏幕来添加对象。 I don't want any overlapping objects. 我不要任何重叠的对象。 While it is easy to detect, whether a collision between 2 objects occurred, i am still struggling with resolving these conflicts. 尽管很容易检测到两个对象之间是否发生了冲突,但我仍在努力解决这些冲突。

Currently, i am resolving the conflicts locally by moving the intruding object away from the collision. 目前,我正在通过将入侵对象移离碰撞来局部解决冲突。 This, however, may lead to new collisions, which are resolved in the same manner. 但是,这可能导致新的冲突,这些冲突可以通过相同的方式解决。 Unfortunately, there is no guarantee, that this process will ever stop. 不幸的是,无法保证该过程将永远停止。

Are there any standard problems this relates to or algorithms to use? 是否存在与此相关的标准问题或要使用的算法? Or any efficient solutions which are not prone to endless recursion? 还是任何不易产生无限递归的有效解决方案?

The easiest way I can think of how you can resolve your problems is by implementing a "grid snap" behavior. 我想到如何解决问题的最简单方法是实现“网格捕捉”行为。

Basically, there are only predefined areas in your frame where the user can add UI elements---the grid cells. 基本上,框架中只有预定义的区域,用户可以在其中添加UI元素-网格单元。 When a user drops a UI element on your frame, you should detect where in the grid does it fall mainly (you can choose your own behavior in case it falls equally across two or more grid cells). 当用户将UI元素放在框架上时,您应该检测出它主要落在网格中的哪个位置(您可以选择自己的行为,以防它均匀落在两个或多个网格单元中)。 This way, you need not detect collision between two UI elements at all. 这样,您根本不需要检测两个UI元素之间的冲突。

Edit 编辑

Well, I certainly did not anticipate the cases you outlined in your comment. 好吧,我当然没想到您在评论中概述的情况。 If the sizes of your objects will vary by that much, I admit that "grid snap" may not be applicable to your situation---you might end up with a lot of empty space. 如果对象的大小相差太大,我承认“网格捕捉”可能不适用于您的情况-您可能最终会有很多空白。 I was thinking of something along the lines of Visual Basic when I composed my answer (VB, as far as I remember does implement some sort of grid snap behavior). 我在想沿着Visual Basic中的,当我由我的答案东西线(VB,据我记得确实实现某种栅格捕捉行为)。

A minor point though: while you may have your user's best interests at heart by letting them have an exact control of the positioning of UI elements, think of how your users will interface with your program. 不过,还有一点要注意:虽然可以通过让用户对UI元素的位置进行精确控制来使用户的最大利益放在心上,但请考虑一下用户将如何与程序交互。 Positioning things exactly on the screen using a mouse can be punishment and it may just backfire on you. 使用鼠标在屏幕上精确定位事物可能会很麻烦,并且可能会适得其反。 The same, however, cannot be said if you are guaranteed that your users will always use a touchscreen device. 但是,如果您保证用户将始终使用触摸屏设备,则不能说相同的话。

"grid snap" is avoiding the problem but not a solution to the problem. “网格捕捉”可以避免问题,但不能解决问题。

As Sarah pointed out you can solve this by moving your objects a random distance into one direction and then check again if there is a collision, then move the colliding object. 正如Sarah所指出的,您可以通过以下方法解决此问题:将对象沿一个方向随机移动一个距离,然后再次检查是否有碰撞,然后移动碰撞的对象。 However, this can lead to exponential growth of the problem. 但是,这可能导致问题呈指数增长。

Instead you can try to implement a light physics engine where your objects "bounce" against each other and using friction come to a halt after some time. 相反,您可以尝试实现一个光物理引擎,在该引擎中,对象彼此“反弹”,并在一段时间后使用摩擦停止。 Try to google for continuous collision avoidance. 尝试使用Google进行连续碰撞避免。

Now that I thought of it more I think you just move all the objects that lie in the direction of the move that you are already doing. 现在,我想得更多了,我想您就是将所有对象朝着您已经在执行的方向移动。 Doing it this way you basically solve the problem of possible endless recursion. 这样,您就可以基本解决可能的无限递归问题。

Since you should know the size of your objects to move and the direction of a smart movement this should solve the problem 由于您应该知道要移动的对象的大小以及智能移动的方向,因此应该可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM