1 /** 2 * The Render Engine 3 * CircleHull 4 * 5 * @fileoverview A collision shape which represents a circular hull. 6 * 7 * @author: Brett Fattori (brettf@renderengine.com) 8 * 9 * @author: $Author: bfattori $ 10 * @version: $Revision: 1573 $ 11 * 12 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 * THE SOFTWARE. 31 * 32 */ 33 34 // The class this file defines and its required classes 35 R.Engine.define({ 36 "class":"R.collision.CircleHull", 37 "requires":[ 38 "R.collision.ConvexHull", 39 "R.math.Rectangle2D", 40 "R.math.Point2D" 41 ] 42 }); 43 44 /** 45 * @class A circular convex hull. 46 * 47 * @param center {Rectangle2D|Point2D|Array} Either the circle's center point, or a rectangle to use 48 * to approximate the bounding circle. 49 * @param radius {Number} The circle's radius if the first argument is a <tt>Point2D</tt>, or a 50 * percentage of the calculated radius if the first argument is an <tt>Array</tt>. 51 * 52 * @extends R.collision.ConvexHull 53 * @constructor 54 * @description Creates a circular hull. 55 */ 56 R.collision.CircleHull = function () { 57 "use strict"; 58 return R.collision.ConvexHull.extend(/** @scope R.collision.CircleHull.prototype */{ 59 60 /** 61 * @private 62 */ 63 constructor:function (center, radius) { 64 if (center && (center.length && center.splice && center.shift)) { 65 // An array of points 66 this.base(center, center.length); 67 if (radius) { 68 this.radius *= radius; 69 } 70 } else { 71 // Approximate with a rectangle 72 var rect; 73 if (center instanceof R.math.Rectangle2D) { 74 rect = center; 75 } else { 76 var p = center; 77 rect = R.math.Rectangle2D.create(R.math.Point2D.create(p.x - radius, p.y - radius), 78 R.math.Point2D.create(p.x + radius, p.y - radius), 79 R.math.Point2D.create(p.x + radius, p.y + radius), 80 R.math.Point2D.create(p.x - radius, p.y + radius)); 81 } 82 this.base([R.math.Point2D.create(0, 0), 83 R.math.Point2D.create(rect.w, 0), 84 R.math.Point2D.create(rect.w, rect.h), 85 R.math.Point2D.create(0, rect.h)]); 86 rect.destroy(); 87 } 88 }, 89 90 /** 91 * Return the type of convex hull this represents. 92 * @return {Number} {@link R.collision.ConvexHull#CONVEX_CIRCLE} 93 */ 94 getType:function () { 95 return R.collision.ConvexHull.CONVEX_CIRCLE; 96 } 97 98 }, /** @scope R.collision.CircleHull.prototype */{ 99 100 /** 101 * Get the class name of this object 102 * @return {String} "R.collision.CircleHull" 103 */ 104 getClassName:function () { 105 return "R.collision.CircleHull"; 106 } 107 }); 108 109 };