Game Development Reference
In-Depth Information
if (sprite) {
var size = sprite.getSize();
var x = pos.x - size.width / 2;
var y = pos.y - size.height / 2;
entity.setPosition({x: x, y: y});
}
}
};
this.getPosition = function() {
return pos;
};
};
Next, let's look at a very crucial component of any game with moving entities, namely
the physics components whose sole responsibility in life is to tell if two entities col-
lide. This is done in a very simple and efficient fashion. In order for an entity to be
able to use the physics component, it must also have a sprite component since
the physics component needs to know where each entity is located as well as how
tall and wide each entity is. With a sprite component, we're able to extract both
pieces of information about each entity.
The way to check whether two entities collide is very simple. The component itself
stores a reference to an entity so the function that performs the check needs to know
the position and size of the entity we're checking against for collision. Once we have
the location and dimensions of both entities, we simply check if one entity's right side
is to the left of the other's left side, if one's left side is to the right of the other's right
side, or if one's bottom is above the other's top, and if one's top is below the other
entity's bottom. If any of these tests passes (in other words, the conditional check
returns positive) then we know that there is no collision, since it is impossible for two
rectangles to be intersecting each other, and yet any of those four statements are
true about them. Similarly, if all of those tests fail, we know that the entities are inter-
secting each other and a collision has occurred.
var Packt = Packt || {};
Packt.Components = Packt.Components || {};
Search WWH ::




Custom Search