Merge pull request #1616 from 2youyou2/physics_example

improve physics
This commit is contained in:
Wang Nan
2017-04-26 17:23:23 +08:00
committed by GitHub
9 changed files with 91 additions and 35 deletions
+24 -8
View File
@@ -213,6 +213,8 @@ PhysicsContact.prototype.init = function (b2contact) {
this.disabledOnce = false;
this._impulse = null;
this._inverted = false;
this._b2contact = b2contact;
b2contact._contact = this;
};
@@ -279,6 +281,11 @@ PhysicsContact.prototype.getWorldManifold = function () {
normal.y = b2worldmanifold.normal.y;
}
if (this._inverted) {
normal.x *= -1;
normal.y *= -1;
}
return worldmanifold;
};
@@ -292,6 +299,8 @@ PhysicsContact.prototype.getWorldManifold = function () {
*/
PhysicsContact.prototype.getManifold = function () {
var points = manifold.points;
var localNormal = manifold.localNormal;
var localPoint = manifold.localPoint;
if (CC_JSB) {
var wrapper = cc.PhysicsUtils.getContactManifoldWrapper();
@@ -307,10 +316,10 @@ PhysicsContact.prototype.getManifold = function () {
points[i] = p;
}
manifold.localNormal.x = wrapper.getLocalNormalX();
manifold.localNormal.y = wrapper.getLocalNormalY();
manifold.localPoint.x = wrapper.getLocalPointX();
manifold.localPoint.y = wrapper.getLocalPointY();
localNormal.x = wrapper.getLocalNormalX();
localNormal.y = wrapper.getLocalNormalY();
localPoint.x = wrapper.getLocalPointX();
localPoint.y = wrapper.getLocalPointY();
manifold.type = wrapper.getType();
}
else {
@@ -329,13 +338,18 @@ PhysicsContact.prototype.getManifold = function () {
points[i] = p;
}
manifold.localPoint.x = b2manifold.localPoint.x * PTM_RATIO;
manifold.localPoint.y = b2manifold.localPoint.y * PTM_RATIO;
manifold.localNormal.x = b2manifold.localNormal.x;
manifold.localNormal.y = b2manifold.localNormal.y;
localPoint.x = b2manifold.localPoint.x * PTM_RATIO;
localPoint.y = b2manifold.localPoint.y * PTM_RATIO;
localNormal.x = b2manifold.localNormal.x;
localNormal.y = b2manifold.localNormal.y;
manifold.type = b2manifold.type;
}
if (this._inverted) {
localNormal.x *= -1;
localNormal.y *= -1;
}
return manifold;
};
@@ -405,6 +419,7 @@ PhysicsContact.prototype.emit = function (contactType) {
if (bodyA.enabledContactListener) {
comps = bodyA.node._components;
this._inverted = false;
for (i = 0, l = comps.length; i < l; i++) {
comp = comps[i];
if (comp[func]) {
@@ -415,6 +430,7 @@ PhysicsContact.prototype.emit = function (contactType) {
if (bodyB.enabledContactListener) {
comps = bodyB.node._components;
this._inverted = true;
for (i = 0, l = comps.length; i < l; i++) {
comp = comps[i];
if (comp[func]) {
@@ -46,10 +46,12 @@ var PhysicsBoxCollider = cc.Class({
properties: cc.PhysicsCollider.properties,
_createShape: function (scale) {
var width = this.size.width/2/PTM_RATIO * scale.x;
var height = this.size.height/2/PTM_RATIO * scale.y;
var offsetX = this.offset.x/PTM_RATIO *scale.x;
var offsetY = this.offset.y/PTM_RATIO *scale.y;
var scaleX = Math.abs(scale.x);
var scaleY = Math.abs(scale.y);
var width = this.size.width/2/PTM_RATIO * scaleX;
var height = this.size.height/2/PTM_RATIO * scaleY;
var offsetX = this.offset.x/PTM_RATIO *scaleX;
var offsetY = this.offset.y/PTM_RATIO *scaleY;
var shape = new b2.PolygonShape();
shape.SetAsBox(width, height, new b2.Vec2(offsetX, offsetY), 0);
@@ -46,8 +46,15 @@ var PhysicsCircleCollider = cc.Class({
properties: cc.PhysicsCollider.properties,
_createShape: function (scale) {
var scaleX = Math.abs(scale.x);
var scaleY = Math.abs(scale.y);
var offsetX = this.offset.x/PTM_RATIO * scaleX;
var offsetY = this.offset.y/PTM_RATIO * scaleY;
var shape = new b2.CircleShape();
shape.m_radius = this.radius / PTM_RATIO * scale.x;
shape.m_radius = this.radius / PTM_RATIO * scaleX;
shape.m_p = new b2.Vec2(offsetX, offsetY);
return shape;
}
});
@@ -60,6 +60,7 @@ var PhysicsPolygonCollider = cc.Class({
if (ret === 0) {
var polys = PolygonSeprator.calcShapes(points);
var offset = this.offset;
for (var i = 0; i < polys.length; i++) {
var poly = polys[i];
@@ -72,7 +73,9 @@ var PhysicsPolygonCollider = cc.Class({
shape = new b2.PolygonShape();
}
var p = poly[j];
var v = new b2.Vec2(p.x/PTM_RATIO*scale.x, p.y/PTM_RATIO*scale.y);
var x = (p.x + offset.x)/PTM_RATIO*scale.x;
var y = (p.y + offset.y)/PTM_RATIO*scale.y;
var v = new b2.Vec2(x, y);
vertices.push( v );
if (!firstVertice) {
+7
View File
@@ -97,6 +97,13 @@ var Joint = cc.Class({
this._init();
},
/**
* !#en
* Apply current changes to joint, this will regenerate inner box2d joint.
* !#zh
* 应用当前关节中的修改,调用此函数会重新生成内部 box2d 的关节。
* @method apply
*/
apply: function () {
this._destroy();
this._init();
+13 -1
View File
@@ -1885,7 +1885,19 @@ cc.macro = {
* 它的值被 native 宏 CC_ENABLE_GC_FOR_NATIVE_OBJECTS 所控制,修改 JS 宏的值不会产生任何效果。
* @property {Number} ENABLE_GC_FOR_NATIVE_OBJECTS
*/
ENABLE_GC_FOR_NATIVE_OBJECTS: true
ENABLE_GC_FOR_NATIVE_OBJECTS: true,
/**
* !#en
* Whether or not enabled tiled map auto culling.
* If you use cc.Camera as tiled map's camera, please disable this macro.
* !#zh
* 是否开启瓦片地图的自动裁减功能。
* 如果需要使用 cc.Camera 来作为瓦片地图的摄像机的话,那么请关闭此宏
* @property {Boolean} ENABLE_TILEDMAP_CULLING
* @default true
*/
ENABLE_TILEDMAP_CULLING: true,
};
/**
+13 -10
View File
@@ -78,16 +78,19 @@ proto.rendering = function (ctx, scaleX, scaleY) {
// Culling
var startCol = 0, startRow = 0,
maxCol = cols, maxRow = rows;
if (!hasRotation && layerOrientation === Orientation.ORTHO) {
startCol = Math.floor(-(mapx - extw * a) / (maptw * a));
startRow = Math.floor((mapy - exth * d + mapth * rows * d - winh) / (mapth * d));
maxCol = Math.ceil((winw - mapx + extw * a) / (maptw * a));
maxRow = rows - Math.floor(-(mapy + exth * d) / (mapth * d));
// Adjustment
if (startCol < 0) startCol = 0;
if (startRow < 0) startRow = 0;
if (maxCol > cols) maxCol = cols;
if (maxRow > rows) maxRow = rows;
if (cc.macro.ENABLE_TILEDMAP_CULLING) {
if (!hasRotation && layerOrientation === Orientation.ORTHO) {
startCol = Math.floor(-(mapx - extw * a) / (maptw * a));
startRow = Math.floor((mapy - exth * d + mapth * rows * d - winh) / (mapth * d));
maxCol = Math.ceil((winw - mapx + extw * a) / (maptw * a));
maxRow = rows - Math.floor(-(mapy + exth * d) / (mapth * d));
// Adjustment
if (startCol < 0) startCol = 0;
if (startRow < 0) startRow = 0;
if (maxCol > cols) maxCol = cols;
if (maxRow > rows) maxRow = rows;
}
}
var i, row, col, colOffset = startRow * cols, z,
+13 -10
View File
@@ -102,16 +102,19 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset) {
// Culling
var startCol = 0, startRow = 0,
maxCol = cols, maxRow = rows;
if (!hasRotation && layerOrientation === Orientation.ORTHO) {
startCol = Math.floor(-(mapx - extw * a) / (maptw * a));
startRow = Math.floor((mapy - exth * d + mapth * rows * d - winh) / (mapth * d));
maxCol = Math.ceil((winw - mapx + extw * a) / (maptw * a));
maxRow = rows - Math.floor(-(mapy + exth * d) / (mapth * d));
// Adjustment
if (startCol < 0) startCol = 0;
if (startRow < 0) startRow = 0;
if (maxCol > cols) maxCol = cols;
if (maxRow > rows) maxRow = rows;
if (cc.macro.ENABLE_TILEDMAP_CULLING) {
if (!hasRotation && layerOrientation === Orientation.ORTHO) {
startCol = Math.floor(-(mapx - extw * a) / (maptw * a));
startRow = Math.floor((mapy - exth * d + mapth * rows * d - winh) / (mapth * d));
maxCol = Math.ceil((winw - mapx + extw * a) / (maptw * a));
maxRow = rows - Math.floor(-(mapy + exth * d) / (mapth * d));
// Adjustment
if (startCol < 0) startCol = 0;
if (startRow < 0) startRow = 0;
if (maxCol > cols) maxCol = cols;
if (maxRow > rows) maxRow = rows;
}
}
var row, col,
+3
View File
@@ -419,6 +419,9 @@ b2.WorldManifold = function ()
var _p = b2.Shape.prototype;
cc.defineGetterSetter(_p, 'm_radius', _p.GetRadius, _p.SetRadius);
// b2CircleShape
_p = b2.CircleShape.prototype;
cc.defineGetterSetter(_p, 'm_p', _p.GetPosition, _p.SetPosition);
// b2Body Type
b2.Body.b2_staticBody = 0;