mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
@@ -130,6 +130,8 @@ let Camera = cc.Class({
|
||||
},
|
||||
|
||||
onEnable () {
|
||||
if (CC_EDITOR) return;
|
||||
|
||||
if (!Camera.main) {
|
||||
Camera.main = this;
|
||||
}
|
||||
@@ -144,6 +146,8 @@ let Camera = cc.Class({
|
||||
},
|
||||
|
||||
onDisable () {
|
||||
if (CC_EDITOR) return;
|
||||
|
||||
if (Camera.main === this) {
|
||||
Camera.main = null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
const PhysicsTypes = require('./CCPhysicsTypes');
|
||||
const BodyType = PhysicsTypes.BodyType;
|
||||
const DrawBits = PhysicsTypes.DrawBits;
|
||||
const PTM_RATIO = PhysicsTypes.PTM_RATIO;
|
||||
|
||||
const math = cc.vmath;
|
||||
const mat4 = math.mat4;
|
||||
const vec2 = math.vec2;
|
||||
|
||||
const ShapeInactiveColor = cc.color(127, 127, 76, 150);
|
||||
const ShapeStaticColor = cc.color(127, 229, 127, 150);
|
||||
const ShapeKinematicColor = cc.color(127, 127, 229, 150);
|
||||
const ShapeInawakeColor = cc.color(153, 153, 153, 150);
|
||||
const ShapeNormalColor = cc.color(229, 178, 178, 150);
|
||||
const JointColor = cc.color(127, 204, 204, 150);
|
||||
const AabbColor = cc.color(229, 77, 229, 255);
|
||||
|
||||
let _vec2_tmp1 = cc.v2();
|
||||
let _vec2_tmp2 = cc.v2();
|
||||
let _mat4_tmp = mat4.create();
|
||||
let _rect_tmp = cc.rect();
|
||||
|
||||
function drawPoints (g, points, offset, color, matrix) {
|
||||
for (let i = 0, l = points.length; i < l; i++) {
|
||||
_vec2_tmp1.x = points[i].x + offset.x;
|
||||
_vec2_tmp1.y = points[i].y + offset.y;
|
||||
|
||||
vec2.transformMat4(_vec2_tmp1, _vec2_tmp1, matrix);
|
||||
if (i === 0) {
|
||||
g.moveTo(_vec2_tmp1.x, _vec2_tmp1. y);
|
||||
}
|
||||
else {
|
||||
g.lineTo(_vec2_tmp1.x, _vec2_tmp1. y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawCollider (g, collider, color, matrix) {
|
||||
if (!collider.body) return;
|
||||
|
||||
if (collider instanceof cc.PhysicsBoxCollider) {
|
||||
let offset = collider.offset;
|
||||
let size = collider.size;
|
||||
let points = [
|
||||
cc.v2(- size.width/2, - size.height/2),
|
||||
cc.v2(+ size.width/2, - size.height/2),
|
||||
cc.v2(+ size.width/2, + size.height/2),
|
||||
cc.v2(- size.width/2, + size.height/2),
|
||||
];
|
||||
|
||||
drawPoints(g, points, collider.offset, color, matrix);
|
||||
g.close();
|
||||
}
|
||||
else if (collider instanceof cc.PhysicsPolygonCollider) {
|
||||
drawPoints(g, collider.points, collider.offset, color, matrix);
|
||||
g.close();
|
||||
}
|
||||
else if (collider instanceof cc.PhysicsChainCollider) {
|
||||
drawPoints(g, collider.points, color, matrix);
|
||||
}
|
||||
else if (collider instanceof cc.PhysicsCircleCollider) {
|
||||
vec2.transformMat4(_vec2_tmp1, collider.offset, matrix);
|
||||
g.circle(_vec2_tmp1.x, _vec2_tmp1.y, collider.radius * collider._scale.x);
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
|
||||
g.fillColor = color;
|
||||
g.fill();
|
||||
g.stroke();
|
||||
}
|
||||
|
||||
function drawSegment (g, p1, p2, color) {
|
||||
if (p1.x === p2.x && p1.y === p2.y) {
|
||||
g.fillColor = color;
|
||||
g.circle(p1.x, p1.y, 1);
|
||||
g.fill();
|
||||
return;
|
||||
}
|
||||
g.strokeColor = color;
|
||||
g.moveTo(p1.x, p1.y);
|
||||
g.lineTo(p2.x, p2.y);
|
||||
g.stroke();
|
||||
}
|
||||
|
||||
module.exports = function (manager) {
|
||||
let flags = manager.debugDrawFlags;
|
||||
if (!flags) {
|
||||
return;
|
||||
}
|
||||
|
||||
manager._checkDebugDrawValid();
|
||||
let debugDrawer = manager._debugDrawer;
|
||||
debugDrawer.clear();
|
||||
|
||||
if (flags & DrawBits.e_shapeBit || flags & DrawBits.e_aabbBit) {
|
||||
let bodies = manager._bodies;
|
||||
for (let i = 0, l = bodies.length; i < l; i++) {
|
||||
let body = bodies[i];
|
||||
let colliders = body.node.getComponents(cc.PhysicsCollider);
|
||||
|
||||
if (colliders.length === 0) continue;
|
||||
|
||||
body.node.getWorldMatrix(_mat4_tmp);
|
||||
|
||||
if (flags & DrawBits.e_shapeBit) {
|
||||
let color = ShapeNormalColor;
|
||||
if (!body.active) {
|
||||
color = ShapeInactiveColor;
|
||||
}
|
||||
else if (body.type == BodyType.Static) {
|
||||
color = ShapeStaticColor;
|
||||
}
|
||||
else if (body.type == BodyType.Kinematic) {
|
||||
color = ShapeKinematicColor;
|
||||
}
|
||||
else if (!body.awake) {
|
||||
color = ShapeInawakeColor;
|
||||
}
|
||||
|
||||
for (let i2 = 0, l2 = colliders.length; i2 < l2; i2++) {
|
||||
drawCollider(debugDrawer, colliders[i2], color, _mat4_tmp);
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & DrawBits.e_aabbBit && body.active) {
|
||||
for (let i2 = 0, l2 = colliders.length; i2 < l2; i2++) {
|
||||
let aabb = colliders[i2].getAABB();
|
||||
debugDrawer.strokeColor = AabbColor;
|
||||
debugDrawer.rect(aabb.x, aabb.y, aabb.width, aabb.height);
|
||||
debugDrawer.stroke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & DrawBits.e_jointBit) {
|
||||
let joints = manager._joints;
|
||||
for (let i = 0, l = joints.length; i < l; i++) {
|
||||
let joint = joints[i];
|
||||
let bodyA = joint.body;
|
||||
let bodyB = joint.connectedBody;
|
||||
|
||||
if (!bodyA || !bodyB) continue;
|
||||
|
||||
bodyA.node.getWorldMatrix(_mat4_tmp);
|
||||
_vec2_tmp1.x = _mat4_tmp.m12;
|
||||
_vec2_tmp1.y = _mat4_tmp.m13;
|
||||
|
||||
bodyB.node.getWorldMatrix(_mat4_tmp);
|
||||
_vec2_tmp2.x = _mat4_tmp.m12;
|
||||
_vec2_tmp2.y = _mat4_tmp.m13;
|
||||
|
||||
let p1 = cc.v2(joint._joint.GetAnchorA()).mulSelf(PTM_RATIO);
|
||||
let p2 = cc.v2(joint._joint.GetAnchorB()).mulSelf(PTM_RATIO);
|
||||
|
||||
if (joint instanceof cc.DistanceJoint) {
|
||||
drawSegment(debugDrawer, p1, p2, JointColor);
|
||||
}
|
||||
else {
|
||||
drawSegment(debugDrawer, _vec2_tmp1, p1, JointColor);
|
||||
drawSegment(debugDrawer, p1, p2, JointColor);
|
||||
drawSegment(debugDrawer, _vec2_tmp2, p2, JointColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
@@ -23,17 +23,26 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var ContactType = require('./CCPhysicsTypes').ContactType;
|
||||
var BodyType = require('./CCPhysicsTypes').BodyType;
|
||||
var RayCastType = require('./CCPhysicsTypes').RayCastType;
|
||||
const PhysicsTypes = require('./CCPhysicsTypes');
|
||||
const ContactType = PhysicsTypes.ContactType;
|
||||
const BodyType = PhysicsTypes.BodyType;
|
||||
const RayCastType = PhysicsTypes.RayCastType;
|
||||
const DrawBits = PhysicsTypes.DrawBits;
|
||||
|
||||
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;
|
||||
const PTM_RATIO = PhysicsTypes.PTM_RATIO;
|
||||
const ANGLE_TO_PHYSICS_ANGLE = PhysicsTypes.ANGLE_TO_PHYSICS_ANGLE;
|
||||
const PHYSICS_ANGLE_TO_ANGLE = PhysicsTypes.PHYSICS_ANGLE_TO_ANGLE;
|
||||
|
||||
var tempB2AABB = new b2.AABB();
|
||||
var tempB2Vec21 = new b2.Vec2();
|
||||
var tempB2Vec22 = new b2.Vec2();
|
||||
const debugDraw = require('./CCPhysicsDebugDraw');
|
||||
const convertToNodeRotation = require('./utils').convertToNodeRotation;
|
||||
|
||||
var b2_aabb_tmp = new b2.AABB();
|
||||
var b2_vec2_tmp1 = new b2.Vec2();
|
||||
var b2_vec2_tmp2 = new b2.Vec2();
|
||||
|
||||
var vec2_tmp = cc.v2();
|
||||
|
||||
let array_tmp = [];
|
||||
|
||||
var FIXED_TIME_STEP = 1/60;
|
||||
var MAX_ACCUMULATOR = 1/5;
|
||||
@@ -72,7 +81,7 @@ var PhysicsManager = cc.Class({
|
||||
cc.PhysicsManager.DrawBits.e_jointBit |
|
||||
cc.PhysicsManager.DrawBits.e_shapeBit;
|
||||
*/
|
||||
DrawBits: b2.Draw,
|
||||
DrawBits: DrawBits,
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -116,6 +125,7 @@ var PhysicsManager = cc.Class({
|
||||
this._world = null;
|
||||
|
||||
this._bodies = [];
|
||||
this._joints = [];
|
||||
|
||||
this._contactMap = {};
|
||||
this._contactID = 0;
|
||||
@@ -135,8 +145,7 @@ var PhysicsManager = cc.Class({
|
||||
* @property {Boolean} enabledAccumulator
|
||||
* @default false
|
||||
*/
|
||||
this.enabledAccumulator = false;
|
||||
|
||||
this.enabledAccumulator = false;
|
||||
},
|
||||
|
||||
pushDelayEvent: function (target, func, args) {
|
||||
@@ -180,9 +189,6 @@ var PhysicsManager = cc.Class({
|
||||
var timeStep = 1/cc.game.config['frameRate'];
|
||||
world.Step(timeStep, velocityIterations, positionIterations);
|
||||
}
|
||||
|
||||
|
||||
world.DrawDebugData();
|
||||
|
||||
this._steping = false;
|
||||
|
||||
@@ -194,6 +200,8 @@ var PhysicsManager = cc.Class({
|
||||
events.length = 0;
|
||||
|
||||
this._syncNode();
|
||||
|
||||
debugDraw(this);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -206,18 +214,18 @@ var PhysicsManager = cc.Class({
|
||||
* @return {PhysicsCollider}
|
||||
*/
|
||||
testPoint: function (point) {
|
||||
var x = tempB2Vec21.x = point.x/PTM_RATIO;
|
||||
var y = tempB2Vec21.y = point.y/PTM_RATIO;
|
||||
var x = b2_vec2_tmp1.x = point.x/PTM_RATIO;
|
||||
var y = b2_vec2_tmp1.y = point.y/PTM_RATIO;
|
||||
|
||||
var d = 0.2/PTM_RATIO;
|
||||
tempB2AABB.lowerBound.x = x-d;
|
||||
tempB2AABB.lowerBound.y = y-d;
|
||||
tempB2AABB.upperBound.x = x+d;
|
||||
tempB2AABB.upperBound.y = y+d;
|
||||
b2_aabb_tmp.lowerBound.x = x-d;
|
||||
b2_aabb_tmp.lowerBound.y = y-d;
|
||||
b2_aabb_tmp.upperBound.x = x+d;
|
||||
b2_aabb_tmp.upperBound.y = y+d;
|
||||
|
||||
var callback = this._aabbQueryCallback;
|
||||
callback.init(tempB2Vec21);
|
||||
this._world.QueryAABB(callback, tempB2AABB);
|
||||
callback.init(b2_vec2_tmp1);
|
||||
this._world.QueryAABB(callback, b2_aabb_tmp);
|
||||
|
||||
var fixture = callback.getFixture();
|
||||
if (fixture) {
|
||||
@@ -237,14 +245,14 @@ var PhysicsManager = cc.Class({
|
||||
* @return {[PhysicsCollider]}
|
||||
*/
|
||||
testAABB: function (rect) {
|
||||
tempB2AABB.lowerBound.x = rect.xMin/PTM_RATIO;
|
||||
tempB2AABB.lowerBound.y = rect.yMin/PTM_RATIO;
|
||||
tempB2AABB.upperBound.x = rect.xMax/PTM_RATIO;
|
||||
tempB2AABB.upperBound.y = rect.yMax/PTM_RATIO;
|
||||
b2_aabb_tmp.lowerBound.x = rect.xMin/PTM_RATIO;
|
||||
b2_aabb_tmp.lowerBound.y = rect.yMin/PTM_RATIO;
|
||||
b2_aabb_tmp.upperBound.x = rect.xMax/PTM_RATIO;
|
||||
b2_aabb_tmp.upperBound.y = rect.yMax/PTM_RATIO;
|
||||
|
||||
var callback = this._aabbQueryCallback;
|
||||
callback.init();
|
||||
this._world.QueryAABB(callback, tempB2AABB);
|
||||
this._world.QueryAABB(callback, b2_aabb_tmp);
|
||||
|
||||
var fixtures = callback.getFixtures();
|
||||
var colliders = fixtures.map(function (fixture) {
|
||||
@@ -273,14 +281,14 @@ var PhysicsManager = cc.Class({
|
||||
|
||||
type = type || RayCastType.Closest;
|
||||
|
||||
tempB2Vec21.x = p1.x/PTM_RATIO;
|
||||
tempB2Vec21.y = p1.y/PTM_RATIO;
|
||||
tempB2Vec22.x = p2.x/PTM_RATIO;
|
||||
tempB2Vec22.y = p2.y/PTM_RATIO;
|
||||
b2_vec2_tmp1.x = p1.x/PTM_RATIO;
|
||||
b2_vec2_tmp1.y = p1.y/PTM_RATIO;
|
||||
b2_vec2_tmp2.x = p2.x/PTM_RATIO;
|
||||
b2_vec2_tmp2.y = p2.y/PTM_RATIO;
|
||||
|
||||
var callback = this._raycastQueryCallback;
|
||||
callback.init(type);
|
||||
this._world.RayCast(callback, tempB2Vec21, tempB2Vec22);
|
||||
this._world.RayCast(callback, b2_vec2_tmp1, b2_vec2_tmp2);
|
||||
|
||||
var fixtures = callback.getFixtures();
|
||||
if (fixtures.length > 0) {
|
||||
@@ -348,8 +356,9 @@ var PhysicsManager = cc.Class({
|
||||
* @param {Camera} camera
|
||||
*/
|
||||
attachDebugDrawToCamera: function (camera) {
|
||||
if (!this._debugDrawer) return;
|
||||
camera.addTarget(this._debugDrawer.getDrawer());
|
||||
if (!this.debugDrawFlags) return;
|
||||
this._checkDebugDrawValid();
|
||||
camera.addTarget(this._debugDrawer.node);
|
||||
},
|
||||
/**
|
||||
* !#en
|
||||
@@ -360,8 +369,9 @@ var PhysicsManager = cc.Class({
|
||||
* @param {Camera} camera
|
||||
*/
|
||||
detachDebugDrawFromCamera: function (camera) {
|
||||
if (!this._debugDrawer) return;
|
||||
camera.removeTarget(this._debugDrawer.getDrawer());
|
||||
if (!this.debugDrawFlags) return;
|
||||
this._checkDebugDrawValid();
|
||||
camera.removeTarget(this._debugDrawer.node);
|
||||
},
|
||||
|
||||
_registerContactFixture: function (fixture) {
|
||||
@@ -379,14 +389,8 @@ var PhysicsManager = cc.Class({
|
||||
if (!world || !node) return;
|
||||
|
||||
body._b2Body = world.CreateBody(bodyDef);
|
||||
|
||||
if (CC_JSB) {
|
||||
body._b2Body.SetUserData( node._sgNode );
|
||||
}
|
||||
|
||||
body._b2Body.body = body;
|
||||
|
||||
this._utils.addB2Body(body._b2Body);
|
||||
this._bodies.push(body);
|
||||
},
|
||||
|
||||
@@ -394,19 +398,33 @@ var PhysicsManager = cc.Class({
|
||||
var world = this._world;
|
||||
if (!world) return;
|
||||
|
||||
if (CC_JSB) {
|
||||
body._b2Body.SetUserData(null);
|
||||
}
|
||||
body._b2Body.body = null;
|
||||
this._utils.removeB2Body(body._b2Body);
|
||||
|
||||
world.DestroyBody(body._b2Body);
|
||||
body._b2Body = null;
|
||||
|
||||
var index = this._bodies.indexOf(body);
|
||||
if (index !== -1) {
|
||||
this._bodies.splice(index, 1);
|
||||
cc.js.array.remove(this._bodies, body);
|
||||
},
|
||||
|
||||
_addJoint (joint, jointDef) {
|
||||
let b2joint = this._world.CreateJoint(jointDef);
|
||||
if (!b2joint) return;
|
||||
|
||||
b2joint._joint = joint;
|
||||
joint._joint = b2joint;
|
||||
|
||||
this._joints.push(joint);
|
||||
},
|
||||
|
||||
_removeJoint (joint) {
|
||||
if (joint._isValid()) {
|
||||
this._world.DestroyJoint(joint._joint);
|
||||
}
|
||||
|
||||
if (joint._joint) {
|
||||
joint._joint._joint = null;
|
||||
}
|
||||
|
||||
cc.js.array.remove(this._joints, joint);
|
||||
},
|
||||
|
||||
_initCallback: function () {
|
||||
@@ -440,21 +458,42 @@ var PhysicsManager = cc.Class({
|
||||
},
|
||||
|
||||
_syncNode: function () {
|
||||
this._utils.syncNode();
|
||||
|
||||
var bodies = this._bodies;
|
||||
for (var i = 0, l = bodies.length; i < l; i++) {
|
||||
var body = bodies[i];
|
||||
var node = body.node;
|
||||
|
||||
var b2body = body._b2Body;
|
||||
var pos = b2body.GetPosition();
|
||||
|
||||
vec2_tmp.x = pos.x * PTM_RATIO;
|
||||
vec2_tmp.y = pos.y * PTM_RATIO;
|
||||
|
||||
var angle = b2body.GetAngle() * PHYSICS_ANGLE_TO_ANGLE;
|
||||
|
||||
// When node's parent is not scene, convert position and rotation.
|
||||
if (node.parent.parent !== null) {
|
||||
vec2_tmp = node.parent.convertToNodeSpaceAR( vec2_tmp );
|
||||
angle = convertToNodeRotation( node.parent, angle );
|
||||
}
|
||||
|
||||
let tempCache = node._hasListenerCache;
|
||||
node._hasListenerCache = array_tmp;
|
||||
|
||||
// sync position
|
||||
node.position = vec2_tmp;
|
||||
|
||||
// sync rotation
|
||||
node.rotation = angle;
|
||||
|
||||
node._hasListenerCache = tempCache;
|
||||
|
||||
if (body.type === BodyType.Animated) {
|
||||
body.resetVelocity();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_onSceneLaunched: function () {
|
||||
this._debugDrawer.AddDrawerToNode( cc.director.getScene()._sgNode );
|
||||
},
|
||||
|
||||
_onBeginContact: function (b2contact) {
|
||||
var c = cc.PhysicsContact.get(b2contact);
|
||||
c.emit(ContactType.BEGIN_CONTACT);
|
||||
@@ -489,6 +528,14 @@ var PhysicsManager = cc.Class({
|
||||
c._impulse = impulse;
|
||||
c.emit(ContactType.POST_SOLVE);
|
||||
c._impulse = null;
|
||||
},
|
||||
|
||||
_checkDebugDrawValid () {
|
||||
if (!this._debugDrawer || !this._debugDrawer.isValid) {
|
||||
let node = new cc.Node('PHYSICS_MANAGER_DEBUG_DRAW');
|
||||
cc.game.addPersistRootNode(node);
|
||||
this._debugDrawer = node.addComponent(cc.Graphics);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -510,7 +557,6 @@ cc.js.getset(PhysicsManager.prototype, 'enabled',
|
||||
world.SetAllowSleeping(true);
|
||||
|
||||
this._world = world;
|
||||
this._utils = new cc.PhysicsUtils();
|
||||
|
||||
this._initCallback();
|
||||
}
|
||||
@@ -544,26 +590,13 @@ cc.js.getset(PhysicsManager.prototype, 'debugDrawFlags',
|
||||
},
|
||||
function (value) {
|
||||
if (value && !this._debugDrawFlags) {
|
||||
if (!this._debugDrawer) {
|
||||
this._debugDrawer = new cc.PhysicsDebugDraw(PTM_RATIO);
|
||||
this._world.SetDebugDraw( this._debugDrawer );
|
||||
}
|
||||
|
||||
var scene = cc.director.getScene();
|
||||
if (scene) {
|
||||
this._debugDrawer.AddDrawerToNode( cc.director.getScene()._sgNode );
|
||||
}
|
||||
cc.director.on(cc.Director.EVENT_AFTER_SCENE_LAUNCH, this._onSceneLaunched, this);
|
||||
if (this._debugDrawer && this._debugDrawer.node) this._debugDrawer.node.active = true;
|
||||
}
|
||||
else if (!value && this._debugDrawFlags) {
|
||||
cc.director.off(cc.Director.EVENT_AFTER_SCENE_LAUNCH, this._onSceneLaunched, this);
|
||||
if (this._debugDrawer && this._debugDrawer.node) this._debugDrawer.node.active = false;
|
||||
}
|
||||
|
||||
this._debugDrawFlags = value;
|
||||
|
||||
if (this._debugDrawer) {
|
||||
this._debugDrawer.SetFlags(value);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -603,20 +636,6 @@ cc.PhysicsManager = module.exports = PhysicsManager;
|
||||
* 绘制包围盒
|
||||
* @property {Number} e_aabbBit
|
||||
*/
|
||||
/**
|
||||
* !#en
|
||||
* Draw broad-phase pairs
|
||||
* !#zh
|
||||
* 绘制粗测阶段物体
|
||||
* @property {Number} e_pairBit
|
||||
*/
|
||||
/**
|
||||
* !#en
|
||||
* Draw center of mass frame
|
||||
* !#zh
|
||||
* 绘制物体质心
|
||||
* @property {Number} e_centerOfMassBit
|
||||
*/
|
||||
/**
|
||||
* !#en
|
||||
* Draw joint connections
|
||||
|
||||
@@ -96,10 +96,14 @@ var RayCastType = cc.Enum({
|
||||
});
|
||||
cc.RayCastType = RayCastType;
|
||||
|
||||
var DrawBits = b2.Draw;
|
||||
|
||||
module.exports = {
|
||||
BodyType: BodyType,
|
||||
ContactType: ContactType,
|
||||
RayCastType: RayCastType,
|
||||
|
||||
DrawBits: DrawBits,
|
||||
|
||||
PTM_RATIO: 32,
|
||||
ANGLE_TO_PHYSICS_ANGLE: -Math.PI / 180,
|
||||
|
||||
@@ -165,6 +165,7 @@ var PhysicsCollider = cc.Class({
|
||||
|
||||
var node = body.node;
|
||||
var scale = getWorldScale(node);
|
||||
this._scale = scale;
|
||||
|
||||
var shapes = scale.x === 0 && scale.y === 0 ? [] : this._createShape(scale);
|
||||
|
||||
|
||||
@@ -24,8 +24,6 @@ require('./joint/CCWheelJoint');
|
||||
require('./joint/CCRopeJoint');
|
||||
|
||||
if (!CC_JSB) {
|
||||
require('./platform/CCPhysicsDebugDraw');
|
||||
require('./platform/CCPhysicsUtils');
|
||||
require('./platform/CCPhysicsContactListner');
|
||||
require('./platform/CCPhysicsAABBQueryCallback');
|
||||
require('./platform/CCPhysicsRayCastCallback');
|
||||
|
||||
@@ -197,8 +197,6 @@ var Joint = cc.Class({
|
||||
this.body = this.getComponent(cc.RigidBody);
|
||||
|
||||
if (this._isValid()) {
|
||||
var world = cc.director.getPhysicsManager()._getWorld();
|
||||
|
||||
var def = this._createJointDef();
|
||||
if (!def) return;
|
||||
|
||||
@@ -206,10 +204,7 @@ var Joint = cc.Class({
|
||||
def.bodyB = this.connectedBody._getBody();
|
||||
def.collideConnected = this.collideConnected;
|
||||
|
||||
this._joint = world.CreateJoint(def);
|
||||
if (this._joint) {
|
||||
this._joint._joint = this;
|
||||
}
|
||||
cc.director.getPhysicsManager()._addJoint(this, def);
|
||||
|
||||
this._inited = true;
|
||||
}
|
||||
@@ -217,13 +212,7 @@ var Joint = cc.Class({
|
||||
__destroy: function () {
|
||||
if (!this._inited) return;
|
||||
|
||||
if (this._isValid()) {
|
||||
cc.director.getPhysicsManager()._getWorld().DestroyJoint(this._joint);
|
||||
}
|
||||
|
||||
if (this._joint) {
|
||||
this._joint._joint = null;
|
||||
}
|
||||
cc.director.getPhysicsManager()._removeJoint(this);
|
||||
|
||||
this._joint = null;
|
||||
this._inited = false;
|
||||
|
||||
@@ -263,7 +263,7 @@ var MouseJoint = cc.Class({
|
||||
return;
|
||||
}
|
||||
|
||||
var camera = cc.Camera.find(this.node);
|
||||
var camera = cc.Camera.findCamera(this.node);
|
||||
if (camera) {
|
||||
this.target = camera.getCameraToWorldPoint(this._pressPoint);
|
||||
}
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
|
||||
|
||||
var PhysicsDebugDraw = cc.Class({
|
||||
name: 'cc.PhysicsDebugDraw',
|
||||
mixins: [b2.Draw],
|
||||
|
||||
ctor: function () {
|
||||
this._drawer = new _ccsg.GraphicsNode();
|
||||
},
|
||||
|
||||
getDrawer: function () {
|
||||
return this._drawer;
|
||||
},
|
||||
|
||||
AddDrawerToNode: function (node) {
|
||||
this._drawer.removeFromParent();
|
||||
node.addChild(this._drawer);
|
||||
},
|
||||
|
||||
ClearDraw: function () {
|
||||
this._drawer.clear();
|
||||
},
|
||||
|
||||
_DrawPolygon: function (vertices, vertexCount) {
|
||||
var drawer = this._drawer;
|
||||
|
||||
for (var i=0; i<vertexCount; i++) {
|
||||
if (i === 0)
|
||||
drawer.moveTo(vertices[i].x * PTM_RATIO, vertices[i].y * PTM_RATIO);
|
||||
else {
|
||||
drawer.lineTo(vertices[i].x * PTM_RATIO, vertices[i].y * PTM_RATIO);
|
||||
}
|
||||
}
|
||||
|
||||
drawer.close();
|
||||
},
|
||||
|
||||
DrawPolygon: function (vertices, vertexCount, color) {
|
||||
this._applyStrokeColor(color);
|
||||
this._DrawPolygon(vertices, vertexCount);
|
||||
this._drawer.stroke();
|
||||
},
|
||||
|
||||
DrawSolidPolygon: function (vertices, vertexCount, color) {
|
||||
this._applyFillColor(color);
|
||||
this._DrawPolygon(vertices, vertexCount);
|
||||
this._drawer.fill();
|
||||
this._drawer.stroke();
|
||||
},
|
||||
|
||||
_DrawCircle: function (center, radius) {
|
||||
this._drawer.circle(center.x * PTM_RATIO, center.y * PTM_RATIO, radius * PTM_RATIO);
|
||||
},
|
||||
|
||||
DrawCircle: function (center, radius, color) {
|
||||
this._applyStrokeColor(color);
|
||||
this._DrawCircle(center, radius);
|
||||
this._drawer.stroke();
|
||||
},
|
||||
|
||||
DrawSolidCircle: function (center, radius, axis, color) {
|
||||
this._applyFillColor(color);
|
||||
this._DrawCircle(center, radius);
|
||||
this._drawer.fill();
|
||||
},
|
||||
|
||||
DrawSegment: function (p1, p2, color) {
|
||||
var drawer = this._drawer;
|
||||
|
||||
if (p1.x === p2.x && p1.y === p2.y) {
|
||||
this._applyFillColor(color);
|
||||
this._DrawCircle(p1, 2/PTM_RATIO);
|
||||
drawer.fill();
|
||||
return;
|
||||
}
|
||||
this._applyStrokeColor(color);
|
||||
drawer.moveTo(p1.x * PTM_RATIO, p1.y * PTM_RATIO);
|
||||
drawer.lineTo(p2.x * PTM_RATIO, p2.y * PTM_RATIO);
|
||||
drawer.stroke();
|
||||
},
|
||||
|
||||
DrawPoint: function (center, radius, color) {
|
||||
},
|
||||
|
||||
_applyStrokeColor: function (color) {
|
||||
this._drawer.setStrokeColor( cc.color(color.r*255, color.g*255, color.b*255, 150) );
|
||||
},
|
||||
|
||||
_applyFillColor: function (color) {
|
||||
this._drawer.setFillColor( cc.color(color.r*255, color.g*255, color.b*255, 150) );
|
||||
}
|
||||
});
|
||||
|
||||
cc.PhysicsDebugDraw = module.exports = PhysicsDebugDraw;
|
||||
@@ -1,52 +0,0 @@
|
||||
|
||||
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 () {
|
||||
}
|
||||
|
||||
PhysicsUtils.prototype.addB2Body = function (body) {};
|
||||
|
||||
PhysicsUtils.prototype.removeB2Body = function (body) {};
|
||||
|
||||
PhysicsUtils.prototype.syncNode = function () {
|
||||
var bodies = cc.director.getPhysicsManager()._bodies;
|
||||
|
||||
for (var i = 0, l = bodies.length; i < l; i++) {
|
||||
var body = bodies[i];
|
||||
var node = body.node;
|
||||
|
||||
var b2body = body._b2Body;
|
||||
var pos = b2body.GetPosition();
|
||||
|
||||
tempPosition.x = pos.x * PTM_RATIO;
|
||||
tempPosition.y = pos.y * PTM_RATIO;
|
||||
|
||||
var angle = b2body.GetAngle() * PHYSICS_ANGLE_TO_ANGLE;
|
||||
|
||||
// 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 );
|
||||
}
|
||||
|
||||
var sgNode = node._sgNode;
|
||||
|
||||
// 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;
|
||||
}
|
||||
};
|
||||
|
||||
cc.PhysicsUtils = module.exports = PhysicsUtils;
|
||||
Reference in New Issue
Block a user