Merge pull request #1628 from 2youyou2/physics

improve sync node position to box2d rigid body
This commit is contained in:
Wang Nan
2017-05-05 10:29:26 +08:00
committed by GitHub
2 changed files with 41 additions and 27 deletions
+30 -22
View File
@@ -788,32 +788,35 @@ var RigidBody = cc.Class({
this._destroy();
},
onLoad: function () {
this._ignoreNodeChanges = false;
_registerNodeEvents: function () {
var node = this.node;
node.on('position-changed', this._onNodePositionChanged, this);
node.on('rotation-changed', this._onNodeRotationChanged, this);
node.on('scale-changed', this._onNodeScaleChanged, this);
},
node.on('position-changed', function() {
if (!this._ignoreNodeChanges) {
this.syncPosition(true);
}
}, this);
_unregisterNodeEvents: function () {
var node = this.node;
node.off('position-changed', this._onNodePositionChanged, this);
node.off('rotation-changed', this._onNodeRotationChanged, this);
node.off('scale-changed', this._onNodeScaleChanged, this);
},
node.on('rotation-changed', function() {
var b2body = this._b2Body;
if (!this._ignoreNodeChanges && b2body) {
this.syncRotation(true);
}
}, this);
_onNodePositionChanged: function () {
this.syncPosition(true);
},
node.on('scale-changed', function () {
if (!this._ignoreNodeChanges) {
var colliders = this.getComponents(cc.Collider);
for (var i = 0; i < colliders.length; i++) {
colliders[i].apply();
}
_onNodeRotationChanged: function (event) {
this.syncRotation(true);
},
_onNodeScaleChanged: function (event) {
if (this._b2Body) {
var colliders = this.getComponents(cc.Collider);
for (var i = 0; i < colliders.length; i++) {
colliders[i].apply();
}
}, this);
}
},
_init: function () {
@@ -826,6 +829,8 @@ var RigidBody = cc.Class({
__init: function () {
if (this._inited) return;
this._registerNodeEvents();
var bodyDef = new b2.BodyDef();
if (this.type === BodyType.Animated) {
@@ -851,7 +856,7 @@ var RigidBody = cc.Class({
var node = this.node;
var pos = node.convertToWorldSpaceAR(VEC2_ZERO);
bodyDef.position = new b2.Vec2(pos.x / PTM_RATIO, pos.y / PTM_RATIO);
bodyDef.angle = -(Math.PI / 180) * getWorldRotation(node);
bodyDef.angle = -(Math.PI / 180) * getWorldRotation(node);
cc.director.getPhysicsManager()._addBody(this, bodyDef);
@@ -859,7 +864,10 @@ var RigidBody = cc.Class({
},
__destroy: function () {
if (!this._inited) return;
cc.director.getPhysicsManager()._removeBody(this);
this._unregisterNodeEvents();
this._inited = false;
},
@@ -28,18 +28,24 @@ PhysicsUtils.prototype.syncNode = function () {
var angle = b2body.GetAngle() * PHYSICS_ANGLE_TO_ANGLE;
body._ignoreNodeChanges = true;
// When node's parent is not scene, convert position and rotation.
if (node.parent.parent !== null) {
tempPosition = node.parent.convertToNodeSpaceAR( tempPosition );
angle = convertToNodeRotation( node.parent, angle );
}
node.position = tempPosition;
node.rotation = angle;
var sgNode = node._sgNode;
body._ignoreNodeChanges = false;
// sync position
var position = node._position;
position.x = tempPosition.x;
position.y = tempPosition.y;
sgNode.setPosition(position);
// sync rotation
node._rotationX = node._rotationY = angle;
sgNode.rotation = angle;
}
};