Answers.Body: <p>Yes, <code>checkLayers()</code> is what is performs the collision test, and compares every layer in the Comp against the one running the script.</p>
  
  <p>To check against only one layer, you could re-write that function a little cleaner, but here's a quick solution.</p>
  
  <p>First, remove the lines that start and end the <code>for</code> loop, since you'll only be checking one layer. But don't remove the code inside the <code>for</code> loop, since that code is what does the collision check. </p>
  
  <pre class="lang-js prettyprint-override"><code>for ( idx = 1; idx &lt;= thisComp.numLayers; idx++){  // delete
      ... // keep this stuff
  } // delete
  </code></pre>
  
  <p>You can also delete these two lines, since you don't need to check every layer in the Comp for whether it's the same as the layer running the script, or whether the layer is active or not:</p>
  
  <pre class="lang-js prettyprint-override"><code>if (index == idx) continue; // skips if the comparison layer is the same as the current layer
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>if (! L.active) continue;   // skips if the comparison layer is inactive
  </code></pre>
  
  <p>Last, modify this line:</p>
  
  <pre class="lang-js prettyprint-override"><code>L = thisComp.layer(idx);
  </code></pre>
  
  <p>… to make <code>L</code> the one layer that you want to compare to. You might do that by referencing a layer by index or name, such as:</p>
  
  <pre class="lang-js prettyprint-override"><code>L = thisComp.layer("Name of Single Layer to Compare");
  </code></pre>
  
  <p>Check the Adobe documentation on <a href="https://helpx.adobe.com/after-effects/using/expression-language-reference.html#comp_attributes_and_methods_expression_reference" rel="nofollow">Comp attributes and methods</a> to see other options for referencing a specific layer.</p>
  
  <p>You <em>do</em> need the <code>if (checkLayers()) 1 else 0</code> line at the end, because that's the “switch” that returns the final value of your expression, depending on whether there was a collision (returns <code>1</code>) or not (returns <code>0</code>).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-14T23:09:08.407Z
Answers.LastActivityDate: 2016-07-15T12:22:51.977Z
Answers.LastEditDate: 2016-07-15T12:22:51.977Z
Answers.UserDisplayName: swizzlevixen
Body: <p>I got this code from <a href="http://www.motionscript.com/design-guide/collision.html" rel="nofollow">motionscript</a> and I would like to change it to check a specific layer instead of all the layers for a collision. Maybe within the function checklayers() and then there's the if checklayers() at the last line   ??</p>
  
  <pre><code>function getMin(a, b, c, d){
    return Math.min(Math.min(a,b),Math.min(c,d));
  }
  
  function getMax(a, b, c, d){
    return Math.max(Math.max(a,b),Math.max(c,d));
  }
  
  function getBoundingBox(theLayer){
    bb = [];
    c1 = theLayer.toWorld([0,0]);
    c2 = theLayer.toWorld([theLayer.width,0]);
    c3 = theLayer.toWorld([theLayer.width,theLayer.height]);
    c4 = theLayer.toWorld([0,theLayer.height]);
    bb[0] = getMin(c1[0],c2[0],c3[0],c4[0]);
    bb[1] = getMin(c1[1],c2[1],c3[1],c4[1]);
    bb[2] = getMax(c1[0],c2[0],c3[0],c4[0]);
    bb[3] = getMax(c1[1],c2[1],c3[1],c4[1]);
    return bb;
  }
  
  function checkLayers(){
    cUL = [];
    cLR = [];
    for ( idx = 1; idx &lt;= thisComp.numLayers; idx++){
      if (index == idx) continue;
      L = thisComp.layer(idx);
      if (! L.active) continue;
      BB = getBoundingBox(L);
      UL = [BB[0],BB[1]];
      LR = [BB[2],BB[3]];
  
      if (!(myLR[1] &lt; UL[1] || LR[1] &lt; myUL[1] ||
            myLR[0] &lt; UL[0] || LR[0] &lt; myUL[0])){
  
        if (myUL[1] &lt; UL[1]) cUL[1]=UL[1] else cUL[1]=myUL[1];
        if (myUL[0] &lt; UL[0]) cUL[0]=UL[0] else cUL[0]=myUL[0];
        if (myLR[1] &lt; LR[1]) cLR[1]=myLR[1] else cLR[1]=LR[1];
        if (myLR[0] &lt; LR[0]) cLR[0]=myLR[0] else cLR[0]=LR[0];
  
        for(i = cUL[0]; i &lt;= cLR[0]; i++){
          for (j = cUL[1]; j &lt;= cLR[1]; j++){
            if (sampleImage(fromWorld([i,j]))[3] &gt; 0 &amp;&amp;
                L.sampleImage(L.fromWorld([i,j]))[3] &gt; 0){
  
              return true;
            }
          }
        }
      }
    }
    return false;
  }
  
  myBB = getBoundingBox(thisLayer);
  myUL = [myBB[0],myBB[1]];
  myLR = [myBB[2],myBB[3]];
  
  if (checkLayers()) 1 else 0
  </code></pre>
  
