Skip to content

Instantly share code, notes, and snippets.

@alonecuzzo
Created March 15, 2013 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alonecuzzo/5172038 to your computer and use it in GitHub Desktop.
Save alonecuzzo/5172038 to your computer and use it in GitHub Desktop.
Here's how they create a hitArea/hitRadius in the /assets/SpaceRock.js file in the examples folder.
p.hitPoint = function (tX, tY) {
return this.hitRadius(tX, tY, 0);
}
p.hitRadius = function (tX, tY, tHit) {
//early returns speed it up
if (tX - tHit > this.x + this.hit) {
return;
}
if (tX + tHit < this.x - this.hit) {
return;
}
if (tY - tHit > this.y + this.hit) {
return;
}
if (tY + tHit < this.y - this.hit) {
return;
}
//now do the circle distance test
return this.hit + tHit > Math.sqrt(Math.pow(Math.abs(this.x - tX), 2) + Math.pow(Math.abs(this.y - tY), 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment