File: physics\Components.js
//=====Position component=====
/**
A component class which holds a position of an entity. The position is the center point of the entity.
@class Position
@constructor
@param x {Number} The x component of the position.
@param y {Number} The y component of the position.
*/
function Position(x, y){
this._position = new Vector2D(x, y);
}
Position.prototype = {
constructor : Position,
_componentIdentifier : 0,
/**
@method getPosition
@param vector {Vector2D} Vector to which the position will be copied.
*/
getPosition : function(vector){
vector.x = this._position.x;
vector.y = this._position.y;
},
/**
@method setPosition
@param x {Number} The x component of the new position.
@param y {Number} The y component of the new position.
*/
setPosition : function(x, y){
this._position.x = x;
this._position.y = y;
}
};
//=====Physics body component=====
/**
A component class which holds a physical shape of an entity.
@class Shape
@constructor
@param shape {Box|Circle|Polygon} Object representing the entity's shape.
*/
function Shape(shape){
this._shape = shape || null;
this._AABB = null;
}
Shape.prototype = {
constructor : Shape,
_componentIdentifier : 0,
/**
@method getShape
@return {Box|Circle|Polygon} Object representing the entity's shape.
*/
getShape : function(){
return this._shape;
},
/**
@method setShape
@param shape {Box|Circle|Polygon} Object representing the entity's new shape.
*/
setShape : function(shape){
this._shape = shape;
}
};
//=====Movable body component=====
/**
A component class which holds variables related to the dynamics of an entity.
@class Movable
@constructor
@param mass {Number}
@param dragCoefficient {Number}
@param fastObject {Boolean} Specifies whether the object is fast or not. A fast object gets updated multiple times per frame which should prevent it from passing through other objects.
@param initialVelocityX {Number} The x component of the initial velocity vector.
@param initialVelocityY {Number} The y component of the initial velocity vector.
*/
function Movable(mass, dragCoefficient, fastObject, initialVelocityX, initialVelocityY){
this._mass = mass || 1;
this._inverseMass = 1 / this._mass;
this._dragCoefficient = dragCoefficient || 0;
this._fastObject = fastObject || false;
this._velocity = new Vector2D(initialVelocityX, initialVelocityY);
this._lastFrameAcceleration = new Vector2D();
this._forceAccumulator = new Vector2D();
this._impulseAccumulator = new Vector2D();
}
Movable.prototype = {
constructor : Movable,
_componentIdentifier : 0,
/**
@method getMass
@return {Number}
*/
getMass : function(){
return this._mass;
},
/**
@method setMass
@param mass {Number} Mass needs to be a positive number. For infinite masses use Number.POSITIVE_INFINITY.
*/
setMass : function(mass){
if(mass <= 0){
throw new Error('Mass has to be greater than 0');
}else{
this._mass = mass;
this._inverseMass = 1 / mass;
}
},
/**
@method getDragCoefficient
@return {Number}
*/
getDragCoefficient : function(){
return this._dragCoefficient;
},
/**
@method setDragCoefficient
@param dragCoefficient {Number}
*/
setDragCoefficient : function(dragCoefficient){
this._dragCoefficient = dragCoefficient;
},
/**
@method getFastObject
@return {Boolean}
*/
getFastObject : function(){
return this._fastObject;
},
/**
Sets whether the object is fast or not. A fast object gets updated multiple times per frame which should prevent it from passing through other objects.
@method setFastObject
@param fastObject {Boolean}
*/
setFastObject : function(fastObject){
this._fastObject = fastObject;
},
/**
@method getVelocity
@param vector {Vector2D} Vector to which the velocity will be copied.
*/
getVelocity : function(vector){
vector.x = this._velocity.x;
vector.y = this._velocity.y
},
/**
@method setVelocity
@param x {Number} The x component of the new velocity.
@param y {Number} The y component of the new velocity.
*/
setVelocity : function(x, y){
this._velocity.x = x;
this._velocity.y = y;
},
/**
@method applyForce
@param force {Vector2D} Vector representing the force to be applied.
*/
applyForce : function(force){
this._forceAccumulator.add(force);
},
/**
Clears all forces that were applied to the component between now and the last physics system update.
@method clearForces
*/
clearForces : function(){
this._forceAccumulator.x = 0;
this._forceAccumulator.y = 0;
},
/**
@method applyImpulse
@param impulse {Vector2D} Vector representing the impulse to be applied.
*/
applyImpulse : function(impulse){
this._impulseAccumulator.add(impulse);
},
/**
Clears all impulses that were applied to the component between now and the last physics system update.
@method clearImpulses
*/
clearImpulses : function(){
this._impulseAccumulator.x = 0;
this._impulseAccumulator.y = 0;
}
};
//=====Collidable body component=====
/**
A component class which holds the collision properties of an entity.
@class Collidable
@constructor
@param restitution {Number}
@param friction {Number}
@param category {Number} A set of bit flags that describe the entity's collision category. The number will be converted to a 32bit long integer
and used to check whether the object should collide with other objects.
@param collideWith {Number} A set of bit flags that describe collision categories that this entity will collide with. The number will be converted to a 32bit long integer
and used to check whether the object should collide with other objects.
*/
function Collidable(restitution, friction, category, collideWith){
this._restitution = restitution || 0;
this._friction = friction || 0;
this._category = (category !== undefined) ? category : 1;
this._collideWith = (collideWith !== undefined) ? collideWith : 0xFFFFFFFF;
}
Collidable.prototype = {
constructor : Collidable,
_componentIdentifier : 0,
/**
@method getRestitution
@return {Number}
*/
getRestitution : function(){
return this._restitution;
},
/**
@method setRestitution
@param restitution {Number}
*/
setRestitution : function(restitution){
this._restitution = restitution;
},
/**
@method getFriction
@return {Number}
*/
getFriction : function(){
return this._friction;
},
/**
@method setFriction
@param friction {Number}
*/
setFriction : function(friction){
this._friction = friction;
},
/**
@method getCategory
@return {Number}
*/
getCategory : function(){
return this._category;
},
/**
A set of bit flags that describe the entity's collision category. The number will be converted to a 32bit long integer
and used to check whether the object should collide with other objects.
@method setCategory
@param category {Number}
*/
setCategory : function(category){
this._category = category;
},
/**
@method getCollideWith
@return {Number}
*/
getCollideWith : function(){
return this._collideWith;
},
/**
A set of bit flags that describe collision categories that this entity will collide with. The number will be converted to a 32bit long integer
and used to check whether the object should collide with other objects.
@method setCollideWith
@param collideWith {Number}
*/
setCollideWith : function(collideWith){
this._collideWith = collideWith;
}
};