Tags: <after-effects><after-effects-expressions>
Title: changing this code to check for a collision from a specific layer
UserDisplayName: Caitlin
ThreadText: # changing this code to check for a collision from a specific layer
  
  (Asked by: username_0 on 2016-04-28)
  
  I got this code from [motionscript](http://www.motionscript.com/design-guide/collision.html) and I would like to change it to check a specific layer instead of all the layers for a collision. Maybe within the function checklayers() and then there's the if checklayers() at the last line ??
  
  ```
  function getMin(a, b, c, d){
    return Math.min(Math.min(a,b),Math.min(c,d));
  }
  
  function getMax(a, b, c, d){
    return Math.max(Math.max(a,b),Math.max(c,d));
  }
  
  function getBoundingBox(theLayer){
    bb = [];
    c1 = theLayer.toWorld([0,0]);
    c2 = theLayer.toWorld([theLayer.width,0]);
    c3 = theLayer.toWorld([theLayer.width,theLayer.height]);
    c4 = theLayer.toWorld([0,theLayer.height]);
    bb[0] = getMin(c1[0],c2[0],c3[0],c4[0]);
    bb[1] = getMin(c1[1],c2[1],c3[1],c4[1]);
    bb[2] = getMax(c1[0],c2[0],c3[0],c4[0]);
    bb[3] = getMax(c1[1],c2[1],c3[1],c4[1]);
    return bb;
  }
  
  function checkLayers(){
    cUL = [];
    cLR = [];
    for ( idx = 1; idx <= thisComp.numLayers; idx++){
      if (index == idx) continue;
      L = thisComp.layer(idx);
      if (! L.active) continue;
      BB = getBoundingBox(L);
      UL = [BB[0],BB[1]];
      LR = [BB[2],BB[3]];
  
      if (!(myLR[1] < UL[1] || LR[1] < myUL[1] ||
            myLR[0] < UL[0] || LR[0] < myUL[0])){
  
        if (myUL[1] < UL[1]) cUL[1]=UL[1] else cUL[1]=myUL[1];
        if (myUL[0] < UL[0]) cUL[0]=UL[0] else cUL[0]=myUL[0];
        if (myLR[1] < LR[1]) cLR[1]=myLR[1] else cLR[1]=LR[1];
        if (myLR[0] < LR[0]) cLR[0]=myLR[0] else cLR[0]=LR[0];
  
        for(i = cUL[0]; i <= cLR[0]; i++){
          for (j = cUL[1]; j <= cLR[1]; j++){
            if (sampleImage(fromWorld([i,j]))[3] > 0 &&
                L.sampleImage(L.fromWorld([i,j]))[3] > 0){
  
              return true;
            }
          }
        }
      }
    }
    return false;
  }
  
  myBB = getBoundingBox(thisLayer);
  myUL = [myBB[0],myBB[1]];
  myLR = [myBB[2],myBB[3]];
  
  if (checkLayers()) 1 else 0
  
  ```
  
  **Tags:** after-effects, after-effects-expressions
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-07-14. Score: 3 | Accepted answer
  
  Yes, `checkLayers()` is what is performs the collision test, and compares every layer in the Comp against the one running the script.
  
  To check against only one layer, you could re-write that function a little cleaner, but here's a quick solution.
  
  First, remove the lines that start and end the `for` loop, since you'll only be checking one layer. But don't remove the code inside the `for` loop, since that code is what does the collision check.
  
  ```js
  for ( idx = 1; idx <= thisComp.numLayers; idx++){  // delete
      ... // keep this stuff
  } // delete
  
  ```
  
  You can also delete these two lines, since you don't need to check every layer in the Comp for whether it's the same as the layer running the script, or whether the layer is active or not:
  
  ```js
  if (index == idx) continue; // skips if the comparison layer is the same as the current layer
  
  ```
  
  ```js
  if (! L.active) continue;   // skips if the comparison layer is inactive
  
  ```
  
  Last, modify this line:
  
  ```js
  L = thisComp.layer(idx);
  
  ```
  
  … to make `L` the one layer that you want to compare to. You might do that by referencing a layer by index or name, such as:
  
  ```js
  L = thisComp.layer("Name of Single Layer to Compare");
  
  ```
  
  Check the Adobe documentation on [Comp attributes and methods](https://helpx.adobe.com/after-effects/using/expression-language-reference.html#comp_attributes_and_methods_expression_reference) to see other options for referencing a specific layer.
  
  You *do* need the `if (checkLayers()) 1 else 0` line at the end, because that's the “switch” that returns the final value of your expression, depending on whether there was a collision (returns `1`) or not (returns `0`).
  
  
  ---
  
  