reconstruction collider

This commit is contained in:
2youyouo2
2017-06-06 11:39:15 +08:00
parent 2e1889812a
commit df4fffcd5d
8 changed files with 310 additions and 306 deletions
+17 -14
View File
@@ -23,20 +23,7 @@
THE SOFTWARE.
****************************************************************************/
/**
* !#en Box Collider.
* !#zh 包围盒碰撞组件
* @class BoxCollider
* @extends Component
*/
var BoxCollider = cc.Class({
name: 'cc.BoxCollider',
extends: cc.Collider,
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.collider/Box Collider',
},
cc.Collider.Box = cc.Class({
properties: {
_offset: cc.v2(0, 0),
_size: cc.size(100, 100),
@@ -87,4 +74,20 @@ var BoxCollider = cc.Class({
}
});
/**
* !#en Box Collider.
* !#zh 包围盒碰撞组件
* @class BoxCollider
* @extends Component
*/
var BoxCollider = cc.Class({
name: 'cc.BoxCollider',
extends: cc.Collider,
mixins: [cc.Collider.Box],
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.collider/Box Collider',
}
});
cc.BoxCollider = module.exports = BoxCollider;
+17 -14
View File
@@ -23,20 +23,7 @@
THE SOFTWARE.
****************************************************************************/
/**
* !#en Circle Collider.
* !#zh 圆形碰撞组件
* @class CircleCollider
* @extends Component
*/
var CircleCollider = cc.Class({
name: 'cc.CircleCollider',
extends: require('./CCCollider'),
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.collider/Circle Collider'
},
cc.Collider.Circle = cc.Class({
properties: {
_offset: cc.v2(0, 0),
_radius: 50,
@@ -83,4 +70,20 @@ var CircleCollider = cc.Class({
}
});
/**
* !#en Circle Collider.
* !#zh 圆形碰撞组件
* @class CircleCollider
* @extends Component
*/
var CircleCollider = cc.Class({
name: 'cc.CircleCollider',
extends: cc.Collider,
mixins: [cc.Collider.Circle],
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.collider/Circle Collider'
},
});
cc.CircleCollider = module.exports = CircleCollider;
+18 -15
View File
@@ -23,21 +23,7 @@
THE SOFTWARE.
****************************************************************************/
/**
* !#en Polygon Collider.
* !#zh 多边形碰撞组件
* @class PolygonCollider
* @extends Component
*/
var PolygonCollider = cc.Class({
name: 'cc.PolygonCollider',
extends: require('./CCCollider'),
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.collider/Polygon Collider',
inspector: 'packages://inspector/inspectors/comps/physics/points-base-collider.js',
},
cc.Collider.Polygon = cc.Class({
properties: {
threshold: {
default: 1,
@@ -87,4 +73,21 @@ var PolygonCollider = cc.Class({
}
});
/**
* !#en Polygon Collider.
* !#zh 多边形碰撞组件
* @class PolygonCollider
* @extends Component
*/
var PolygonCollider = cc.Class({
name: 'cc.PolygonCollider',
extends: cc.Collider,
mixins: [cc.Collider.Polygon],
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.collider/Polygon Collider',
inspector: 'packages://inspector/inspectors/comps/physics/points-base-collider.js',
},
});
cc.PolygonCollider = module.exports = PolygonCollider;
@@ -36,16 +36,14 @@ var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
*/
var PhysicsBoxCollider = cc.Class({
name: 'cc.PhysicsBoxCollider',
extends: cc.BoxCollider,
mixins: [cc.PhysicsCollider],
extends: cc.PhysicsCollider,
mixins: [cc.Collider.Box],
editor: {
menu: CC_EDITOR && 'i18n:MAIN_MENU.component.physics/Collider/Box',
requireComponent: cc.RigidBody
},
properties: cc.PhysicsCollider.properties,
_createShape: function (scale) {
var scaleX = Math.abs(scale.x);
var scaleY = Math.abs(scale.y);
@@ -36,8 +36,7 @@ var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
*/
var PhysicsChainCollider = cc.Class({
name: 'cc.PhysicsChainCollider',
extends: cc.Collider,
mixins: [cc.PhysicsCollider],
extends: cc.PhysicsCollider,
editor: {
menu: CC_EDITOR && 'i18n:MAIN_MENU.component.physics/Collider/Chain',
@@ -45,7 +44,7 @@ var PhysicsChainCollider = cc.Class({
requireComponent: cc.RigidBody
},
properties: cc.js.mixin({
properties: {
loop: false,
points: {
default: function () {
@@ -59,7 +58,7 @@ var PhysicsChainCollider = cc.Class({
serializable: false,
visible: false
},
}, cc.PhysicsCollider.properties),
},
_createShape: function (scale) {
var shape = new b2.ChainShape();
@@ -36,16 +36,14 @@ var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
*/
var PhysicsCircleCollider = cc.Class({
name: 'cc.PhysicsCircleCollider',
extends: cc.CircleCollider,
mixins: [cc.PhysicsCollider],
extends: cc.PhysicsCollider,
mixins: [cc.Collider.Circle],
editor: {
menu: CC_EDITOR && 'i18n:MAIN_MENU.component.physics/Collider/Circle',
requireComponent: cc.RigidBody
},
properties: cc.PhysicsCollider.properties,
_createShape: function (scale) {
var scaleX = Math.abs(scale.x);
var scaleY = Math.abs(scale.y);
+249 -247
View File
@@ -29,271 +29,273 @@ var getWorldScale = require('../utils').getWorldScale;
/**
* @class PhysicsCollider
*/
function PhysicsCollider () {
this._fixtures = [];
this._shapes = [];
this._inited = false;
this._rect = cc.rect();
this.body = null;
}
var PhysicsCollider = cc.Class({
name: 'cc.PhysicsCollider',
extends: cc.Collider,
PhysicsCollider.prototype.onDisable = function () {
this._destroy();
};
ctor: function () {
this._fixtures = [];
this._shapes = [];
this._inited = false;
this._rect = cc.rect();
},
PhysicsCollider.prototype.onEnable = function () {
this._init();
};
PhysicsCollider.prototype.start = function () {
this._init();
};
/**
* !#en
* Apply current changes to collider, this will regenerate inner box2d fixtures.
* !#zh
* 应用当前 collider 中的修改,调用此函数会重新生成内部 box2d 的夹具。
* @method apply
*/
PhysicsCollider.prototype.apply = function () {
this._destroy();
this._init();
};
/**
* !#en
* Get the world aabb of the collider
* !#zh
* 获取碰撞体的世界坐标系下的包围盒
* @method getAABB
*/
PhysicsCollider.prototype.getAABB = function () {
var MAX = 10e6;
var minX = MAX, minY = MAX;
var maxX = -MAX, maxY = -MAX;
var fixtures = this._fixtures;
for (var i = 0; i < fixtures.length; i++) {
var fixture = fixtures[i];
var count = fixture.GetShape().GetChildCount();
for (var j = 0; j < count; j++) {
var aabb = fixture.GetAABB(j);
if (aabb.lowerBound.x < minX) minX = aabb.lowerBound.x;
if (aabb.lowerBound.y < minY) minY = aabb.lowerBound.y;
if (aabb.upperBound.x > maxX) maxX = aabb.upperBound.x;
if (aabb.upperBound.y > maxY) maxY = aabb.upperBound.y;
}
}
minX *= PTM_RATIO;
minY *= PTM_RATIO;
maxX *= PTM_RATIO;
maxY *= PTM_RATIO;
var r = this._rect;
r.x = minX;
r.y = minY;
r.width = maxX - minX;
r.height = maxY - minY;
return r;
};
PhysicsCollider.prototype._getFixtureIndex = function (fixture) {
return this._fixtures.indexOf(fixture);
};
PhysicsCollider.prototype._init = function () {
cc.director.getPhysicsManager().pushDelayEvent(this, '__init', []);
};
PhysicsCollider.prototype._destroy = function () {
cc.director.getPhysicsManager().pushDelayEvent(this, '__destroy', []);
};
PhysicsCollider.prototype.__init = function () {
if (this._inited) return;
var body = this.body || this.getComponent(cc.RigidBody);
if (!body) return;
var innerBody = body._getBody();
if (!innerBody) return;
var node = body.node;
var scale = getWorldScale(node);
var shapes = scale.x === 0 && scale.y === 0 ? [] : this._createShape(scale);
if (!(shapes instanceof Array)) {
shapes = [shapes];
}
var categoryBits = 1 << node.groupIndex;
var maskBits = 0;
var bits = cc.game.collisionMatrix[node.groupIndex];
for (let i = 0; i < bits.length; i++) {
if (!bits[i]) continue;
maskBits |= 1 << i;
}
var filter = {
categoryBits: categoryBits,
maskBits: maskBits,
groupIndex: 0
};
var manager = cc.director.getPhysicsManager();
for (let i = 0; i < shapes.length; i++) {
var shape = shapes[i];
var fixDef = new b2.FixtureDef();
fixDef.density = this.density;
fixDef.isSensor = this.sensor;
fixDef.friction = this.friction;
fixDef.restitution = this.restitution;
fixDef.shape = shape;
fixDef.filter = filter;
var fixture = innerBody.CreateFixture(fixDef);
fixture.collider = this;
if (body.enabledContactListener) {
manager._registerContactFixture(fixture);
}
this._shapes.push(shape);
this._fixtures.push(fixture);
}
this.body = body;
this._inited = true;
};
PhysicsCollider.prototype.__destroy = function () {
if (!this._inited) return;
var fixtures = this._fixtures;
var body = this.body._getBody();
var manager = cc.director.getPhysicsManager();
for (var i = fixtures.length-1; i >=0 ; i--) {
var fixture = fixtures[i];
fixture.collider = null;
manager._unregisterContactFixture(fixture);
if (body) {
body.DestroyFixture(fixture);
}
}
this.body = null;
this._fixtures.length = 0;
this._shapes.length = 0;
this._inited = false;
};
PhysicsCollider.prototype._createShape = function () {
};
PhysicsCollider.properties = {
_density: 1.0,
_sensor: false,
_friction: 0.2,
_restitution: 0,
/**
* !#en
* The density.
* !#zh
* 密度
* @property {Number} density
* @default 1
*/
density: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.density',
get: function () {
return this._density;
properties: {
_density: 1.0,
_sensor: false,
_friction: 0.2,
_restitution: 0,
/**
* !#en
* The density.
* !#zh
* 密度
* @property {Number} density
* @default 1
*/
density: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.density',
get: function () {
return this._density;
},
set: function (value) {
this._density = value;
}
},
set: function (value) {
this._density = value;
/**
* !#en
* A sensor collider collects contact information but never generates a collision response
* !#zh
* 一个传感器类型的碰撞体会产生碰撞回调,但是不会发生物理碰撞效果。
* @property {Boolean} sensor
* @default false
*/
sensor: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.sensor',
get: function () {
return this._sensor;
},
set: function (value) {
this._sensor = value;
}
},
/**
* !#en
* The friction coefficient, usually in the range [0,1].
* !#zh
* 摩擦系数,取值一般在 [0, 1] 之间
* @property {Number} friction
* @default 0.2
*/
friction: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.friction',
get: function () {
return this._friction;
},
set: function (value) {
this._friction = value;
}
},
/**
* !#en
* The restitution (elasticity) usually in the range [0,1].
* !#zh
* 弹性系数,取值一般在 [0, 1]之间
* @property {Number} restitution
* @default 0
*/
restitution: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.restitution',
get: function () {
return this._restitution;
},
set: function (value) {
this._restitution = value;
}
},
/**
* !#en
* Physics collider will find the rigidbody component on the node and set to this property.
* !#zh
* 碰撞体会在初始化时查找节点上是否存在刚体,如果查找成功则赋值到这个属性上。
* @property {RigidBody} body
* @default null
*/
body: {
default: null,
type: cc.RigidBody,
visible: false
}
},
onDisable: function () {
this._destroy();
},
onEnable: function () {
this._init();
},
start: function () {
this._init();
},
_getFixtureIndex: function (fixture) {
return this._fixtures.indexOf(fixture);
},
_init: function () {
cc.director.getPhysicsManager().pushDelayEvent(this, '__init', []);
},
_destroy: function () {
cc.director.getPhysicsManager().pushDelayEvent(this, '__destroy', []);
},
__init: function () {
if (this._inited) return;
var body = this.body || this.getComponent(cc.RigidBody);
if (!body) return;
var innerBody = body._getBody();
if (!innerBody) return;
var node = body.node;
var scale = getWorldScale(node);
var shapes = scale.x === 0 && scale.y === 0 ? [] : this._createShape(scale);
if (!(shapes instanceof Array)) {
shapes = [shapes];
}
var categoryBits = 1 << node.groupIndex;
var maskBits = 0;
var bits = cc.game.collisionMatrix[node.groupIndex];
for (let i = 0; i < bits.length; i++) {
if (!bits[i]) continue;
maskBits |= 1 << i;
}
var filter = {
categoryBits: categoryBits,
maskBits: maskBits,
groupIndex: 0
};
var manager = cc.director.getPhysicsManager();
for (let i = 0; i < shapes.length; i++) {
var shape = shapes[i];
var fixDef = new b2.FixtureDef();
fixDef.density = this.density;
fixDef.isSensor = this.sensor;
fixDef.friction = this.friction;
fixDef.restitution = this.restitution;
fixDef.shape = shape;
fixDef.filter = filter;
var fixture = innerBody.CreateFixture(fixDef);
fixture.collider = this;
if (body.enabledContactListener) {
manager._registerContactFixture(fixture);
}
this._shapes.push(shape);
this._fixtures.push(fixture);
}
this.body = body;
this._inited = true;
},
__destroy: function () {
if (!this._inited) return;
var fixtures = this._fixtures;
var body = this.body._getBody();
var manager = cc.director.getPhysicsManager();
for (var i = fixtures.length-1; i >=0 ; i--) {
var fixture = fixtures[i];
fixture.collider = null;
manager._unregisterContactFixture(fixture);
if (body) {
body.DestroyFixture(fixture);
}
}
this.body = null;
this._fixtures.length = 0;
this._shapes.length = 0;
this._inited = false;
},
_createShape: function () {
},
/**
* !#en
* A sensor collider collects contact information but never generates a collision response
* Apply current changes to collider, this will regenerate inner box2d fixtures.
* !#zh
* 一个传感器类型的碰撞体会产生碰撞回调,但是不会发生物理碰撞效果。
* @property {Boolean} sensor
* @default false
* 应用当前 collider 中的修改,调用此函数会重新生成内部 box2d 的夹具。
* @method apply
*/
sensor: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.sensor',
get: function () {
return this._sensor;
},
set: function (value) {
this._sensor = value;
}
apply: function () {
this._destroy();
this._init();
},
/**
* !#en
* The friction coefficient, usually in the range [0,1].
* Get the world aabb of the collider
* !#zh
* 摩擦系数,取值一般在 [0, 1] 之间
* @property {Number} friction
* @default 0.2
* 获取碰撞体的世界坐标系下的包围盒
* @method getAABB
*/
friction: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.friction',
get: function () {
return this._friction;
},
set: function (value) {
this._friction = value;
}
},
getAABB: function () {
var MAX = 10e6;
/**
* !#en
* The restitution (elasticity) usually in the range [0,1].
* !#zh
* 弹性系数,取值一般在 [0, 1]之间
* @property {Number} restitution
* @default 0
*/
restitution: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.restitution',
get: function () {
return this._restitution;
},
set: function (value) {
this._restitution = value;
}
},
var minX = MAX, minY = MAX;
var maxX = -MAX, maxY = -MAX;
var fixtures = this._fixtures;
for (var i = 0; i < fixtures.length; i++) {
var fixture = fixtures[i];
/**
* !#en
* Physics collider will find the rigidbody component on the node and set to this property.
* !#zh
* 碰撞体会在初始化时查找节点上是否存在刚体,如果查找成功则赋值到这个属性上。
* @property {RigidBody} body
* @default null
*/
body: {
default: null,
type: cc.RigidBody,
visible: false
var count = fixture.GetShape().GetChildCount();
for (var j = 0; j < count; j++) {
var aabb = fixture.GetAABB(j);
if (aabb.lowerBound.x < minX) minX = aabb.lowerBound.x;
if (aabb.lowerBound.y < minY) minY = aabb.lowerBound.y;
if (aabb.upperBound.x > maxX) maxX = aabb.upperBound.x;
if (aabb.upperBound.y > maxY) maxY = aabb.upperBound.y;
}
}
minX *= PTM_RATIO;
minY *= PTM_RATIO;
maxX *= PTM_RATIO;
maxY *= PTM_RATIO;
var r = this._rect;
r.x = minX;
r.y = minY;
r.width = maxX - minX;
r.height = maxY - minY;
return r;
}
};
});
cc.PhysicsCollider = module.exports = PhysicsCollider;
@@ -37,8 +37,8 @@ var PolygonSeprator = require('../CCPolygonSeprator');
*/
var PhysicsPolygonCollider = cc.Class({
name: 'cc.PhysicsPolygonCollider',
extends: cc.PolygonCollider,
mixins: [cc.PhysicsCollider],
extends: cc.PhysicsCollider,
mixins: [cc.Collider.Polygon],
editor: {
menu: CC_EDITOR && 'i18n:MAIN_MENU.component.physics/Collider/Polygon',
@@ -46,8 +46,6 @@ var PhysicsPolygonCollider = cc.Class({
requireComponent: cc.RigidBody
},
properties: cc.PhysicsCollider.properties,
_createShape: function (scale) {
var shapes = [];