From e4886d74e0d4d5a4c6d420f76cae9c184821e94c Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 25 Apr 2017 18:22:04 +0800 Subject: [PATCH 1/7] fixed physics box collider when scale is negative --- cocos2d/core/physics/collider/CCPhysicsBoxCollider.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/physics/collider/CCPhysicsBoxCollider.js b/cocos2d/core/physics/collider/CCPhysicsBoxCollider.js index fc88707096..b74dab3d3e 100644 --- a/cocos2d/core/physics/collider/CCPhysicsBoxCollider.js +++ b/cocos2d/core/physics/collider/CCPhysicsBoxCollider.js @@ -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); From 25dc4e3709f8a94542e2faf4a29acdc8734acaf1 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 25 Apr 2017 18:22:31 +0800 Subject: [PATCH 2/7] fixed physics polygon collider offset calculation --- cocos2d/core/physics/collider/CCPhysicsPolygonCollider.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/physics/collider/CCPhysicsPolygonCollider.js b/cocos2d/core/physics/collider/CCPhysicsPolygonCollider.js index e66be47a23..0b424413af 100644 --- a/cocos2d/core/physics/collider/CCPhysicsPolygonCollider.js +++ b/cocos2d/core/physics/collider/CCPhysicsPolygonCollider.js @@ -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) { From dca6ae57df7b78b4e27f94e149823eb3a5c24150 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 25 Apr 2017 18:22:59 +0800 Subject: [PATCH 3/7] add tiled map culling flag to macro --- cocos2d/core/platform/CCMacro.js | 11 +++++++++- cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js | 23 +++++++++++--------- cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js | 23 +++++++++++--------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/cocos2d/core/platform/CCMacro.js b/cocos2d/core/platform/CCMacro.js index 74f50f51aa..b56a2d7e8b 100644 --- a/cocos2d/core/platform/CCMacro.js +++ b/cocos2d/core/platform/CCMacro.js @@ -1885,7 +1885,16 @@ 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. + * !#zh + * 是否开启瓦片地图的自动裁减功能 + * @property {Boolean} ENABLE_TILEDMAP_CULLING + */ + ENABLE_TILEDMAP_CULLING: true, }; /** diff --git a/cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js b/cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js index 838a74ae8f..1d1c74ad79 100644 --- a/cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js +++ b/cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js @@ -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, diff --git a/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js b/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js index 9920251d1b..0a5103de0d 100644 --- a/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js +++ b/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js @@ -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, From 693b5e98167d2153c6411b2527b1159a30c1a211 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 25 Apr 2017 18:45:28 +0800 Subject: [PATCH 4/7] should invert manifold normal when selfCollider not equal to colliderA --- cocos2d/core/physics/CCPhysicsContact.js | 32 ++++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/cocos2d/core/physics/CCPhysicsContact.js b/cocos2d/core/physics/CCPhysicsContact.js index 04add3e000..65a6527cfc 100644 --- a/cocos2d/core/physics/CCPhysicsContact.js +++ b/cocos2d/core/physics/CCPhysicsContact.js @@ -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]) { From 60849ca905ba0cc4bda505a39a302af6c6ace8ae Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 26 Apr 2017 14:01:52 +0800 Subject: [PATCH 5/7] update joint api doc --- cocos2d/core/physics/joint/CCJoint.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cocos2d/core/physics/joint/CCJoint.js b/cocos2d/core/physics/joint/CCJoint.js index cc39f3e45c..3dd624bde6 100644 --- a/cocos2d/core/physics/joint/CCJoint.js +++ b/cocos2d/core/physics/joint/CCJoint.js @@ -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(); From b648accd4d1d76626b45b566acb5206c462a9a27 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 26 Apr 2017 14:40:08 +0800 Subject: [PATCH 6/7] support offset of PhysicsCircleCollider --- cocos2d/core/physics/collider/CCPhysicsCircleCollider.js | 9 ++++++++- jsb/jsb-box2d.js | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/physics/collider/CCPhysicsCircleCollider.js b/cocos2d/core/physics/collider/CCPhysicsCircleCollider.js index 2d9b466c39..d10daf4840 100644 --- a/cocos2d/core/physics/collider/CCPhysicsCircleCollider.js +++ b/cocos2d/core/physics/collider/CCPhysicsCircleCollider.js @@ -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; } }); diff --git a/jsb/jsb-box2d.js b/jsb/jsb-box2d.js index 8e4764631e..8d5b76f0f8 100644 --- a/jsb/jsb-box2d.js +++ b/jsb/jsb-box2d.js @@ -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; From 2b182bf29f597877f00d10af000fdc7b2e42c14b Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 26 Apr 2017 15:14:18 +0800 Subject: [PATCH 7/7] update api doc for ENABLE_TILEDMAP_CULLING --- cocos2d/core/platform/CCMacro.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/platform/CCMacro.js b/cocos2d/core/platform/CCMacro.js index b56a2d7e8b..5bbd720015 100644 --- a/cocos2d/core/platform/CCMacro.js +++ b/cocos2d/core/platform/CCMacro.js @@ -1890,9 +1890,12 @@ cc.macro = { /** * !#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, };