From dba48661b022520f24b4e8638c70ca6c6c700342 Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Tue, 17 Jul 2018 09:59:13 +0800 Subject: [PATCH] fixed physics system (#2984) * should reset physics contact * fixed physics debug draw * fixed revolute joint limit --- cocos2d/core/physics/CCPhysicsContact.js | 4 ++++ cocos2d/core/physics/CCPhysicsManager.js | 9 ++++++++- cocos2d/core/physics/joint/CCRevoluteJoint.js | 18 ++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/physics/CCPhysicsContact.js b/cocos2d/core/physics/CCPhysicsContact.js index 7b5ad944ba..a1aaf59377 100644 --- a/cocos2d/core/physics/CCPhysicsContact.js +++ b/cocos2d/core/physics/CCPhysicsContact.js @@ -217,6 +217,10 @@ PhysicsContact.prototype.init = function (b2contact) { }; PhysicsContact.prototype.reset = function () { + this.setTangentSpeed(0); + this.resetFriction(); + this.resetRestitution(); + this.colliderA = null; this.colliderB = null; this.disabled = false; diff --git a/cocos2d/core/physics/CCPhysicsManager.js b/cocos2d/core/physics/CCPhysicsManager.js index 59640f511c..e13ff732ff 100644 --- a/cocos2d/core/physics/CCPhysicsManager.js +++ b/cocos2d/core/physics/CCPhysicsManager.js @@ -537,7 +537,9 @@ var PhysicsManager = cc.Class({ cc.game.addPersistRootNode(node); this._debugDrawer = node.addComponent(cc.Graphics); - this._world.SetDebugDraw(new DebugDraw(this._debugDrawer)); + let debugDraw = new DebugDraw(this._debugDrawer); + debugDraw.SetFlags(this.debugDrawFlags); + this._world.SetDebugDraw(debugDraw); } } }); @@ -609,6 +611,11 @@ cc.js.getset(PhysicsManager.prototype, 'debugDrawFlags', } this._debugDrawFlags = value; + + if (value) { + this._checkDebugDrawValid(); + this._world.m_debugDraw.SetFlags(value); + } } ); diff --git a/cocos2d/core/physics/joint/CCRevoluteJoint.js b/cocos2d/core/physics/joint/CCRevoluteJoint.js index fa0a476493..61d3aca3ff 100644 --- a/cocos2d/core/physics/joint/CCRevoluteJoint.js +++ b/cocos2d/core/physics/joint/CCRevoluteJoint.js @@ -198,14 +198,28 @@ var RevoluteJoint = cc.Class({ return 0; }, + /** + * #!en + * Set the max and min limit angle. + * #!zh + * 设置关节的角度最大和最小角度。 + * @param {Number} lower + * @param {Number} upper + */ + setLimits (lower, upper) { + if (this._joint) { + return this._joint.SetLimits(lower * ANGLE_TO_PHYSICS_ANGLE, upper * ANGLE_TO_PHYSICS_ANGLE); + } + }, + _createJointDef: function () { var def = new b2.RevoluteJointDef(); def.localAnchorA = new b2.Vec2(this.anchor.x/PTM_RATIO, this.anchor.y/PTM_RATIO); def.localAnchorB = new b2.Vec2(this.connectedAnchor.x/PTM_RATIO, this.connectedAnchor.y/PTM_RATIO); // cocos degree 0 is to right, and box2d degree 0 is to up. - def.lowerAngle = (this.upperAngle + 90) * ANGLE_TO_PHYSICS_ANGLE; - def.upperAngle = (this.lowerAngle + 90) * ANGLE_TO_PHYSICS_ANGLE; + def.lowerAngle = this.upperAngle* ANGLE_TO_PHYSICS_ANGLE; + def.upperAngle = this.lowerAngle* ANGLE_TO_PHYSICS_ANGLE; def.maxMotorTorque = this.maxMotorTorque; def.motorSpeed = this.motorSpeed * ANGLE_TO_PHYSICS_ANGLE;