1

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. I've placed a println() function inside the detection to ensure its not the functions inside that aren't working.

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");
    }
 }
bdkopen
  • 494
  • 1
  • 6
  • 16
  • 1
    Possible duplicate of [Javascript: Collision detection](http://stackoverflow.com/questions/2440377/javascript-collision-detection) – Scott Marcus Dec 23 '16 at 16:55
  • 1
    @ScottMarcus that post doesn't help. I understand how collisions work, but for some reason this one in particular doesn't. Also that post includes a lot of jquery answers (which I'm not using). – bdkopen Dec 23 '16 at 16:58
  • 1
    Is `println` defined somewhere in your code? If not, this will throw an error. – Dom Dec 23 '16 at 17:00
  • 1
    Thee are many links on that page that point to resources that show a variety of techniques that are done with pure JS. – Scott Marcus Dec 23 '16 at 17:00
  • 1
    @Dom println() is a processing.js library function, similar to console.log() or alert() debugging. – bdkopen Dec 23 '16 at 17:01

1 Answers1

2

firstly, particles[a] === particles[b] means, and can only be true when a === b

secondly, when particles[a] === particles[b] you increment b, then the for loop increments b again ... means you miss a particle!!

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");
     }
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • 1
    Still not getting a result. I tried bumping the width/height up to 100 but still don't get anything. Just rechecked I was calling the right array too... – bdkopen Dec 23 '16 at 17:12