API Docs for:
Show:

File: physics\Shapes.js


//=====Box shape=====

/**
 This shape represents a 2D box.
 @class Box
 @constructor
 @param width {Number} Width of the box.
 @param height {Number} Height of the box.
 */
function Box(width, height){
    this._width = width || 0;
    this._height = height || 0;
    this._vertices = [];
    
    var halfWidth = this._width / 2,
        halfHeight = this._height / 2;
    
    //Top left corner.
    this._vertices.push(new Vector2D(-halfWidth, -halfHeight));
    //Top right corner.
    this._vertices.push(new Vector2D(halfWidth, -halfHeight));
    //Bottom right corner.
    this._vertices.push(new Vector2D(halfWidth, halfHeight));
    //Bottom left corner.
    this._vertices.push(new Vector2D(-halfWidth, halfHeight));
}

Box.prototype = {
    constructor : Box,
    
    /**
     @method setWidth
     @param width {Number} The new width of the box.
     */
    setWidth : function(width){
        this._width = width;
        
        var halfWidth = this._width / 2;
        
        //Top left corner.
        this._vertices[0].x = -halfWidth;
        //Top right corner.
        this._vertices[1].x = halfWidth;
        //Bottom right corner.
        this._vertices[2].x = halfWidth;
        //Bottom left corner.
        this._vertices[3].x = -halfWidth;
    },
    
    /**
     @method getWidth
     @return {Number} The box's width.
     */
    getWidth : function(){
        return this._width;
    },
    
    /**
     @method setHeight
     @param height {Number} The new height of the box.
     */
    setHeight : function(height){
        this._height = height;
        
        var halfHeight = this._height / 2;
        
        //Top left corner.
        this._vertices[0].y = -halfHeight;
        //Top right corner.
        this._vertices[1].y = -halfHeight;
        //Bottom right corner.
        this._vertices[2].y = halfHeight;
        //Bottom left corner.
        this._vertices[3].y = halfHeight;
    },
    
    /**
     @method getHeight
     @return {Number} The box's height.
     */
    getHeight : function(){
        return this._height;
    },
    
    /**
     Returns the vertices of the box. Doesn't copy. Handle with care.
     @method getVertices
     @return {Array} Four corners of the box.
     */
    getVertices : function(){
        return this._vertices;
    }
};

//=====Circle shape=====

/**
 This shape represents a circle.
 @class Circle
 @constructor
 @param radius {Number} Radius of the circle.
 */
function Circle(radius){
    this._radius = radius || 0;
}

Circle.prototype = {
    construtor : Circle,
    
    /**
     @method setRadius
     @param radius {Number} The circle's new radius.
     */
    setRadius : function(radius){
        this._radius = radius;
    },
    
    /**
     @method getRadius
     @return {Number} The circle's radius.
     */
    getRadius : function(){
        return this._radius;
    }
};

//=====Polygon shape=====

/**
 This shape represents a 2D polygon.
 @class Polygon
 @constructor
 */
function Polygon(){
    this._vertices = [];
}

Polygon.prototype = {
    constructor : Polygon,
    
    /**
     The positions of the vertices should be defined IN RELATION TO THE CENTER OF THE POLYGON. All vertices should be ADDED IN CLOCKWISE ORDER taking into account the coordinate system used by the engine.
     @method addVertex
     @param x {Number} The vertex's x coordinate.
     @param y {Number} The vertex's y coordinate.
     */
    addVertex : function(x, y){
        this._vertices.push( new Vector2D(x, y) );
    },
    
    /**
     Returns the vertices of the polygon. Doesn't copy. Handle with care.
     @method getVertices
     @return {Array} Vertices of the polygon.
     */
    getVertices : function(){
        return this._vertices;
    },
    
    /**
     Clears the vertex array.
     @method clearVertices
     */
    clearVertices : function(){
        this._vertices.length = 0;
    }
};