From a9339b83a863768d28f9ac05efff57231d978c8e Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 1 Jun 2016 11:11:30 +0800 Subject: [PATCH 1/2] fixed collider doc --- cocos2d/core/collider/CCCollisionManager.js | 6 +- cocos2d/core/collider/CCIntersection.js | 102 ++++++++++++-------- 2 files changed, 63 insertions(+), 45 deletions(-) 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..4c17276ac1 100644 --- a/cocos2d/core/collider/CCIntersection.js +++ b/cocos2d/core/collider/CCIntersection.js @@ -7,8 +7,10 @@ var Intersection = {}; /** + * !#en Test line and line + * !#zh 测试线段与线段是否相交 * @method lineLine - * @param {Vec2} a1 + * @param {Vec2} a1 * @param {Vec2} a2 * @param {Vec2} b1 * @param {Vec2} b2 @@ -36,6 +38,8 @@ function lineLine ( a1, a2, b1, b2 ) { Intersection.lineLine = lineLine; /** + * !#en Test line and rect + * !#zh 测试线段与矩形是否相交 * @method lineRect * @param {Vec2} a1 * @param {Vec2} a2 @@ -66,6 +70,8 @@ function lineRect ( a1, a2, b ) { Intersection.lineRect = lineRect; /** + * !#en Test line and polygon + * !#zh 测试线段与多边形是否相交 * @method linePolygon * @param {Vec2} a1 * @param {Vec2} a2 @@ -89,6 +95,8 @@ function linePolygon ( a1, a2, b ) { Intersection.linePolygon = linePolygon; /** + * !#en Test rect and rect + * !#zh 测试矩形与矩形是否相交 * @method rectRect * @param {Rect} a * @param {Rect} b @@ -117,6 +125,8 @@ function rectRect ( a, b ) { Intersection.rectRect = rectRect; /** + * !#en Test rect and polygon + * !#zh 测试矩形与多边形是否相交 * @method rectPolygon * @param {Rect} a * @param {[Vec2]} b @@ -167,6 +177,8 @@ function rectPolygon ( a, b ) { Intersection.rectPolygon = rectPolygon; /** + * !#en Test polygon and polygon + * !#zh 测试多边形与多边形是否相交 * @method polygonPolygon * @param {[Vec2]} a * @param {[Vec2]} b @@ -202,7 +214,54 @@ function polygonPolygon ( a, b ) { Intersection.polygonPolygon = polygonPolygon; + /** + * !#en Test circle and circle + * !#zh 测试圆形与圆形是否相交 + * @method circleCircle + * @param {Object} a - Object contains position and radius + * @param {Object} b - Object contains position and radius + * @return {boolean} + */ +function circleCircle (a, b) { + var distance = a.position.sub(b.position).mag(); + return distance < (a.radius + b.radius); +} + +Intersection.circleCircle = circleCircle; + + +/** + * !#en Test polygon and circle + * !#zh 测试矩形与圆形是否相交 + * @method polygonCircle + * @param {[Vec2]} polygon + * @param {Object} circle - Object contains position and radius + * @return {boolean} + */ +function polygonCircle (polygon, circle) { + var position = circle.position; + if (pointInPolygon(position, polygon)) { + return true; + } + + for (var i = 0, l = polygon.length; i < l; i++) { + var start = i === 0 ? polygon[polygon.length - 1] : polygon[i- 1]; + var end = polygon[i]; + + if (pointLineDistance(position, start, end, true) < circle.radius) { + return true; + } + } + + return false; +} + +Intersection.polygonCircle = polygonCircle; + +/** + * !#en Test whether the point is in the polygon + * !#zh 测试一个点是否在一个多边形中 * @method pointInPolygon * @param {Vec2} point * @param {[Vec2]} polygon @@ -230,47 +289,6 @@ function pointInPolygon (point, polygon) { Intersection.pointInPolygon = pointInPolygon; - -/** - * @method circleCircle - * @param {Object} a - Object contains position and radius - * @param {Object} b - Object contains position and radius - * @return {boolean} - */ -function circleCircle (a, b) { - var distance = a.position.sub(b.position).mag(); - return distance < (a.radius + b.radius); -} - -Intersection.circleCircle = circleCircle; - - -/** - * @method polygonCircle - * @param {[Vec2]} polygon - * @param {Object} circle - Object contains position and radius - * @return {boolean} - */ -function polygonCircle (polygon, circle) { - var position = circle.position; - if (pointInPolygon(position, polygon)) { - return true; - } - - for (var i = 0, l = polygon.length; i < l; i++) { - var start = i === 0 ? polygon[polygon.length - 1] : polygon[i- 1]; - var end = polygon[i]; - - if (pointLineDistance(position, start, end, true) < circle.radius) { - return true; - } - } - - return false; -} - -Intersection.polygonCircle = polygonCircle; - /** * !#en Calculate the distance of point to line. * !#zh 计算点到直线的距离。如果这是一条线段并且垂足不在线段内,则会计算点到线段端点的距离。 From d5bafe9aabc0ea956f68ee91307b73189c5d7d02 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 1 Jun 2016 11:27:01 +0800 Subject: [PATCH 2/2] update CCIntersection doc --- cocos2d/core/collider/CCIntersection.js | 44 ++++++++++++------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/cocos2d/core/collider/CCIntersection.js b/cocos2d/core/collider/CCIntersection.js index 4c17276ac1..ef5fab661b 100644 --- a/cocos2d/core/collider/CCIntersection.js +++ b/cocos2d/core/collider/CCIntersection.js @@ -10,10 +10,10 @@ 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 ) { @@ -41,9 +41,9 @@ 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 ) { @@ -73,9 +73,9 @@ 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 ) { @@ -98,8 +98,8 @@ 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 ) { @@ -128,8 +128,8 @@ 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 ) { @@ -180,8 +180,8 @@ 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 ) { @@ -235,7 +235,7 @@ 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} */ @@ -263,8 +263,8 @@ Intersection.polygonCircle = polygonCircle; * !#en Test whether the point is in the polygon * !#zh 测试一个点是否在一个多边形中 * @method pointInPolygon - * @param {Vec2} point - * @param {[Vec2]} polygon + * @param {Vec2} point - The point + * @param {[Vec2]} polygon - The polygon, a set of points * @return {boolean} */ function pointInPolygon (point, polygon) { @@ -293,9 +293,9 @@ 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} */