HTML and CSS Reference
In-Depth Information
Listing 5-16. isPixelContained will determine if a given point resides inside the convex hull for this sprite
function SpriteProto(){
//...
//--------------
this.isPixelContained=function(lclx,lcly)
{
var inPoly = false;
var numPoints = this.hullData.length;
var j = numPoints-2;
var latLng={x:lclx,y:lcly};
//from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
for(var i=0; i < numPoints; i++)
{
var vertex1 = this.hullData[i];
var vertex2 = this.hullData[j];
if ((vertex1.x < latLng.x && vertex2.x >= latLng.x) ||
(vertex2.x < latLng.x && vertex1.x >= latLng.x) )
{
if (vertex1.y + (latLng.x - vertex1.x) / (vertex2.x - vertex1.x) *
(vertex2.y - vertex1.y) < latLng.y)
{
inPoly = !inPoly;
}
}
j=i;
}
return inPoly;
}
} // end of SpriteProto function
Caveats
As mentioned, this method will reduce the overall memory footprint for your data significantly (compared to per-pixel
testing), and on some platforms can have the added benefit of faster execution for a given pick operation.
The real “gotcha” with convex hulls is that it takes a bit of management in order to properly fit into your asset
build pipeline the correct way. This poses a particularly large issue for those games using texture atlases, as you'll
need to calculate the convex hull of each sprite before inserting it into the atlas.
 
Search WWH ::




Custom Search