add collider helpers

This commit is contained in:
2youyouo2
2016-04-27 17:22:35 +08:00
parent 1aa2afc976
commit cfaf68ef69
3 changed files with 481 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Chukong Aipu reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var Collider = cc.Class({
name: 'cc.Collider',
extends: cc.Component,
properties: {
_category: {
default: 1,
type: 'Integer',
range: [0, 65535]
},
_mask: {
default: 65535,
type: 'Integer',
range: [0, 65535]
},
editing: {
default: false,
serializable: false
},
category: {
get: function () {
return this._category;
},
set: function (value) {
if (!CC_EDITOR) {
cc.director.getColliderManager().removeCollider(this);
}
this._category = value;
if (!CC_EDITOR) {
cc.director.getColliderManager().addCollider(this);
}
},
type: 'Integer'
},
mask: {
get: function () {
return this._mask;
},
set: function (value) {
if (!CC_EDITOR) {
cc.director.getColliderManager().removeCollider(this);
}
this._mask = value;
if (!CC_EDITOR) {
cc.director.getColliderManager().addCollider(this);
}
},
type: 'Integer'
}
},
onDisable: function () {
cc.director.getColliderManager().removeCollider(this);
},
onEnable: function () {
cc.director.getColliderManager().addCollider(this);
}
});
cc.Collider = module.exports = Collider;
+74
View File
@@ -0,0 +1,74 @@
var Intersection = require('./CCIntersection');
function Contact (collider1, collider2) {
this.collider1 = collider1;
this.collider2 = collider2;
this.touching = false;
var isCollider1Polygon = (collider1 instanceof cc.BoxCollider) || (collider1 instanceof cc.PolygonCollider);
var isCollider2Polygon = (collider2 instanceof cc.BoxCollider) || (collider2 instanceof cc.PolygonCollider);
var isCollider1Circle = collider1 instanceof cc.CircleCollider;
var isCollider2Circle = collider2 instanceof cc.CircleCollider;
if (isCollider1Polygon && isCollider2Polygon) {
this.testFunc = Intersection.polygonPolygon;
}
else if (isCollider1Circle && isCollider2Circle) {
this.testFunc = Intersection.circleCircle;
}
else if (isCollider1Polygon && isCollider2Circle) {
this.testFunc = Intersection.polygonCircle;
}
else if (isCollider1Circle && isCollider2Polygon) {
this.testFunc = Intersection.polygonCircle;
this.collider1 = collider2;
this.collider2 = collider1;
}
else {
cc.error('Can\'t find testFunc for (%s, $s).', cc.js.getClassByName(collider1), cc.js.getClassByName(collider2));
}
}
Contact.prototype.test = function () {
var world1 = this.collider1.world;
var world2 = this.collider2.world;
if (!world1.aabb.intersects(world2.aabb)) {
return false;
}
if (this.testFunc === Intersection.polygonPolygon) {
return this.testFunc(world1.points, world2.points);
}
else if (this.testFunc === Intersection.circleCircle) {
return this.testFunc(world1, world2);
}
else if (this.testFunc === Intersection.polygonCircle) {
return this.testFunc(world1.points, world2);
}
return false;
};
Contact.prototype.collider = function () {
var result = this.test();
var type = '';
if (result && !this.touching) {
this.touching = true;
type = 'CollisionEnter';
}
else if (result && this.touching) {
type = 'CollisionStay';
}
else if (!result && this.touching) {
this.touching = false;
type = 'CollisionExit';
}
return type;
};
module.exports = Contact;
+313
View File
@@ -0,0 +1,313 @@
/**
* !#en Intersection helper class
* !#zh 辅助类,用于测试形状与形状是否相交
* @class Intersection
* @static
*/
var Intersection = {};
/**
* @method lineLine
* @param {Vec2} a1
* @param {Vec2} a2
* @param {Vec2} b1
* @param {Vec2} b2
* @return {boolean}
*/
function lineLine ( a1, a2, b1, b2 ) {
// jshint camelcase:false
var ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x);
var ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x);
var u_b = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
if ( u_b !== 0 ) {
var ua = ua_t / u_b;
var ub = ub_t / u_b;
if ( 0 <= ua && ua <= 1 && 0 <= ub && ub <= 1 ) {
return true;
}
}
return false;
}
Intersection.lineLine = lineLine;
/**
* @method lineRect
* @param {Vec2} a1
* @param {Vec2} a2
* @param {Vec2} b
* @return {boolean}
*/
function lineRect ( a1, a2, b ) {
var r0 = new cc.Vec2( b.x, b.y );
var r1 = new cc.Vec2( b.x, b.yMax );
var r2 = new cc.Vec2( b.xMax, b.yMax );
var r3 = new cc.Vec2( b.xMax, b.y );
if ( lineLine( a1, a2, r0, r1 ) )
return true;
if ( lineLine( a1, a2, r1, r2 ) )
return true;
if ( lineLine( a1, a2, r2, r3 ) )
return true;
if ( lineLine( a1, a2, r3, r0 ) )
return true;
return false;
}
Intersection.lineRect = lineRect;
/**
* @method linePolygon
* @param {Vec2} a1
* @param {Vec2} a2
* @param {[Vec2]} b
* @return {boolean}
*/
function linePolygon ( a1, a2, b ) {
var length = b.length;
for ( var i = 0; i < length; ++i ) {
var b1 = b[i];
var b2 = b[(i+1)%length];
if ( lineLine( a1, a2, b1, b2 ) )
return true;
}
return false;
}
Intersection.linePolygon = linePolygon;
/**
* @method rectRect
* @param {Rect} a
* @param {Rect} b
* @return {boolean}
*/
function rectRect ( a, b ) {
// jshint camelcase:false
var a_min_x = a.x;
var a_min_y = a.y;
var a_max_x = a.x + a.width;
var a_max_y = a.y + a.height;
var b_min_x = b.x;
var b_min_y = b.y;
var b_max_x = b.x + b.width;
var b_max_y = b.y + b.height;
return a_min_x <= b_max_x &&
a_max_x >= b_min_x &&
a_min_y <= b_max_y &&
a_max_y >= b_min_y
;
}
Intersection.rectRect = rectRect;
/**
* @method rectPolygon
* @param {Rect} a
* @param {[Vec2]} b
* @return {boolean}
*/
function rectPolygon ( a, b ) {
var i, l;
var r0 = new cc.Vec2( a.x, a.y );
var r1 = new cc.Vec2( a.x, a.yMax );
var r2 = new cc.Vec2( a.xMax, a.yMax );
var r3 = new cc.Vec2( a.xMax, a.y );
// intersection check
if ( linePolygon( r0, r1, b ) )
return true;
if ( linePolygon( r1, r2, b ) )
return true;
if ( linePolygon( r2, r3, b ) )
return true;
if ( linePolygon( r3, r0, b ) )
return true;
// check if a contains b
for ( i = 0, l = b.length; i < l; ++i ) {
if ( pointInPolygon(b[i], a) )
return true;
}
// check if b contains a
if ( pointInPolygon(r0, b) )
return true;
if ( pointInPolygon(r1, b) )
return true;
if ( pointInPolygon(r2, b) )
return true;
if ( pointInPolygon(r3, b) )
return true;
return false;
}
Intersection.rectPolygon = rectPolygon;
/**
* @method polygonPolygon
* @param {[Vec2]} a
* @param {[Vec2]} b
* @return {boolean}
*/
function polygonPolygon ( a, b ) {
var i, l;
// check if a intersects b
for ( i = 0, l = a.length; i < l; ++i ) {
var a1 = a[i];
var a2 = a[(i+1)%l];
if ( linePolygon( a1, a2, b ) )
return true;
}
// check if a contains b
for ( i = 0, l = b.length; i < l; ++i ) {
if ( pointInPolygon(b[i], a) )
return true;
}
// check if b contains a
for ( i = 0, l = a.length; i < l; ++i ) {
if ( pointInPolygon( a[i], b ) )
return true;
}
return false;
}
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;
/**
* @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 计算点到直线的距离。如果这是一条线段并且垂足不在线段内,则会计算点到线段端点的距离。
* @method pointLineDistance
* @param {Vec2} point
* @param {Vec2} start - start point of line
* @param {Vec2} end - end point of line
* @param {boolean} isSegment - whether this line is a segment
* @return {boolean}
*/
function pointLineDistance(point, start, end, isSegment) {
var dx = end.x - start.x;
var dy = end.y - start.y;
var d = dx*dx + dy*dy;
var t = ((point.x - start.x) * dx + (point.y - start.y) * dy) / d;
var p;
if (!isSegment) {
p = cc.v2(start.x + t * dx, start.y + t * dy);
}
else {
if (d) {
if (t < 0) p = start;
else if (t > 1) p = end;
else p = cc.v2(start.x + t * dx, start.y + t * dy);
}
else {
p = start;
}
}
dx = point.x - p.x;
dy = point.y - p.y;
return Math.sqrt(dx*dx + dy*dy);
}
Intersection.pointLineDistance = pointLineDistance;
cc.Intersection = module.exports = Intersection;