mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
@@ -1,64 +1,197 @@
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('./CCPhysicsTypes').PTM_RATIO;
|
||||
var ContactType = require('./CCPhysicsTypes').ContactType;
|
||||
|
||||
function PhysicsContact (contact) {
|
||||
this._contact = contact;
|
||||
this.colliderA = contact.GetFixtureA().collider;
|
||||
this.colliderB = contact.GetFixtureB().collider;
|
||||
this.disabled = false;
|
||||
var pools = [];
|
||||
|
||||
|
||||
// temp world manifold
|
||||
var pointCache = [cc.v2(), cc.v2()];
|
||||
|
||||
var b2worldmanifold;
|
||||
if (!CC_JSB) {
|
||||
b2worldmanifold = new b2.WorldManifold();
|
||||
}
|
||||
|
||||
var worldmanifold = {
|
||||
points: [],
|
||||
separations: [],
|
||||
localNormal: cc.v2()
|
||||
};
|
||||
|
||||
// temp manifold
|
||||
function ManifoldPoint () {
|
||||
this.localPoint = cc.v2();
|
||||
this.normalImpulse = 0;
|
||||
this.tangentImpulse = 0;
|
||||
}
|
||||
|
||||
var manifoldPointCache = [new ManifoldPoint(), new ManifoldPoint()];
|
||||
|
||||
var b2manifold;
|
||||
if (!CC_JSB) {
|
||||
b2manifold = new b2.Manifold();
|
||||
}
|
||||
|
||||
var manifold = {
|
||||
type: 0,
|
||||
localPoint: cc.v2(),
|
||||
localNormal: cc.v2(),
|
||||
points: []
|
||||
};
|
||||
|
||||
// temp impulse
|
||||
var impulse = {
|
||||
normalImpulses: [],
|
||||
tangentImpulses: []
|
||||
};
|
||||
|
||||
// PhysicsContact
|
||||
function PhysicsContact () {
|
||||
}
|
||||
|
||||
PhysicsContact.prototype.init = function (b2contact) {
|
||||
this.colliderA = b2contact.GetFixtureA().collider;
|
||||
this.colliderB = b2contact.GetFixtureB().collider;
|
||||
this.disabled = false;
|
||||
this._impulse = null;
|
||||
|
||||
|
||||
this._b2contact = b2contact;
|
||||
b2contact._contact = this;
|
||||
};
|
||||
|
||||
PhysicsContact.prototype.reset = function () {
|
||||
this.colliderA = null;
|
||||
this.colliderB = null;
|
||||
this.disabled = false;
|
||||
this._impulse = null;
|
||||
|
||||
this._b2contact._contact = null;
|
||||
this._b2contact = null;
|
||||
};
|
||||
|
||||
PhysicsContact.prototype.getWorldManifold = function () {
|
||||
var worldmanifold;
|
||||
var points = worldmanifold.points;
|
||||
var separations = worldmanifold.separations;
|
||||
|
||||
if (CC_JSB) {
|
||||
worldmanifold = cc.director.getPhysicsManager()._utils.getContactWorldManifold(this._contact);
|
||||
var wrapper = cc.PhysicsUtils.getContactWorldManifoldWrapper(this._b2contact);
|
||||
var count = wrapper.getCount();
|
||||
var localNormal = worldmanifold.localNormal;
|
||||
|
||||
points.length = separations.length = count;
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
let p = pointCache[i];
|
||||
p.x = wrapper.getX(i);
|
||||
p.y = wrapper.getY(i);
|
||||
|
||||
points[i] = p;
|
||||
separations[i] = wrapper.getSeparation(i);
|
||||
}
|
||||
|
||||
localNormal.x = wrapper.getNormalX();
|
||||
localNormal.y = wrapper.getNormalY();
|
||||
}
|
||||
else {
|
||||
worldmanifold = new b2.WorldManifold();
|
||||
this._contact.GetWorldManifold(worldmanifold);
|
||||
}
|
||||
this._b2contact.GetWorldManifold(b2worldmanifold);
|
||||
var b2points = b2worldmanifold.points;
|
||||
var b2separations = b2worldmanifold.separations;
|
||||
|
||||
var i;
|
||||
var points = worldmanifold.points;
|
||||
for (i = 0; i < points.length; i++) {
|
||||
var p = points[i];
|
||||
if (!p) break;
|
||||
|
||||
points[i] = cc.v2(p.x * CC_PTM_RATIO, p.y * CC_PTM_RATIO);
|
||||
}
|
||||
|
||||
points.length = i;
|
||||
|
||||
var separations = worldmanifold.separations;
|
||||
separations.length = points.length;
|
||||
|
||||
for (i = 0; i < separations.length; i++) {
|
||||
separations[i] *= CC_PTM_RATIO;
|
||||
var count = this._b2contact.GetManifold().pointCount;
|
||||
points.length = separations.length = count;
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
var p = pointCache[i];
|
||||
p.x = b2points[i].x * PTM_RATIO;
|
||||
p.y = b2points[i].y * PTM_RATIO;
|
||||
|
||||
points[i] = p;
|
||||
separations[i] = b2separations[i] * PTM_RATIO;
|
||||
}
|
||||
}
|
||||
|
||||
return worldmanifold;
|
||||
};
|
||||
|
||||
PhysicsContact.prototype.getManifold = function () {
|
||||
var manifold = this._contact.GetManifold();
|
||||
if (!CC_JSB) {
|
||||
manifold = manifold.Clone();
|
||||
}
|
||||
|
||||
var points = manifold.points;
|
||||
for (var i = 0; i < manifold.pointCount; i++) {
|
||||
var p = points[i].localPoint;
|
||||
points[i].localPoint = cc.v2(p.x * CC_PTM_RATIO, p.y * CC_PTM_RATIO);
|
||||
}
|
||||
|
||||
if (CC_JSB) {
|
||||
var wrapper = cc.PhysicsUtils.getContactManifoldWrapper();
|
||||
var count = points.length = wrapper.getCount();
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
var p = manifoldPointCache[i];
|
||||
p.localPoint.x = wrapper.getX(i);
|
||||
p.localPoint.y = wrapper.getX(i);
|
||||
p.normalImpulse = wrapper.getNormalImpulse(i);
|
||||
p.tangentImpulse = wrapper.getTangentImpulse(i);
|
||||
|
||||
manifold.localPoint = cc.v2(manifold.localPoint.x * CC_PTM_RATIO, manifold.localPoint.y * CC_PTM_RATIO);
|
||||
manifold.localNormal = cc.v2(manifold.localNormal);
|
||||
points[i] = p;
|
||||
}
|
||||
|
||||
manifold.localNormal.x = wrapper.getLocalNormalX();
|
||||
manifold.localNormal.y = wrapper.getLocalNormalY();
|
||||
manifold.localPoint.x = wrapper.getLocalPointX();
|
||||
manifold.localPoint.y = wrapper.getLocalPointY();
|
||||
manifold.type = wrapper.getType();
|
||||
}
|
||||
else {
|
||||
var b2manifold = this._b2contact.GetManifold();
|
||||
var b2points = b2manifold.points;
|
||||
var count = points.length = b2manifold.pointCount;
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
var p = manifoldPointCache[i];
|
||||
var b2p = b2points[i];
|
||||
p.localPoint.x = b2p.localPoint.x * PTM_RATIO;
|
||||
p.localPoint.Y = b2p.localPoint.Y * PTM_RATIO;
|
||||
p.normalImpulse = b2p.normalImpulse * PTM_RATIO;
|
||||
p.tangentImpulse = b2p.tangentImpulse;
|
||||
|
||||
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;
|
||||
manifold.type = b2manifold.type;
|
||||
}
|
||||
|
||||
return manifold;
|
||||
};
|
||||
|
||||
PhysicsContact.prototype.getImpulse = function () {
|
||||
var b2impulse = this._impulse;
|
||||
if (!b2impulse) return null;
|
||||
|
||||
var normalImpulses = impulse.normalImpulses;
|
||||
var tangentImpulses = impulse.tangentImpulses;
|
||||
var count;
|
||||
|
||||
if (CC_JSB) {
|
||||
count = b2impulse.getCount();
|
||||
for (var i = 0; i < count; i++) {
|
||||
normalImpulses[i] = b2impulse.getNormalImpulse(i);
|
||||
tangentImpulses[i] = b2impulse.getTangentImpulse(i);
|
||||
}
|
||||
}
|
||||
else {
|
||||
count = b2impulse.count;
|
||||
for (var i = 0; i < count; i++) {
|
||||
normalImpulses[i] = b2impulse.normalImpulses[i] * PTM_RATIO;
|
||||
tangentImpulses[i] = b2impulse.tangentImpulses[i];
|
||||
}
|
||||
}
|
||||
|
||||
tangentImpulses.length = normalImpulses.length = count;
|
||||
|
||||
return impulse;
|
||||
};
|
||||
|
||||
PhysicsContact.prototype.emit = function (contactType) {
|
||||
var func;
|
||||
switch (contactType) {
|
||||
@@ -82,8 +215,6 @@ PhysicsContact.prototype.emit = function (contactType) {
|
||||
var bodyA = colliderA.body;
|
||||
var bodyB = colliderB.body;
|
||||
|
||||
var a1 = arguments[1]; // now just maybe impulse
|
||||
|
||||
var comps;
|
||||
var i, l, comp;
|
||||
|
||||
@@ -92,7 +223,7 @@ PhysicsContact.prototype.emit = function (contactType) {
|
||||
for (i = 0, l = comps.length; i < l; i++) {
|
||||
comp = comps[i];
|
||||
if (comp[func]) {
|
||||
comp[func](this, colliderA, colliderB, a1);
|
||||
comp[func](this, colliderA, colliderB);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,23 +233,57 @@ PhysicsContact.prototype.emit = function (contactType) {
|
||||
for (i = 0, l = comps.length; i < l; i++) {
|
||||
comp = comps[i];
|
||||
if (comp[func]) {
|
||||
comp[func](this, colliderB, colliderA, a1);
|
||||
comp[func](this, colliderB, colliderA);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
PhysicsContact.get = function (b2contact) {
|
||||
var c;
|
||||
if (pools.length === 0) {
|
||||
c = new cc.PhysicsContact();
|
||||
}
|
||||
else {
|
||||
c = pools.pop();
|
||||
}
|
||||
|
||||
c.init(b2contact);
|
||||
return c;
|
||||
};
|
||||
|
||||
PhysicsContact.put = function (b2contact) {
|
||||
var c = b2contact._contact;
|
||||
if (!c) return;
|
||||
|
||||
pools.push(c);
|
||||
c.reset();
|
||||
};
|
||||
|
||||
[
|
||||
'IsTouching',
|
||||
'SetEnabled', 'IsEnabled',
|
||||
'SetFriction', 'GetFriction',
|
||||
'ResetFriction', 'SetRestitution',
|
||||
'GetRestitution', 'ResetRestitution',
|
||||
'SetTangentSpeed', 'GetTangentSpeed',
|
||||
'IsEnabled',
|
||||
'GetFriction',
|
||||
'ResetFriction',
|
||||
'GetRestitution',
|
||||
'ResetRestitution',
|
||||
'GetTangentSpeed',
|
||||
].forEach(function(name) {
|
||||
var funcName = name[0].toLowerCase() + name.substr(1, name.length-1);
|
||||
PhysicsContact.prototype[funcName] = function () {
|
||||
return this._contact[name].apply(this._contact, arguments);
|
||||
return this._b2contact[name]();
|
||||
};
|
||||
});
|
||||
|
||||
[
|
||||
'SetEnabled',
|
||||
'SetFriction',
|
||||
'SetRestitution',
|
||||
'SetTangentSpeed',
|
||||
].forEach(function(name) {
|
||||
var funcName = name[0].toLowerCase() + name.substr(1, name.length-1);
|
||||
PhysicsContact.prototype[funcName] = function (arg) {
|
||||
this._b2contact[name](arg);
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@@ -23,14 +23,14 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = 32;
|
||||
var CC_TO_PHYSICS_ANGLE = -Math.PI / 180;
|
||||
var PHYSICS_TO_CC_ANGLE = -180 / Math.PI;
|
||||
|
||||
var ContactType = require('./CCPhysicsTypes').ContactType;
|
||||
var BodyType = require('./CCPhysicsTypes').BodyType;
|
||||
var RayCastType = require('./CCPhysicsTypes').RayCastType;
|
||||
|
||||
var PTM_RATIO = require('./CCPhysicsTypes').PTM_RATIO;
|
||||
var ANGLE_TO_PHYSICS_ANGLE = require('./CCPhysicsTypes').ANGLE_TO_PHYSICS_ANGLE;
|
||||
var PHYSICS_ANGLE_TO_ANGLE = require('./CCPhysicsTypes').PHYSICS_ANGLE_TO_ANGLE;
|
||||
|
||||
var STEP_DT = 1/60;
|
||||
|
||||
var PhysicsManager = cc.Class({
|
||||
@@ -74,14 +74,15 @@ var PhysicsManager = cc.Class({
|
||||
this.emit('before-step');
|
||||
|
||||
this._steping = true;
|
||||
var timeStep = 1/cc.game.config['frameRate'];
|
||||
|
||||
// http://new.gafferongames.com/post/fix_your_timestep/
|
||||
// will be super slow
|
||||
|
||||
// this._accumulator += dt;
|
||||
// while (this._accumulator > STEP_DT) {
|
||||
world.Step(STEP_DT, 10, 10);
|
||||
// this._accumulator -= STEP_DT;
|
||||
// while (this._accumulator > timeStep) {
|
||||
world.Step(timeStep, 10, 10);
|
||||
// this._accumulator -= timeStep;
|
||||
// }
|
||||
|
||||
world.DrawDebugData();
|
||||
@@ -100,10 +101,10 @@ var PhysicsManager = cc.Class({
|
||||
},
|
||||
|
||||
testPoint: function (point) {
|
||||
point = new b2.Vec2(point.x/CC_PTM_RATIO, point.y/CC_PTM_RATIO);
|
||||
point = new b2.Vec2(point.x/PTM_RATIO, point.y/PTM_RATIO);
|
||||
|
||||
var aabb = new b2.AABB();
|
||||
var d = new b2.Vec2(0.2/CC_PTM_RATIO, 0.2/CC_PTM_RATIO);
|
||||
var d = new b2.Vec2(0.2/PTM_RATIO, 0.2/PTM_RATIO);
|
||||
aabb.lowerBound = new b2.Vec2(point.x-d.x, point.y-d.y);
|
||||
aabb.upperBound = new b2.Vec2(point.x+d.x, point.y+d.y);
|
||||
|
||||
@@ -120,8 +121,8 @@ var PhysicsManager = cc.Class({
|
||||
|
||||
testAABB: function (rect) {
|
||||
var aabb = new b2.AABB();
|
||||
aabb.lowerBound = new b2.Vec2(rect.xMin/CC_PTM_RATIO, rect.yMin/CC_PTM_RATIO);
|
||||
aabb.upperBound = new b2.Vec2(rect.xMax/CC_PTM_RATIO, rect.yMax/CC_PTM_RATIO);
|
||||
aabb.lowerBound = new b2.Vec2(rect.xMin/PTM_RATIO, rect.yMin/PTM_RATIO);
|
||||
aabb.upperBound = new b2.Vec2(rect.xMax/PTM_RATIO, rect.yMax/PTM_RATIO);
|
||||
|
||||
var callback = new cc.PhysicsAABBQueryCallback();
|
||||
this._world.QueryAABB(callback, aabb);
|
||||
@@ -141,8 +142,8 @@ var PhysicsManager = cc.Class({
|
||||
|
||||
type = type || RayCastType.Closest;
|
||||
|
||||
p1 = new b2.Vec2(p1.x/CC_PTM_RATIO, p1.y/CC_PTM_RATIO);
|
||||
p2 = new b2.Vec2(p2.x/CC_PTM_RATIO, p2.y/CC_PTM_RATIO);
|
||||
p1 = new b2.Vec2(p1.x/PTM_RATIO, p1.y/PTM_RATIO);
|
||||
p2 = new b2.Vec2(p2.x/PTM_RATIO, p2.y/PTM_RATIO);
|
||||
|
||||
var callback = new cc.PhysicsRayCastCallback(type);
|
||||
this._world.RayCast(callback, p1, p2);
|
||||
@@ -160,7 +161,7 @@ var PhysicsManager = cc.Class({
|
||||
results.push({
|
||||
collider: collider,
|
||||
fixtureIndex: collider._getFixtureIndex(fixture),
|
||||
point: cc.v2(points[i].x*CC_PTM_RATIO, points[i].y*CC_PTM_RATIO),
|
||||
point: cc.v2(points[i].x*PTM_RATIO, points[i].y*PTM_RATIO),
|
||||
normal: cc.v2(normals[i]),
|
||||
fraction: fractions[i]
|
||||
});
|
||||
@@ -178,6 +179,12 @@ var PhysicsManager = cc.Class({
|
||||
bodies[i].syncPosition();
|
||||
}
|
||||
},
|
||||
syncRotation: function () {
|
||||
var bodies = this._bodies;
|
||||
for (var i = 0; i < bodies.length; i++) {
|
||||
bodies[i].syncRotation();
|
||||
}
|
||||
},
|
||||
|
||||
attachDebugDrawToCamera: function (camera) {
|
||||
if (!this._debugDrawer) return;
|
||||
@@ -242,10 +249,10 @@ var PhysicsManager = cc.Class({
|
||||
if (this._contactListener) return;
|
||||
|
||||
var listener = new cc.PhysicsContactListener();
|
||||
listener.setBeginContact(this._onBeginContact.bind(this));
|
||||
listener.setEndContact(this._onEndContact.bind(this));
|
||||
listener.setPreSolve(this._onPreSolve.bind(this));
|
||||
listener.setPostSolve(this._onPostSolve.bind(this));
|
||||
listener.setBeginContact(this._onBeginContact);
|
||||
listener.setEndContact(this._onEndContact);
|
||||
listener.setPreSolve(this._onPreSolve);
|
||||
listener.setPostSolve(this._onPostSolve);
|
||||
this._world.SetContactListener(listener);
|
||||
|
||||
this._contactListener = listener;
|
||||
@@ -289,27 +296,27 @@ var PhysicsManager = cc.Class({
|
||||
this._debugDrawer.AddDrawerToNode( cc.director.getScene()._sgNode );
|
||||
},
|
||||
|
||||
_onBeginContact: function (contact) {
|
||||
var c = new cc.PhysicsContact(contact);
|
||||
contact._contact = c;
|
||||
_onBeginContact: function (b2contact) {
|
||||
var c = cc.PhysicsContact.get(b2contact);
|
||||
c.emit(ContactType.BEGIN_CONTACT);
|
||||
|
||||
if (c.disabled) {
|
||||
contact.SetEnabled(false);
|
||||
b2contact.SetEnabled(false);
|
||||
}
|
||||
},
|
||||
|
||||
_onEndContact: function (contact) {
|
||||
var c = contact._contact;
|
||||
_onEndContact: function (b2contact) {
|
||||
var c = b2contact._contact;
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
c.emit(ContactType.END_CONTACT);
|
||||
contact._contact = null;
|
||||
|
||||
cc.PhysicsContact.put(b2contact);
|
||||
},
|
||||
|
||||
_onPreSolve: function (contact) {
|
||||
var c = contact._contact;
|
||||
_onPreSolve: function (b2contact) {
|
||||
var c = b2contact._contact;
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
@@ -317,16 +324,20 @@ var PhysicsManager = cc.Class({
|
||||
c.emit(ContactType.PRE_SOLVE);
|
||||
|
||||
if (c.disabled) {
|
||||
contact.SetEnabled(false);
|
||||
b2contact.SetEnabled(false);
|
||||
}
|
||||
},
|
||||
|
||||
_onPostSolve: function (contact, impulse) {
|
||||
var c = contact._contact;
|
||||
_onPostSolve: function (b2contact, impulse) {
|
||||
var c = b2contact._contact;
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
c.emit(ContactType.POST_SOLVE, impulse);
|
||||
|
||||
// impulse only survive during post sole callback
|
||||
c._impulse = impulse;
|
||||
c.emit(ContactType.POST_SOLVE);
|
||||
c._impulse = null;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -356,7 +367,7 @@ cc.js.getset(PhysicsManager.prototype, 'debugDrawFlags',
|
||||
function (value) {
|
||||
if (value && !this._debugDrawFlags) {
|
||||
if (!this._debugDrawer) {
|
||||
this._debugDrawer = new cc.PhysicsDebugDraw(CC_PTM_RATIO);
|
||||
this._debugDrawer = new cc.PhysicsDebugDraw(PTM_RATIO);
|
||||
this._world.SetDebugDraw( this._debugDrawer );
|
||||
}
|
||||
|
||||
@@ -382,22 +393,19 @@ cc.js.getset(PhysicsManager.prototype, 'gravity',
|
||||
function () {
|
||||
if (this._world) {
|
||||
var g = this._world.GetGravity();
|
||||
return cc.v2(g.x*CC_PTM_RATIO, g.y*CC_PTM_RATIO);
|
||||
return cc.v2(g.x*PTM_RATIO, g.y*PTM_RATIO);
|
||||
}
|
||||
return cc.v2();
|
||||
},
|
||||
|
||||
function (value) {
|
||||
if (this._world) {
|
||||
this._world.SetGravity(new b2.Vec2(value.x/CC_PTM_RATIO, value.y/CC_PTM_RATIO));
|
||||
this._world.SetGravity(new b2.Vec2(value.x/PTM_RATIO, value.y/PTM_RATIO));
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
PhysicsManager.DrawBits = b2.Draw;
|
||||
PhysicsManager.CC_PTM_RATIO = CC_PTM_RATIO;
|
||||
PhysicsManager.CC_TO_PHYSICS_ANGLE = CC_TO_PHYSICS_ANGLE;
|
||||
PhysicsManager.PHYSICS_TO_CC_ANGLE = PHYSICS_TO_CC_ANGLE;
|
||||
PhysicsManager.PTM_RATIO = PTM_RATIO;
|
||||
|
||||
cc.PhysicsManager = module.exports = PhysicsManager;
|
||||
|
||||
@@ -24,5 +24,9 @@ cc.RayCastType = RayCastType;
|
||||
module.exports = {
|
||||
BodyType: BodyType,
|
||||
ContactType: ContactType,
|
||||
RayCastType: RayCastType
|
||||
RayCastType: RayCastType,
|
||||
|
||||
PTM_RATIO: 32,
|
||||
ANGLE_TO_PHYSICS_ANGLE: -Math.PI / 180,
|
||||
PHYSICS_ANGLE_TO_ANGLE: -180 / Math.PI,
|
||||
};
|
||||
|
||||
@@ -23,13 +23,18 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var CC_TO_PHYSICS_ANGLE = cc.PhysicsManager.CC_TO_PHYSICS_ANGLE;
|
||||
var PHYSICS_TO_CC_ANGLE = cc.PhysicsManager.PHYSICS_TO_CC_ANGLE;
|
||||
var PTM_RATIO = require('./CCPhysicsTypes').PTM_RATIO;
|
||||
var ANGLE_TO_PHYSICS_ANGLE = require('./CCPhysicsTypes').ANGLE_TO_PHYSICS_ANGLE;
|
||||
var PHYSICS_ANGLE_TO_ANGLE = require('./CCPhysicsTypes').PHYSICS_ANGLE_TO_ANGLE;
|
||||
|
||||
var getWorldRotation = require('./utils').getWorldRotation;
|
||||
var BodyType = require('./CCPhysicsTypes').BodyType;
|
||||
|
||||
var tempb2Vec21 = new b2.Vec2();
|
||||
var tempb2Vec22 = new b2.Vec2();
|
||||
|
||||
var VEC2_ZERO = cc.Vec2.ZERO;
|
||||
|
||||
var RigidBody = cc.Class({
|
||||
name: 'cc.RigidBody',
|
||||
extends: cc.Component,
|
||||
@@ -85,6 +90,9 @@ var RigidBody = cc.Class({
|
||||
|
||||
allowSleep: {
|
||||
get: function () {
|
||||
if (this._b2Body) {
|
||||
return this._b2Body.IsSleepingAllowed();
|
||||
}
|
||||
return this._allowSleep;
|
||||
},
|
||||
set: function (value) {
|
||||
@@ -135,16 +143,21 @@ var RigidBody = cc.Class({
|
||||
linearVelocity: {
|
||||
type: cc.Vec2,
|
||||
get: function () {
|
||||
var lv = this._linearVelocity;
|
||||
if (this._b2Body) {
|
||||
var velocity = this._b2Body.GetLinearVelocity();
|
||||
return cc.v2(velocity.x*CC_PTM_RATIO, velocity.y*CC_PTM_RATIO);
|
||||
lv.x = velocity.x*PTM_RATIO;
|
||||
lv.y = velocity.y*PTM_RATIO;
|
||||
}
|
||||
return this._linearVelocity;
|
||||
return lv;
|
||||
},
|
||||
set: function (value) {
|
||||
this._linearVelocity = value;
|
||||
if (this._b2Body) {
|
||||
this._b2Body.SetLinearVelocity( new b2.Vec2(value.x/CC_PTM_RATIO, value.y/CC_PTM_RATIO) );
|
||||
var b2body = this._b2Body;
|
||||
if (b2body) {
|
||||
var temp = CC_JSB ? tempb2Vec21 : b2body.m_linearVelocity;
|
||||
temp.Set(value.x/PTM_RATIO, value.y/PTM_RATIO);
|
||||
b2body.SetLinearVelocity(temp);
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -152,14 +165,14 @@ var RigidBody = cc.Class({
|
||||
angularVelocity: {
|
||||
get: function () {
|
||||
if (this._b2Body) {
|
||||
return this._b2Body.GetAngularVelocity() * PHYSICS_TO_CC_ANGLE;
|
||||
return this._b2Body.GetAngularVelocity() * PHYSICS_ANGLE_TO_ANGLE;
|
||||
}
|
||||
return this._angularVelocity;
|
||||
},
|
||||
set: function (value) {
|
||||
this._angularVelocity = value;
|
||||
if (this._b2Body) {
|
||||
this._b2Body.SetAngularVelocity( value * CC_TO_PHYSICS_ANGLE );
|
||||
this._b2Body.SetAngularVelocity( value * ANGLE_TO_PHYSICS_ANGLE );
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -207,78 +220,110 @@ var RigidBody = cc.Class({
|
||||
}
|
||||
},
|
||||
|
||||
getLocalPoint: function (worldPoint) {
|
||||
getLocalPoint: function (worldPoint, out) {
|
||||
out = out || cc.v2();
|
||||
if (this._b2Body) {
|
||||
worldPoint = new b2.Vec2(worldPoint.x/CC_PTM_RATIO, worldPoint.y/CC_PTM_RATIO);
|
||||
var pos = this._b2Body.GetLocalPoint(worldPoint);
|
||||
return cc.v2(pos.x*CC_PTM_RATIO, pos.y*CC_PTM_RATIO);
|
||||
tempb2Vec21.Set(worldPoint.x/PTM_RATIO, worldPoint.y/PTM_RATIO);
|
||||
var pos = this._b2Body.GetLocalPoint(tempb2Vec21);
|
||||
out.x = pos.x*PTM_RATIO;
|
||||
out.y = pos.y*PTM_RATIO;
|
||||
}
|
||||
return cc.v2();
|
||||
return out;
|
||||
},
|
||||
|
||||
getWorldPoint: function (localPoint) {
|
||||
getWorldPoint: function (localPoint, out) {
|
||||
out = out || cc.v2();
|
||||
if (this._b2Body) {
|
||||
localPoint = new b2.Vec2(localPoint.x/CC_PTM_RATIO, localPoint.y/CC_PTM_RATIO);
|
||||
var pos = this._b2Body.GetWorldPoint(localPoint);
|
||||
return cc.v2(pos.x*CC_PTM_RATIO, pos.y*CC_PTM_RATIO);
|
||||
tempb2Vec21.Set(localPoint.x/PTM_RATIO, localPoint.y/PTM_RATIO);
|
||||
var pos = this._b2Body.GetWorldPoint(tempb2Vec21);
|
||||
out.x = pos.x*PTM_RATIO;
|
||||
out.y = pos.y*PTM_RATIO;
|
||||
}
|
||||
return cc.v2();
|
||||
return out;
|
||||
},
|
||||
|
||||
getWorldVector: function (localVector) {
|
||||
getWorldVector: function (localVector, out) {
|
||||
out = out || cc.v2();
|
||||
if (this._b2Body) {
|
||||
localVector = new b2.Vec2(localVector.x*CC_PTM_RATIO, localVector.y*CC_PTM_RATIO);
|
||||
var vector = this._b2Body.GetWorldVector(localVector);
|
||||
return cc.v2(vector.x/CC_PTM_RATIO, vector.y/CC_PTM_RATIO);
|
||||
tempb2Vec21.Set(localVector.x/PTM_RATIO, localVector.y/PTM_RATIO);
|
||||
var vector = this._b2Body.GetWorldVector(tempb2Vec21);
|
||||
out.x = vector.x*PTM_RATIO;
|
||||
out.y = vector.y*PTM_RATIO;
|
||||
}
|
||||
return cc.v2();
|
||||
return out;
|
||||
},
|
||||
|
||||
getLocalVector: function (worldVector) {
|
||||
getLocalVector: function (worldVector, out) {
|
||||
out = out || cc.v2();
|
||||
if (this._b2Body) {
|
||||
worldVector = new b2.Vec2(worldVector.x*CC_PTM_RATIO, worldVector.y*CC_PTM_RATIO);
|
||||
var vector = this._b2Body.GetLocalVector(worldVector);
|
||||
return cc.v2(vector.x/CC_PTM_RATIO, vector.y/CC_PTM_RATIO);
|
||||
tempb2Vec21.Set(worldVector.x/PTM_RATIO, worldVector.y/PTM_RATIO);
|
||||
var vector = this._b2Body.GetLocalVector(tempb2Vec21);
|
||||
out.x = vector.x*PTM_RATIO;
|
||||
out.y = vector.y*PTM_RATIO;
|
||||
}
|
||||
return cc.v2();
|
||||
return out;
|
||||
},
|
||||
|
||||
getWorldPosition: function () {
|
||||
getWorldPosition: function (out) {
|
||||
out = out || cc.v2();
|
||||
if (this._b2Body) {
|
||||
var pos = this._b2Body.GetPosition();
|
||||
return cc.v2(pos.x*CC_PTM_RATIO, pos.y*CC_PTM_RATIO);
|
||||
out.x = pos.x*PTM_RATIO;
|
||||
out.y = pos.y*PTM_RATIO;
|
||||
}
|
||||
return cc.v2();
|
||||
return out;
|
||||
},
|
||||
|
||||
getWorldRotation: function () {
|
||||
if (this._b2Body) {
|
||||
return -this._b2Body.GetAngle() * (180 / Math.PI);
|
||||
return this._b2Body.GetAngle() * PHYSICS_ANGLE_TO_ANGLE;
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
|
||||
getLinearVelocityFromWorldPoint: function (p) {
|
||||
getLocalCenter: function (out) {
|
||||
out = out || cc.v2();
|
||||
if (this._b2Body) {
|
||||
p = new b2.Vec2(p.x*CC_PTM_RATIO, p.y*CC_PTM_RATIO);
|
||||
var velocity = this._b2Body.GetLinearVelocityFromWorldPoint(p);
|
||||
return cc.v2(velocity.x/CC_PTM_RATIO, velocity.y/CC_PTM_RATIO);
|
||||
var pos = this._b2Body.GetLocalCenter();
|
||||
out.x = pos.x*PTM_RATIO;
|
||||
out.y = pos.y*PTM_RATIO;
|
||||
}
|
||||
return cc.v2();
|
||||
return out;
|
||||
},
|
||||
|
||||
getWorldCenter: function (out) {
|
||||
out = out || cc.v2();
|
||||
if (this._b2Body) {
|
||||
var pos = this._b2Body.GetWorldCenter();
|
||||
out.x = pos.x*PTM_RATIO;
|
||||
out.y = pos.y*PTM_RATIO;
|
||||
}
|
||||
return out;
|
||||
},
|
||||
|
||||
getLinearVelocityFromWorldPoint: function (worldPoint, out) {
|
||||
out = out || cc.v2();
|
||||
if (this._b2Body) {
|
||||
tempb2Vec21.Set(worldPoint.x/PTM_RATIO, worldPoint.y/PTM_RATIO);
|
||||
var velocity = this._b2Body.GetLinearVelocityFromWorldPoint(tempb2Vec21);
|
||||
out.x = velocity.x*PTM_RATIO;
|
||||
out.y = velocity.y*PTM_RATIO;
|
||||
}
|
||||
return out;
|
||||
},
|
||||
|
||||
applyForce: function (force, point, wake) {
|
||||
if (this._b2Body) {
|
||||
force = new b2.Vec2(force.x/CC_PTM_RATIO, force.y/CC_PTM_RATIO);
|
||||
point = new b2.Vec2(point.x/CC_PTM_RATIO, point.y/CC_PTM_RATIO);
|
||||
this._b2Body.ApplyForce(force, point, wake);
|
||||
tempb2Vec21.Set(force.x/PTM_RATIO, force.y/PTM_RATIO);
|
||||
tempb2Vec22.Set(point.x/PTM_RATIO, point.y/PTM_RATIO);
|
||||
this._b2Body.ApplyForce(tempb2Vec21, tempb2Vec22, wake);
|
||||
}
|
||||
},
|
||||
|
||||
applyForceToCenter: function (force, wake) {
|
||||
if (this._b2Body) {
|
||||
force = new b2.Vec2(force.x/CC_PTM_RATIO, force.y/CC_PTM_RATIO);
|
||||
this._b2Body.ApplyForceToCenter(force, wake);
|
||||
tempb2Vec21.Set(force.x/PTM_RATIO, force.y/PTM_RATIO);
|
||||
this._b2Body.ApplyForceToCenter(tempb2Vec21, wake);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -287,12 +332,31 @@ var RigidBody = cc.Class({
|
||||
this._b2Body.ApplyTorque(torque, wake);
|
||||
}
|
||||
},
|
||||
|
||||
applyLinearImpulse: function (impulse, point, wake) {
|
||||
if (this._b2Body) {
|
||||
tempb2Vec21.Set(impulse.x/PTM_RATIO, impulse.y/PTM_RATIO);
|
||||
tempb2Vec22.Set(point.x/PTM_RATIO, point.y/PTM_RATIO);
|
||||
this._b2Body.ApplyLinearImpulse(tempb2Vec21, tempb2Vec22, wake);
|
||||
}
|
||||
},
|
||||
|
||||
applyAngularImpulse: function (impulse, wake) {
|
||||
if (this._b2Body) {
|
||||
tempb2Vec21.Set(impulse.x/PTM_RATIO, impulse.y/PTM_RATIO);
|
||||
this._b2Body.ApplyAngularImpulse(tempb2Vec21, wake);
|
||||
}
|
||||
},
|
||||
|
||||
resetVelocity: function () {
|
||||
if (!this._b2Body) return;
|
||||
var b2body = this._b2Body;
|
||||
if (!b2body) return;
|
||||
|
||||
this._b2Body.SetLinearVelocity(new b2.Vec2());
|
||||
this._b2Body.SetAngularVelocity(0);
|
||||
var temp = CC_JSB ? tempb2Vec21 : b2body.m_linearVelocity;
|
||||
temp.Set(0, 0);
|
||||
|
||||
b2body.SetLinearVelocity(temp);
|
||||
b2body.SetAngularVelocity(0);
|
||||
},
|
||||
|
||||
onEnable: function () {
|
||||
@@ -309,35 +373,52 @@ var RigidBody = cc.Class({
|
||||
var node = this.node;
|
||||
|
||||
node.on('position-changed', function() {
|
||||
if (!this._ignoreNodeChanges) {
|
||||
var pos = node.convertToWorldSpaceAR(cc.Vec2.ZERO);
|
||||
if (node.scale && this.type === BodyType.Animated) {
|
||||
pos = new b2.Vec2(pos.x / CC_PTM_RATIO, pos.y / CC_PTM_RATIO);
|
||||
var b2Pos = this._b2Body.GetPosition();
|
||||
var b2body = this._b2Body;
|
||||
if (!this._ignoreNodeChanges && b2body) {
|
||||
var pos = node.convertToWorldSpaceAR(VEC2_ZERO);
|
||||
|
||||
pos.x = (pos.x - b2Pos.x)/(1/60);
|
||||
pos.y = (pos.y - b2Pos.y)/(1/60);
|
||||
|
||||
this._b2Body.SetAwake(true);
|
||||
this._b2Body.SetLinearVelocity(pos);
|
||||
var temp;
|
||||
if (CC_JSB) {
|
||||
temp = tempb2Vec21;
|
||||
}
|
||||
else if (this.type === BodyType.Animated) {
|
||||
temp = b2body.GetLinearVelocity();
|
||||
}
|
||||
else {
|
||||
this._b2Body.SetTransform(new b2.Vec2(pos.x / CC_PTM_RATIO, pos.y / CC_PTM_RATIO), this._b2Body.GetAngle());
|
||||
temp = b2body.GetPosition();
|
||||
}
|
||||
|
||||
temp.x = pos.x / PTM_RATIO;
|
||||
temp.y = pos.y / PTM_RATIO;
|
||||
|
||||
if (this.type === BodyType.Animated) {
|
||||
var b2Pos = b2body.GetPosition();
|
||||
|
||||
var timeStep = cc.game.config['frameRate'];
|
||||
temp.x = (temp.x - b2Pos.x)*timeStep;
|
||||
temp.y = (temp.y - b2Pos.y)*timeStep;
|
||||
|
||||
b2body.SetAwake(true);
|
||||
b2body.SetLinearVelocity(temp);
|
||||
}
|
||||
else {
|
||||
b2body.SetTransform(temp, b2body.GetAngle());
|
||||
}
|
||||
}
|
||||
}, this);
|
||||
|
||||
node.on('rotation-changed', function() {
|
||||
if (!this._ignoreNodeChanges) {
|
||||
var rotation = CC_TO_PHYSICS_ANGLE * getWorldRotation(node);
|
||||
if (node.scale && this.type === BodyType.Animated) {
|
||||
var b2Rotation = this._b2Body.GetAngle();
|
||||
|
||||
this._b2Body.SetAwake(true);
|
||||
this._b2Body.SetAngularVelocity((rotation - b2Rotation)/(1/60));
|
||||
var b2body = this._b2Body;
|
||||
if (!this._ignoreNodeChanges && b2body) {
|
||||
var rotation = ANGLE_TO_PHYSICS_ANGLE * getWorldRotation(node);
|
||||
if (this.type === BodyType.Animated) {
|
||||
var b2Rotation = b2body.GetAngle();
|
||||
var timeStep = cc.game.config['frameRate'];
|
||||
b2body.SetAwake(true);
|
||||
b2body.SetAngularVelocity((rotation - b2Rotation)*timeStep);
|
||||
}
|
||||
else {
|
||||
this._b2Body.SetTransform(this._b2Body.GetPosition(), rotation);
|
||||
b2body.SetTransform(b2body.GetPosition(), rotation);
|
||||
}
|
||||
}
|
||||
}, this);
|
||||
@@ -353,8 +434,12 @@ var RigidBody = cc.Class({
|
||||
},
|
||||
|
||||
syncPosition: function () {
|
||||
var pos = this.node.convertToWorldSpaceAR(cc.Vec2.ZERO);
|
||||
this._b2Body.SetTransform(new b2.Vec2(pos.x / CC_PTM_RATIO, pos.y / CC_PTM_RATIO), this._b2Body.GetAngle());
|
||||
var pos = this.node.convertToWorldSpaceAR(VEC2_ZERO);
|
||||
this._b2Body.SetTransform(new b2.Vec2(pos.x / PTM_RATIO, pos.y / PTM_RATIO), this._b2Body.GetAngle());
|
||||
},
|
||||
syncRotation: function () {
|
||||
var rotation = ANGLE_TO_PHYSICS_ANGLE * getWorldRotation(node);
|
||||
b2body.SetTransform(b2body.GetPosition(), rotation);
|
||||
},
|
||||
|
||||
_init: function () {
|
||||
@@ -382,16 +467,16 @@ var RigidBody = cc.Class({
|
||||
bodyDef.angularDamping = this.angularDamping;
|
||||
|
||||
var linearVelocity = this.linearVelocity;
|
||||
bodyDef.linearVelocity = new b2.Vec2(linearVelocity.x/CC_PTM_RATIO, linearVelocity.y/CC_PTM_RATIO);
|
||||
bodyDef.linearVelocity = new b2.Vec2(linearVelocity.x/PTM_RATIO, linearVelocity.y/PTM_RATIO);
|
||||
|
||||
bodyDef.angularVelocity = this.angularVelocity * CC_TO_PHYSICS_ANGLE;
|
||||
bodyDef.angularVelocity = this.angularVelocity * ANGLE_TO_PHYSICS_ANGLE;
|
||||
|
||||
bodyDef.fixedRotation = this.fixedRotation;
|
||||
bodyDef.bullet = this.bullet;
|
||||
|
||||
var node = this.node;
|
||||
var pos = node.convertToWorldSpaceAR(cc.Vec2.ZERO);
|
||||
bodyDef.position = new b2.Vec2(pos.x / CC_PTM_RATIO, pos.y / CC_PTM_RATIO);
|
||||
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);
|
||||
|
||||
cc.director.getPhysicsManager()._addBody(this, bodyDef);
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
|
||||
var PhysicsBoxCollider = cc.Class({
|
||||
name: 'cc.PhysicsBoxCollider',
|
||||
@@ -37,10 +37,10 @@ var PhysicsBoxCollider = cc.Class({
|
||||
properties: cc.PhysicsCollider.properties,
|
||||
|
||||
_createShape: function (scale) {
|
||||
var width = this.size.width/2/CC_PTM_RATIO * scale.x;
|
||||
var height = this.size.height/2/CC_PTM_RATIO * scale.y;
|
||||
var offsetX = this.offset.x/CC_PTM_RATIO *scale.x;
|
||||
var offsetY = this.offset.y/CC_PTM_RATIO *scale.y;
|
||||
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 shape = new b2.PolygonShape();
|
||||
shape.SetAsBox(width, height, new b2.Vec2(offsetX, offsetY), 0);
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
|
||||
var PhysicsChainCollider = cc.Class({
|
||||
name: 'cc.PhysicsChainCollider',
|
||||
@@ -51,17 +51,14 @@ var PhysicsChainCollider = cc.Class({
|
||||
},
|
||||
}, cc.PhysicsCollider.properties),
|
||||
|
||||
_createShape: function (scale, transform) {
|
||||
_createShape: function (scale) {
|
||||
var shape = new b2.ChainShape();
|
||||
|
||||
var points = this.points;
|
||||
var vertices = [];
|
||||
for (var i = 0; i < points.length; i++) {
|
||||
var p = points[i];
|
||||
if (transform) {
|
||||
p = cc.pointApplyAffineTransform(p, transform);
|
||||
}
|
||||
vertices.push( new b2.Vec2(p.x/CC_PTM_RATIO*scale.x, p.y/CC_PTM_RATIO*scale.y) );
|
||||
vertices.push( new b2.Vec2(p.x/PTM_RATIO*scale.x, p.y/PTM_RATIO*scale.y) );
|
||||
}
|
||||
|
||||
if (this.loop) {
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
|
||||
var PhysicsCircleCollider = cc.Class({
|
||||
name: 'cc.PhysicsCircleCollider',
|
||||
@@ -38,7 +38,7 @@ var PhysicsCircleCollider = cc.Class({
|
||||
|
||||
_createShape: function (scale) {
|
||||
var shape = new b2.CircleShape();
|
||||
shape.m_radius = this.radius / CC_PTM_RATIO * scale.x;
|
||||
shape.m_radius = this.radius / PTM_RATIO * scale.x;
|
||||
return shape;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -23,13 +23,16 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
var getWorldScale = require('../utils').getWorldScale;
|
||||
|
||||
function PhysicsCollider () {
|
||||
this._fixtures = [];
|
||||
this._shapes = [];
|
||||
this._inited = false;
|
||||
this._rect = cc.rect();
|
||||
|
||||
this.body = null;
|
||||
}
|
||||
|
||||
PhysicsCollider.prototype.onDisable = function () {
|
||||
@@ -55,12 +58,11 @@ PhysicsCollider.prototype.getAABB = function () {
|
||||
var minX = MAX, minY = MAX;
|
||||
var maxX = -MAX, maxY = -MAX;
|
||||
|
||||
var manager = cc.director.getPhysicsManager();
|
||||
var fixtures = this._fixtures;
|
||||
for (var i = 0; i < fixtures.length; i++) {
|
||||
var fixture = fixtures[i];
|
||||
|
||||
var count = fixture.m_proxyCount;
|
||||
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;
|
||||
@@ -68,16 +70,20 @@ PhysicsCollider.prototype.getAABB = function () {
|
||||
if (aabb.upperBound.x > maxX) maxX = aabb.upperBound.x;
|
||||
if (aabb.upperBound.y > maxY) maxY = aabb.upperBound.y;
|
||||
}
|
||||
|
||||
manager._registerContactFixture(fixture);
|
||||
}
|
||||
|
||||
minX *= CC_PTM_RATIO;
|
||||
minY *= CC_PTM_RATIO;
|
||||
maxX *= CC_PTM_RATIO;
|
||||
maxY *= CC_PTM_RATIO;
|
||||
minX *= PTM_RATIO;
|
||||
minY *= PTM_RATIO;
|
||||
maxX *= PTM_RATIO;
|
||||
maxY *= PTM_RATIO;
|
||||
|
||||
return cc.rect(minX, minY, maxX-minX, maxY-minY);
|
||||
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) {
|
||||
@@ -100,15 +106,10 @@ PhysicsCollider.prototype.__init = function () {
|
||||
var innerBody = body._getBody();
|
||||
if (!innerBody) return;
|
||||
|
||||
var transform;
|
||||
if (body.node !== this.node) {
|
||||
transform = cc.affineTransformConcat( this.node.getNodeToWorldTransformAR(), cc.affineTransformInvert(body.node.getNodeToWorldTransformAR()) );
|
||||
}
|
||||
|
||||
var node = body.node;
|
||||
var scale = getWorldScale(node);
|
||||
|
||||
var shapes = scale.x === 0 && scale.y === 0 ? [] : this._createShape(scale, transform);
|
||||
var shapes = scale.x === 0 && scale.y === 0 ? [] : this._createShape(scale);
|
||||
|
||||
if (!(shapes instanceof Array)) {
|
||||
shapes = [shapes];
|
||||
@@ -162,11 +163,14 @@ PhysicsCollider.prototype.__destroy = function () {
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -188,11 +192,6 @@ PhysicsCollider.properties = {
|
||||
_friction: 0.2,
|
||||
_restitution: 0,
|
||||
|
||||
body: {
|
||||
default: null,
|
||||
type: cc.RigidBody
|
||||
},
|
||||
|
||||
density: {
|
||||
get: function () {
|
||||
return this._density;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
var PolygonSeprator = require('../CCPolygonSeprator');
|
||||
|
||||
var PhysicsPolygonCollider = cc.Class({
|
||||
@@ -38,7 +38,7 @@ var PhysicsPolygonCollider = cc.Class({
|
||||
|
||||
properties: cc.PhysicsCollider.properties,
|
||||
|
||||
_createShape: function (scale, transform) {
|
||||
_createShape: function (scale) {
|
||||
var shapes = [];
|
||||
|
||||
var points = this.points;
|
||||
@@ -56,16 +56,19 @@ var PhysicsPolygonCollider = cc.Class({
|
||||
var poly = polys[i];
|
||||
|
||||
var shape = null, vertices = [];
|
||||
var firstVertice;
|
||||
|
||||
for (var j = 0, l = poly.length; j < l; j++) {
|
||||
if (!shape) {
|
||||
shape = new b2.PolygonShape();
|
||||
}
|
||||
var p = poly[j];
|
||||
if (transform) {
|
||||
p = cc.pointApplyAffineTransform(p, transform);
|
||||
var v = new b2.Vec2(p.x/PTM_RATIO*scale.x, p.y/PTM_RATIO*scale.y);
|
||||
vertices.push( v );
|
||||
|
||||
if (!firstVertice) {
|
||||
firstVertice = v;
|
||||
}
|
||||
vertices.push( new b2.Vec2(p.x/CC_PTM_RATIO*scale.x, p.y/CC_PTM_RATIO*scale.y) );
|
||||
|
||||
if (vertices.length === b2.maxPolygonVertices) {
|
||||
shape.Set(vertices, vertices.length);
|
||||
@@ -74,7 +77,7 @@ var PhysicsPolygonCollider = cc.Class({
|
||||
shape = null;
|
||||
|
||||
if (j < l - 1) {
|
||||
vertices = [vertices[0], vertices[j]];
|
||||
vertices = [firstVertice, vertices[vertices.length - 1]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = cc.PhysicsManager.PTM_RATIO;
|
||||
|
||||
var DistanceJoint = cc.Class({
|
||||
name: 'cc.DistanceJoint',
|
||||
@@ -81,9 +81,9 @@ var DistanceJoint = cc.Class({
|
||||
|
||||
_createJointDef: function () {
|
||||
var def = new b2.DistanceJointDef();
|
||||
def.localAnchorA = new b2.Vec2(this.anchor.x/CC_PTM_RATIO, this.anchor.y/CC_PTM_RATIO);
|
||||
def.localAnchorB = new b2.Vec2(this.connectedAnchor.x/CC_PTM_RATIO, this.connectedAnchor.y/CC_PTM_RATIO);
|
||||
def.length = this.distance/CC_PTM_RATIO;
|
||||
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);
|
||||
def.length = this.distance/PTM_RATIO;
|
||||
def.dampingRatio = this.dampingRatio;
|
||||
def.frequencyHz = this.frequency;
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var CC_TO_PHYSICS_ANGLE = cc.PhysicsManager.CC_TO_PHYSICS_ANGLE;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
var ANGLE_TO_PHYSICS_ANGLE = require('../CCPhysicsTypes').ANGLE_TO_PHYSICS_ANGLE;
|
||||
|
||||
var MotorJoint = cc.Class({
|
||||
name: 'cc.MotorJoint',
|
||||
@@ -49,7 +49,7 @@ var MotorJoint = cc.Class({
|
||||
set: function (value) {
|
||||
this._linearOffset = value;
|
||||
if (this._joint) {
|
||||
this._joint.SetLinearOffset( new b2.Vec2(value.x/CC_PTM_RATIO, value.y/CC_PTM_RATIO) );
|
||||
this._joint.SetLinearOffset( new b2.Vec2(value.x/PTM_RATIO, value.y/PTM_RATIO) );
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -105,8 +105,8 @@ var MotorJoint = cc.Class({
|
||||
|
||||
_createJointDef: function () {
|
||||
var def = new b2.MotorJointDef();
|
||||
def.linearOffset = new b2.Vec2(this.linearOffset.x/CC_PTM_RATIO, this.linearOffset.y/CC_PTM_RATIO);
|
||||
def.angularOffset = this.angularOffset * CC_TO_PHYSICS_ANGLE;
|
||||
def.linearOffset = new b2.Vec2(this.linearOffset.x/PTM_RATIO, this.linearOffset.y/PTM_RATIO);
|
||||
def.angularOffset = this.angularOffset * ANGLE_TO_PHYSICS_ANGLE;
|
||||
def.maxForce = this.maxForce;
|
||||
def.maxTorque = this.maxTorque;
|
||||
def.correctionFactor = this.correctionFactor;
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
|
||||
var MouseJoint = cc.Class({
|
||||
name: 'cc.MouseJoint',
|
||||
extends: cc.Joint,
|
||||
@@ -74,7 +75,7 @@ var MouseJoint = cc.Class({
|
||||
set: function (value) {
|
||||
this._target = value;
|
||||
if (this._joint) {
|
||||
this._joint.SetTarget(new b2.Vec2(value.x/CC_PTM_RATIO, value.y/CC_PTM_RATIO));
|
||||
this._joint.SetTarget(new b2.Vec2(value.x/PTM_RATIO, value.y/PTM_RATIO));
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -155,7 +156,7 @@ var MouseJoint = cc.Class({
|
||||
|
||||
_createJointDef: function () {
|
||||
var def = new b2.MouseJointDef();
|
||||
def.target = new b2.Vec2(this.target.x/CC_PTM_RATIO, this.target.y/CC_PTM_RATIO);
|
||||
def.target = new b2.Vec2(this.target.x/PTM_RATIO, this.target.y/PTM_RATIO);
|
||||
def.maxForce = this.maxForce;
|
||||
def.dampingRatio = this.dampingRatio;
|
||||
def.frequencyHz = this.frequency;
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var CC_TO_PHYSICS_ANGLE = cc.PhysicsManager.CC_TO_PHYSICS_ANGLE;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
var ANGLE_TO_PHYSICS_ANGLE = require('../CCPhysicsTypes').ANGLE_TO_PHYSICS_ANGLE;
|
||||
|
||||
var PrismaticJoint = cc.Class({
|
||||
name: 'cc.PrismaticJoint',
|
||||
@@ -76,13 +76,13 @@ var PrismaticJoint = cc.Class({
|
||||
|
||||
_createJointDef: function () {
|
||||
var def = new b2.PrismaticJointDef();
|
||||
def.localAnchorA = new b2.Vec2(this.anchor.x/CC_PTM_RATIO, this.anchor.y/CC_PTM_RATIO);
|
||||
def.localAnchorB = new b2.Vec2(this.connectedAnchor.x/CC_PTM_RATIO, this.connectedAnchor.y/CC_PTM_RATIO);
|
||||
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);
|
||||
def.localAxisA = new b2.Vec2(this.localAxisA.x, this.localAxisA.y);
|
||||
def.referenceAngle = this.referenceAngle * CC_TO_PHYSICS_ANGLE;
|
||||
def.referenceAngle = this.referenceAngle * ANGLE_TO_PHYSICS_ANGLE;
|
||||
def.enableLimit = this.enableLimit;
|
||||
def.lowerTranslation = this.lowerTranslation/CC_PTM_RATIO;
|
||||
def.upperTranslation = this.upperTranslation/CC_PTM_RATIO;
|
||||
def.lowerTranslation = this.lowerTranslation/PTM_RATIO;
|
||||
def.upperTranslation = this.upperTranslation/PTM_RATIO;
|
||||
def.enableMotor = this.enableMotor;
|
||||
def.maxMotorForce = this.maxMotorForce;
|
||||
def.motorSpeed = this.motorSpeed;
|
||||
|
||||
@@ -23,9 +23,9 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var CC_TO_PHYSICS_ANGLE = cc.PhysicsManager.CC_TO_PHYSICS_ANGLE;
|
||||
var PHYSICS_TO_CC_ANGLE = cc.PhysicsManager.PHYSICS_TO_CC_ANGLE;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
var ANGLE_TO_PHYSICS_ANGLE = require('../CCPhysicsTypes').ANGLE_TO_PHYSICS_ANGLE;
|
||||
var PHYSICS_ANGLE_TO_ANGLE = require('../CCPhysicsTypes').PHYSICS_ANGLE_TO_ANGLE;
|
||||
|
||||
var RevoluteJoint = cc.Class({
|
||||
name: 'cc.RevoluteJoint',
|
||||
@@ -68,7 +68,7 @@ var RevoluteJoint = cc.Class({
|
||||
set: function (value) {
|
||||
this._motorSpeed = value;
|
||||
if (this._joint) {
|
||||
this._joint.SetMotorSpeed(value * CC_TO_PHYSICS_ANGLE);
|
||||
this._joint.SetMotorSpeed(value * ANGLE_TO_PHYSICS_ANGLE);
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -101,7 +101,7 @@ var RevoluteJoint = cc.Class({
|
||||
visible: false,
|
||||
get: function () {
|
||||
if (this._joint) {
|
||||
return this._joint.GetJointAngle() * PHYSICS_TO_CC_ANGLE;
|
||||
return this._joint.GetJointAngle() * PHYSICS_ANGLE_TO_ANGLE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -110,19 +110,19 @@ var RevoluteJoint = cc.Class({
|
||||
|
||||
_createJointDef: function () {
|
||||
var def = new b2.RevoluteJointDef();
|
||||
def.localAnchorA = new b2.Vec2(this.anchor.x/CC_PTM_RATIO, this.anchor.y/CC_PTM_RATIO);
|
||||
def.localAnchorB = new b2.Vec2(this.connectedAnchor.x/CC_PTM_RATIO, this.connectedAnchor.y/CC_PTM_RATIO);
|
||||
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) * CC_TO_PHYSICS_ANGLE;
|
||||
def.upperAngle = (this.lowerAngle + 90) * CC_TO_PHYSICS_ANGLE;
|
||||
def.lowerAngle = (this.upperAngle + 90) * ANGLE_TO_PHYSICS_ANGLE;
|
||||
def.upperAngle = (this.lowerAngle + 90) * ANGLE_TO_PHYSICS_ANGLE;
|
||||
|
||||
def.maxMotorTorque = this.maxMotorTorque;
|
||||
def.motorSpeed = this.motorSpeed * CC_TO_PHYSICS_ANGLE;
|
||||
def.motorSpeed = this.motorSpeed * ANGLE_TO_PHYSICS_ANGLE;
|
||||
def.enableLimit = this.enableLimit;
|
||||
def.enableMotor = this.enableMotor;
|
||||
|
||||
def.referenceAngle = this.referenceAngle * CC_TO_PHYSICS_ANGLE;
|
||||
def.referenceAngle = this.referenceAngle * ANGLE_TO_PHYSICS_ANGLE;
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
|
||||
var RopeJoint = cc.Class({
|
||||
name: 'cc.RopeJoint',
|
||||
extends: cc.Joint,
|
||||
@@ -55,9 +56,9 @@ var RopeJoint = cc.Class({
|
||||
|
||||
_createJointDef: function () {
|
||||
var def = new b2.RopeJointDef();
|
||||
def.localAnchorA = new b2.Vec2(this.anchor.x/CC_PTM_RATIO, this.anchor.y/CC_PTM_RATIO);
|
||||
def.localAnchorB = new b2.Vec2(this.connectedAnchor.x/CC_PTM_RATIO, this.connectedAnchor.y/CC_PTM_RATIO);
|
||||
def.maxLength = this.maxLength/CC_PTM_RATIO;
|
||||
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);
|
||||
def.maxLength = this.maxLength/PTM_RATIO;
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var CC_TO_PHYSICS_ANGLE = cc.PhysicsManager.CC_TO_PHYSICS_ANGLE;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
var ANGLE_TO_PHYSICS_ANGLE = require('../CCPhysicsTypes').ANGLE_TO_PHYSICS_ANGLE;
|
||||
|
||||
var WeldJoint = cc.Class({
|
||||
name: 'cc.WeldJoint',
|
||||
@@ -70,9 +70,9 @@ var WeldJoint = cc.Class({
|
||||
|
||||
_createJointDef: function () {
|
||||
var def = new b2.WeldJointDef();
|
||||
def.localAnchorA = new b2.Vec2(this.anchor.x/CC_PTM_RATIO, this.anchor.y/CC_PTM_RATIO);
|
||||
def.localAnchorB = new b2.Vec2(this.connectedAnchor.x/CC_PTM_RATIO, this.connectedAnchor.y/CC_PTM_RATIO);
|
||||
def.referenceAngle = this.referenceAngle * CC_TO_PHYSICS_ANGLE;
|
||||
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);
|
||||
def.referenceAngle = this.referenceAngle * ANGLE_TO_PHYSICS_ANGLE;
|
||||
|
||||
def.frequencyHz = this.frequency;
|
||||
def.dampingRatio = this.dampingRatio;
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var CC_TO_PHYSICS_ANGLE = cc.PhysicsManager.CC_TO_PHYSICS_ANGLE;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
var ANGLE_TO_PHYSICS_ANGLE = require('../CCPhysicsTypes').ANGLE_TO_PHYSICS_ANGLE;
|
||||
|
||||
var WheelJoint = cc.Class({
|
||||
name: 'cc.WheelJoint',
|
||||
@@ -67,7 +67,7 @@ var WheelJoint = cc.Class({
|
||||
set: function (value) {
|
||||
this._motorSpeed = value;
|
||||
if (this._joint) {
|
||||
this._joint.SetMotorSpeed(value * CC_TO_PHYSICS_ANGLE);
|
||||
this._joint.SetMotorSpeed(value * ANGLE_TO_PHYSICS_ANGLE);
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -111,13 +111,13 @@ var WheelJoint = cc.Class({
|
||||
|
||||
_createJointDef: function () {
|
||||
var def = new b2.WheelJointDef();
|
||||
def.localAnchorA = new b2.Vec2(this.anchor.x/CC_PTM_RATIO, this.anchor.y/CC_PTM_RATIO);
|
||||
def.localAnchorB = new b2.Vec2(this.connectedAnchor.x/CC_PTM_RATIO, this.connectedAnchor.y/CC_PTM_RATIO);
|
||||
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);
|
||||
|
||||
def.localAxisA = new b2.Vec2(this.localAxisA.x, this.localAxisA.y);
|
||||
|
||||
def.maxMotorTorque = this.maxMotorTorque;
|
||||
def.motorSpeed = this.motorSpeed * CC_TO_PHYSICS_ANGLE;
|
||||
def.motorSpeed = this.motorSpeed * ANGLE_TO_PHYSICS_ANGLE;
|
||||
def.enableMotor = this.enableMotor;
|
||||
|
||||
def.dampingRatio = this.dampingRatio;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
|
||||
var PhysicsDebugDraw = cc.Class({
|
||||
name: 'cc.PhysicsDebugDraw',
|
||||
@@ -27,9 +27,9 @@ var PhysicsDebugDraw = cc.Class({
|
||||
|
||||
for (var i=0; i<vertexCount; i++) {
|
||||
if (i === 0)
|
||||
drawer.moveTo(vertices[i].x * CC_PTM_RATIO, vertices[i].y * CC_PTM_RATIO);
|
||||
drawer.moveTo(vertices[i].x * PTM_RATIO, vertices[i].y * PTM_RATIO);
|
||||
else {
|
||||
drawer.lineTo(vertices[i].x * CC_PTM_RATIO, vertices[i].y * CC_PTM_RATIO);
|
||||
drawer.lineTo(vertices[i].x * PTM_RATIO, vertices[i].y * PTM_RATIO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ var PhysicsDebugDraw = cc.Class({
|
||||
},
|
||||
|
||||
_DrawCircle: function (center, radius) {
|
||||
this._drawer.circle(center.x * CC_PTM_RATIO, center.y * CC_PTM_RATIO, radius * CC_PTM_RATIO);
|
||||
this._drawer.circle(center.x * PTM_RATIO, center.y * PTM_RATIO, radius * PTM_RATIO);
|
||||
},
|
||||
|
||||
DrawCircle: function (center, radius, color) {
|
||||
@@ -69,13 +69,13 @@ var PhysicsDebugDraw = cc.Class({
|
||||
|
||||
if (p1.x === p2.x && p1.y === p2.y) {
|
||||
this._applyFillColor(color);
|
||||
this._DrawCircle(p1, 2/CC_PTM_RATIO);
|
||||
this._DrawCircle(p1, 2/PTM_RATIO);
|
||||
drawer.fill();
|
||||
return;
|
||||
}
|
||||
this._applyStrokeColor(color);
|
||||
drawer.moveTo(p1.x * CC_PTM_RATIO, p1.y * CC_PTM_RATIO);
|
||||
drawer.lineTo(p2.x * CC_PTM_RATIO, p2.y * CC_PTM_RATIO);
|
||||
drawer.moveTo(p1.x * PTM_RATIO, p1.y * PTM_RATIO);
|
||||
drawer.lineTo(p2.x * PTM_RATIO, p2.y * PTM_RATIO);
|
||||
drawer.stroke();
|
||||
},
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
|
||||
var PHYSICS_TO_CC_ANGLE = cc.PhysicsManager.PHYSICS_TO_CC_ANGLE;
|
||||
var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO;
|
||||
var PHYSICS_ANGLE_TO_ANGLE = require('../CCPhysicsTypes').PHYSICS_ANGLE_TO_ANGLE;
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
|
||||
var convertToNodeRotation = require('../utils').convertToNodeRotation;
|
||||
|
||||
var tempPosition = cc.v2();
|
||||
|
||||
function PhysicsUtils () {
|
||||
}
|
||||
|
||||
@@ -21,20 +23,21 @@ PhysicsUtils.prototype.syncNode = function () {
|
||||
var b2body = body._b2Body;
|
||||
var pos = b2body.GetPosition();
|
||||
|
||||
var position = cc.v2( pos.x * CC_PTM_RATIO, pos.y * CC_PTM_RATIO);
|
||||
var angle = b2body.GetAngle() * PHYSICS_TO_CC_ANGLE;
|
||||
tempPosition.x = pos.x * PTM_RATIO;
|
||||
tempPosition.y = pos.y * PTM_RATIO;
|
||||
|
||||
var angle = b2body.GetAngle() * PHYSICS_ANGLE_TO_ANGLE;
|
||||
|
||||
body._ignoreNodeChanges = true;
|
||||
|
||||
if (node.parent.parent !== null) {
|
||||
node.position = node.parent.convertToNodeSpaceAR( position );
|
||||
node.rotation = convertToNodeRotation( node.parent, angle );
|
||||
}
|
||||
else {
|
||||
node.position = position;
|
||||
node.rotation = angle;
|
||||
tempPosition = node.parent.convertToNodeSpaceAR( tempPosition );
|
||||
angle = convertToNodeRotation( node.parent, angle );
|
||||
}
|
||||
|
||||
node.position = tempPosition;
|
||||
node.rotation = angle;
|
||||
|
||||
body._ignoreNodeChanges = false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -408,6 +408,12 @@ b2.WheelJointDef = function () {
|
||||
this.dampingRatio = 0.7;
|
||||
};
|
||||
|
||||
b2.WorldManifold = function ()
|
||||
{
|
||||
this.normal = new b2.Vec2(); ///< world vector pointing from A to B
|
||||
this.points = []; ///< world contact point (point of intersection)
|
||||
this.separations = []; ///< a negative value indicates overlap, in meters
|
||||
}
|
||||
|
||||
// b2Shape
|
||||
var _p = b2.Shape.prototype;
|
||||
@@ -446,5 +452,6 @@ b2.Joint.e_atUpperLimit = 2;
|
||||
b2.Joint.e_equalLimits = 3;
|
||||
|
||||
b2.maxPolygonVertices = 8;
|
||||
b2.maxManifoldPoints = 2;
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user