碰撞检测工作时间减半
我正在尝试检查两个数组之间的碰撞,一个是移动矩形,另一个是固定边界(试图让矩形从墙壁上反弹)。
问题是我编写了一个嵌套的 for 循环,它似乎适用于 4 个边界中的 2 个。我的循环没有达到所有可能的组合吗?
这是我的循环:
for(int n=0;n<_f;n++){
for(int m=0;m<_b;m++){
if(farr[n].inter(barr[m]))
farr[n].setD();
}
}
_f 计算移动矩形(从 0 开始,添加每个矩形后增加),_b 计算边界。 inter() 是我用来检测冲突的一种方法,它在我的程序的所有其他部分中都有效。
任何帮助将不胜感激, 提前谢谢!
public boolean inter(Rect rect){
if(Rect.intersects(rect, rec))
return true;
else
return false;
}
setD() 方法:
public void setD(){
if(_d==0)
_d=2;
if(_d==1)
_d=3;
if(_d==2)
_d=0;
if(_d==3)
_d=1;
}
使用 _d 的 move 方法:
public void moveF(){
if(_d==0){_l+=_s;_r+=_s;}
if(_d==1){_t+=_s;_b+=_s;}
if(_d==2){_l-=_s;_r-=_s;}
if(_d==3){_t-=_s;_b-=_s;}
}
_l 是左侧,_t 是顶部,_r 是右侧,_b 是底部,_s 是每次迭代移动的像素数(在所有情况下都设置为 1)
I am trying to check collisions between two arrays, one of moving rectangles and the other of stationery boundaries (trying to get the rectangles to bounce off the walls).
The problem is that I wrote a nested for loop that seems to work for 2 out of 4 boundaries. Is my loop not reaching all possible combinations?
Here is my loop:
for(int n=0;n<_f;n++){
for(int m=0;m<_b;m++){
if(farr[n].inter(barr[m]))
farr[n].setD();
}
}
_f counts the moving rectangles (starts at 0 and increases after each one is added) and _b counts the boundaries. The inter() is a method I am using to detect collisions and it has worked in all other parts of my program.
Any help would be greatly appreciated,
Thanks in advace!!!
public boolean inter(Rect rect){
if(Rect.intersects(rect, rec))
return true;
else
return false;
}
The setD() method:
public void setD(){
if(_d==0)
_d=2;
if(_d==1)
_d=3;
if(_d==2)
_d=0;
if(_d==3)
_d=1;
}
The move method where _d is used:
public void moveF(){
if(_d==0){_l+=_s;_r+=_s;}
if(_d==1){_t+=_s;_b+=_s;}
if(_d==2){_l-=_s;_r-=_s;}
if(_d==3){_t-=_s;_b-=_s;}
}
_l is left side, _t is top, _r is right, and _b is bottom, and _s is how many pixels it moves per iteration(set to 1 in all cases)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设 _f、_b、farr 和 barr 在循环执行期间不会更改,则循环将检查所有组合一次。那么“两次检查某些碰撞”是如何实现的呢? setD() 做了什么鬼祟的事情吗?你的意思是一旦矩形碰撞就不需要检查更多的边界?如果是这样,可以使用简单的break语句来修复。否则,您的 inter() 方法可能存在问题,与它是否在其他地方工作无关。您可以发布您的内部实现吗?
可能会出现另一个问题,即在离散空间中假设连续属性。正如我令人惊叹的 ascii 艺术(标题:球和墙)技能所展示的那样...
第 1 帧:
o__|_
第 2 帧:
_o_|_
第 3 帧:
__o |_
第 4 帧:
___|o
注意,球穿过了墙壁!在任何一帧中,球都没有与墙壁相交。如果每帧移动的距离大致等于或大于移动对象的特征尺寸,就会发生这种情况。通过简单的交叉检查很难检查这一点。您实际上需要检查球在帧之间占据的路径。
如果您的矩形和障碍物的方向没有旋转,这仍然是一个相当简单的检查。使用两个框架之间的移动矩形的边界矩形并将其与障碍物相交。
其他想法:
您是双重碰撞,两次切换方向。
您的矩形位于两个不同的坐标空间中。
其他一些线程正在与您的直肠拧在一起。
但基本上,你的代码看起来不错。你有多少个矩形?你能让它们有不同的颜色吗?然后,在循环中,当碰撞时,调用 setD 并输出碰撞矩形的颜色及其位置。然后,当您发现问题时,终止代码并查看输出。如果您连续看到两次碰撞(导致矩形两次切换方向),那么您就发现了错误。如果您处于两个不同的坐标空间中,输出坐标也可能会有所帮助。
如果是线程问题,那么是时候温习一下关键部分了。
发现你的错误:
其中每一个都需要是 else if,否则你会在同一次调用中将 0 更新为 2,然后将 2 更新为 0。
Assuming _f, _b, farr, and barr do not change during the execution of the loop, your loop checks all combinations exactly once. So how is it that you "check some collisions twice"? Does setD() do something sneaky? Do you mean that once a rectangle collides there is no need to check more boundaries? If so, that can be fixed with a simple break statement. Otherwise, there likely is a problem with your inter() method, independent as to whether or not it appears to work elsewhere. Can you post your inter implementation?
There is a possibility of another problem, that of assuming continuous properties in a discrete space. As my amazing ascii art (titled: ball and wall) skills demonstrate...
Frame 1:
o__|_
Frame 2:
_o_|_
Frame 3:
__o|_
Frame 4:
___|o
Notice that the ball passed through the wall! In no frame did the ball intersect the wall. This happens if your distance moved per frame can be roughly the same or larger than the characteristic size of your moving object. This is difficult to check for with a simple intersection check. You actually need to check the path that the ball occupied between frames.
If your rectangles and barriers are oriented without rotation, this is still a fairly easy check. Use the bounding rectangle of the moving rectangle between the two frames and intersect that with the barriers.
Other ideas:
You are double colliding, switching the direction twice.
Your rectangles are in two different coordinate spaces.
Some other thread is screwing with your rects.
But basically, your code looks good. How many rectangles do you have? Can you make them distinct colors? Then, in your loop, when you collide, call setD and output the color of the rectangle that collided, and where it was. Then, when you notice a problem, kill the code and look at the output. If you see two collisions in a row (causing the rect to switch directions twice), you'll have caught your bug. Outputting the coordinates might also help, on the off chance that you are in two different coordinate spaces.
If it's a threading issue, then it's time to brush up on critical sections.
Found your mistake:
Each of these needs to be else if, otherwise you update 0 to become 2 and then 2 to become 0 in the same call.