<script>
tag<script src="jquery-1.8.3.js"></script> <script src="jquery-collision.js"></script>
$(...).collision(...)
will be available in your javascript codevar colliders_selector = ".collider"; var obstacles_selector = ".obstacle"; var hits = $(colliders_selector).collision(obstacles_selector)
var hits = $(colliders_selector).collision(obstacles_selector, { mode: "collision" /*etc*/ } )
<div />
" or any selector to morphThis changes the nature of what is returned. Rather than returning the obstacles that were hit, it returns the overlapping
areas themselves. The "as" refers to the kind of HTML DOM object it creates. If there is one collision, and the overlap
is 50x50 starting at [100,100]
from the edge of the window, and "as" is "<div />
", it essentially returns:
$('<div style="left:100px; top:100px; width:50px; height:50px;"></div>')
If we are returning an "as" object, the default coordinates are relative to "body". If "collider" is given, then the
coordinates are relative to that collision's collider. If "obstacle" is given, then the coordinates are relative to the
obstacle hit. If any other selector is given, then the coordinates are relative to that. This means that you can give
"#gameboard" as a relative setting, get all the collisions, and then append them to "#gameboard", and they will overlap
properly.
If set, all returned "as" objects have a .data("some_string") value available, containing the $(collider) object for that
particular collision.
If set, all returned "as" objects have a .data("some_string") value available, containing the $(obstacle) object for that
particular collision.
If set, all returned "as" objects have a .data("some_string") value available, containing a string describing what part of
the collider was hit. Possibilities include the 8 compass directions in caps, plus "Inside" and "Outside". For example,
if the collision was on the top, left part of the collider, then the value would be "NW". If it occurred on the right side
of the collider, either the whole right edge or an inner portion of it, the value would be "E". If the obstacle is completely
engulfed by the collider, then the collider was hit on the "Inside". If the obstacle completely engulfs the collider, then
the collider was hit on the "Outside".
The association to the collider is reversed for protrusions, see below.
If set to collision or not set at all, all of the above is what happens. If set to "protrusion", the collision detection
goes in reverse. So rather than returning what obstacles the colliders hit, it returns what ones they are escaping from.
So if a small collider is engulfed by a large obstacle (let's call it a restraint instead), then it returns nothing,
because none of the collider is "protruding" from the restraint. If it is peeking out from any edge or completely outside
the restraint, the restraint is returned.
That's if there is no "as" option set. Just like in collisions, protrusions returned "as" a "<div />
" (for example) are
the portion that's affected, not the whole obstacle/restraint. So the "part"(s) of the collider that are protruding from
the retraint are returned. The easiest example is a small collider that is just peeking out of the right side of a
restraint. What is returned is the part that's outside the restraint. And if you asked for directionData, the direction
would be "E" because it's escaping out the "E" edge of the restraint.
What happens if the collider is escaping out the top-right corner of the restraint? Well, we had to pick a good way to
describe this accurately and usefully. What happens is that it will return THREE protrusions. The chunk of the collider
that is directly above the restraint (but not to the right of the corner) is returned with a direction of "N". The chunk
of the collider that is wholly to the right of the restraint (but not above the corner) is returned with a direction of
"E". And the chunk of the collider that is entirely above and to the right of the corner of the restraint is returned
with a direction of "NE". The best way to figure out what this means and how it works is to try the examples.