Skip to content

Commit

Permalink
optimise coordinates check
Browse files Browse the repository at this point in the history
prevent going through all single polygons, but first checking the
bounding box
  • Loading branch information
obiot committed Jan 26, 2016
1 parent 6333157 commit d5184cb
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions examples/shapes/js/entities/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ game.ShapeObject = me.Entity.extend({
pointerMove: function (event) {
this.hover = false;

// calculate the final coordinates, as the move event is global (viewport);
var parentPos = this.ancestor.getBounds().pos;
var x = event.gameX - this.pos.x - parentPos.x;
var y = event.gameY - this.pos.y - parentPos.y;

// the pointer event system will use the object bounding rect, check then with with all defined shapes
for (var i = this.body.shapes.length, shape; i--, (shape = this.body.shapes[i]);) {
if (shape.containsPoint(x, y)) {
this.hover = true;
break;
// move event is global (relative to the viewport)
if (this.getBounds().containsPoint(event.gameX, event.gameY)) {
// calculate the final coordinates
var parentPos = this.ancestor.getBounds().pos;
var x = event.gameX - this.pos.x - parentPos.x;
var y = event.gameY - this.pos.y - parentPos.y;

// the pointer event system will use the object bounding rect, check then with with all defined shapes
for (var i = this.body.shapes.length, shape; i--, (shape = this.body.shapes[i]);) {
if (shape.containsPoint(x, y)) {
this.hover = true;
break;
}
}
}

Expand Down

0 comments on commit d5184cb

Please sign in to comment.