diff --git a/cocos2d/core/collider/CCCollisionManager.js b/cocos2d/core/collider/CCCollisionManager.js index 10b5e8580d..71121113b2 100644 --- a/cocos2d/core/collider/CCCollisionManager.js +++ b/cocos2d/core/collider/CCCollisionManager.js @@ -8,10 +8,10 @@ var CollisionType = Contact.CollisionType; * !#zh * 一个简单的碰撞组件管理类,用于处理节点之间的碰撞组件是否产生了碰撞,并调用相应回调函数。 * - * @class ColliderManager + * @class CollisionManager * @example * - * // Get the collider manager. + * // Get the collision manager. * var manager = cc.director.getCollisionManager(); * * // Enabled the colider manager. @@ -24,7 +24,7 @@ var CollisionType = Contact.CollisionType; * manager.enabledDrawBoundingBox = true; * * - * // Collider callback + * // Collision callback * onCollisionEnter: function (other, self) { * this.node.color = cc.Color.RED; * this.touchingNumber ++; diff --git a/cocos2d/core/collider/CCIntersection.js b/cocos2d/core/collider/CCIntersection.js index 59f33f4da6..ef5fab661b 100644 --- a/cocos2d/core/collider/CCIntersection.js +++ b/cocos2d/core/collider/CCIntersection.js @@ -7,11 +7,13 @@ var Intersection = {}; /** + * !#en Test line and line + * !#zh 测试线段与线段是否相交 * @method lineLine - * @param {Vec2} a1 - * @param {Vec2} a2 - * @param {Vec2} b1 - * @param {Vec2} b2 + * @param {Vec2} a1 - The start point of the first line + * @param {Vec2} a2 - The end point of the first line + * @param {Vec2} b1 - The start point of the second line + * @param {Vec2} b2 - The end point of the second line * @return {boolean} */ function lineLine ( a1, a2, b1, b2 ) { @@ -36,10 +38,12 @@ function lineLine ( a1, a2, b1, b2 ) { Intersection.lineLine = lineLine; /** + * !#en Test line and rect + * !#zh 测试线段与矩形是否相交 * @method lineRect - * @param {Vec2} a1 - * @param {Vec2} a2 - * @param {Vec2} b + * @param {Vec2} a1 - The start point of the line + * @param {Vec2} a2 - The end point of the line + * @param {Rect} b - The rect * @return {boolean} */ function lineRect ( a1, a2, b ) { @@ -66,10 +70,12 @@ function lineRect ( a1, a2, b ) { Intersection.lineRect = lineRect; /** + * !#en Test line and polygon + * !#zh 测试线段与多边形是否相交 * @method linePolygon - * @param {Vec2} a1 - * @param {Vec2} a2 - * @param {[Vec2]} b + * @param {Vec2} a1 - The start point of the line + * @param {Vec2} a2 - The end point of the line + * @param {[Vec2]} b - The polygon, a set of points * @return {boolean} */ function linePolygon ( a1, a2, b ) { @@ -89,9 +95,11 @@ function linePolygon ( a1, a2, b ) { Intersection.linePolygon = linePolygon; /** + * !#en Test rect and rect + * !#zh 测试矩形与矩形是否相交 * @method rectRect - * @param {Rect} a - * @param {Rect} b + * @param {Rect} a - The first rect + * @param {Rect} b - The second rect * @return {boolean} */ function rectRect ( a, b ) { @@ -117,9 +125,11 @@ function rectRect ( a, b ) { Intersection.rectRect = rectRect; /** + * !#en Test rect and polygon + * !#zh 测试矩形与多边形是否相交 * @method rectPolygon - * @param {Rect} a - * @param {[Vec2]} b + * @param {Rect} a - The rect + * @param {[Vec2]} b - The polygon, a set of points * @return {boolean} */ function rectPolygon ( a, b ) { @@ -167,9 +177,11 @@ function rectPolygon ( a, b ) { Intersection.rectPolygon = rectPolygon; /** + * !#en Test polygon and polygon + * !#zh 测试多边形与多边形是否相交 * @method polygonPolygon - * @param {[Vec2]} a - * @param {[Vec2]} b + * @param {[Vec2]} a - The first polygon, a set of points + * @param {[Vec2]} b - The second polygon, a set of points * @return {boolean} */ function polygonPolygon ( a, b ) { @@ -202,36 +214,10 @@ function polygonPolygon ( a, b ) { Intersection.polygonPolygon = polygonPolygon; -/** - * @method pointInPolygon - * @param {Vec2} point - * @param {[Vec2]} polygon - * @return {boolean} - */ -function pointInPolygon (point, polygon) { - var inside = false; - var x = point.x; - var y = point.y; - - // use some raycasting to test hits - // https://github.com/substack/point-in-polygon/blob/master/index.js - var length = polygon.length; - - for ( var i = 0, j = length-1; i < length; j = i++ ) { - var xi = polygon[i].x, yi = polygon[i].y, - xj = polygon[j].x, yj = polygon[j].y, - intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); - - if ( intersect ) inside = !inside; - } - - return inside; -} - -Intersection.pointInPolygon = pointInPolygon; - /** + * !#en Test circle and circle + * !#zh 测试圆形与圆形是否相交 * @method circleCircle * @param {Object} a - Object contains position and radius * @param {Object} b - Object contains position and radius @@ -246,8 +232,10 @@ Intersection.circleCircle = circleCircle; /** + * !#en Test polygon and circle + * !#zh 测试矩形与圆形是否相交 * @method polygonCircle - * @param {[Vec2]} polygon + * @param {[Vec2]} polygon - The Polygon, a set of points * @param {Object} circle - Object contains position and radius * @return {boolean} */ @@ -271,13 +259,43 @@ function polygonCircle (polygon, circle) { Intersection.polygonCircle = polygonCircle; +/** + * !#en Test whether the point is in the polygon + * !#zh 测试一个点是否在一个多边形中 + * @method pointInPolygon + * @param {Vec2} point - The point + * @param {[Vec2]} polygon - The polygon, a set of points + * @return {boolean} + */ +function pointInPolygon (point, polygon) { + var inside = false; + var x = point.x; + var y = point.y; + + // use some raycasting to test hits + // https://github.com/substack/point-in-polygon/blob/master/index.js + var length = polygon.length; + + for ( var i = 0, j = length-1; i < length; j = i++ ) { + var xi = polygon[i].x, yi = polygon[i].y, + xj = polygon[j].x, yj = polygon[j].y, + intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); + + if ( intersect ) inside = !inside; + } + + return inside; +} + +Intersection.pointInPolygon = pointInPolygon; + /** * !#en Calculate the distance of point to line. * !#zh 计算点到直线的距离。如果这是一条线段并且垂足不在线段内,则会计算点到线段端点的距离。 * @method pointLineDistance - * @param {Vec2} point - * @param {Vec2} start - start point of line - * @param {Vec2} end - end point of line + * @param {Vec2} point - The point + * @param {Vec2} start - The start point of line + * @param {Vec2} end - The end point of line * @param {boolean} isSegment - whether this line is a segment * @return {boolean} */