[英]Hit detection issue
I can't figure out why this hit detection won't work. 我不知道为什么这种命中检测无法正常工作。 I've tried changing the >, and the width/height values, but get no working result. 我试过更改>和width / height值,但是没有任何工作结果。 I've placed a println() function inside the detection to ensure its not the functions inside that aren't working. 我在检测中放置了一个println()函数,以确保其内部的函数不起作用。
Code: 码:
for(var b = 0; b < particles.length; b++) {
if(particles[a] === particles[b]) {
b++;
/*particle[a][3] OR particle[b][3] shows the x point.*/
/*particle[a][4] OR particle[b][4] shows the y point.*/
/*a is defined in a for loop just like the for loop above (for b).*/
} else if(particles[a][3]+10 > particles[b][3] && particles[a][3]-10 < particles[b][3] && particles[a][4]+10 > particles[b][4] && particles[a][4]+10 < particles[b][4]) {
var temp = particles[a][5];
particles[a][5] = particles[b][5];
particles[b][5] = temp;
println("hi");
}
}
Little bit more backstory to the project. 该项目还有更多背景知识。 Basically I have a array with a list of points ("particles") and I want them to have a hit detection system so they bounce off each other and head in opposite directions. 基本上,我有一个包含点(“粒子”)列表的数组,我希望它们具有命中检测系统,以便它们彼此反弹并朝相反的方向前进。
Any help would be appreciated! 任何帮助,将不胜感激!
Final working code: 最终工作代码:
for(var b = 0; b < particles.length; b++) {
if(a !== b && particles[a][3] > particles[b][3]-10 && particles[a][3] < particles[b][3]+10 && particles[a][4] > particles[b][4]-10 && particles[a][4] < particles[b][4]+10) {
var temp = particles[a][5];
particles[a][5] = particles[b][5];
particles[b][5] = temp;
println("hi");
}
}
firstly, particles[a] === particles[b]
means, and can only be true when a === b
首先, particles[a] === particles[b]
表示,并且仅当a === b
时才为真
secondly, when particles[a] === particles[b]
you increment b, then the for loop increments b again ... means you miss a particle!! 其次,当particles[a] === particles[b]
您增加b,然后for循环将再次增加b ... ...表示您错过了一个粒子!
try this: 尝试这个:
for(var b = 0; b < particles.length; b++) {
if(a === b) {
continue;
} else if(particles[a][3]+10 > particles[b][3] && particles[a][3]-10 < particles[b][3] && particles[a][4]+10 > particles[b][4] && particles[a][4]+10 < particles[b][4]) {
var temp = particles[a][5];
particles[a][5] = particles[b][5];
particles[b][5] = temp;
println("hi");
}
}
or even 甚至
for(var b = 0; b < particles.length; b++) {
if (a !== b && particles[a][3]+10 > particles[b][3] && particles[a][3]-10 < particles[b][3] && particles[a][4]+10 > particles[b][4] && particles[a][4]+10 < particles[b][4]) {
var temp = particles[a][5];
particles[a][5] = particles[b][5];
particles[b][5] = temp;
println("hi");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.