From dd8a393bc971d7ac7129b83dc8e9fd4873fd6f9a Mon Sep 17 00:00:00 2001 From: photonstorm Date: Fri, 21 Feb 2014 12:40:49 +0000 Subject: [PATCH] Updated version of p2 --- build/p2.js | 92 ++++++++++++++++++---------- src/gameobjects/GameObjectFactory.js | 2 +- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/build/p2.js b/build/p2.js index 8835ae42..e3d50ce4 100644 --- a/build/p2.js +++ b/build/p2.js @@ -1614,12 +1614,22 @@ Broadphase.aabbCheck = function(bodyA, bodyB){ * @return {Boolean} */ Broadphase.canCollide = function(bodyA, bodyB){ + // Cannot collide static bodies - if(bodyA.motionState & Body.STATIC && bodyB.motionState & Body.STATIC) + if(bodyA.motionState == Body.STATIC && bodyB.motionState == Body.STATIC) return false; - // Cannot collide sleeping bodies - if(bodyA.sleepState & Body.SLEEPING && bodyB.sleepState & Body.SLEEPING) + // Cannot collide static vs kinematic bodies + if( (bodyA.motionState == Body.KINEMATIC && bodyB.motionState == Body.STATIC) || + (bodyA.motionState == Body.STATIC && bodyB.motionState == Body.KINEMATIC)) + return false; + + // Cannot collide kinematic vs kinematic + if(bodyA.motionState == Body.KINEMATIC && bodyB.motionState == Body.KINEMATIC) + return false; + + // Cannot collide both sleeping bodies + if(bodyA.sleepState == Body.SLEEPING && bodyB.sleepState == Body.SLEEPING) return false; return true; @@ -2027,9 +2037,9 @@ Narrowphase.prototype.createContactEquation = function(bodyA,bodyB,shapeA,shapeB c.firstImpact = !this.collidedLastStep(bodyA,bodyB); c.enabled = true; - if(bodyA.allowSleep && (bodyA.motionState & Body.DYNAMIC) && !(bodyB.motionState & Body.STATIC || bodyB.sleepState === Body.SLEEPY)) + if(bodyA.allowSleep && (bodyA.motionState == Body.DYNAMIC) && !(bodyB.motionState == Body.STATIC || bodyB.sleepState === Body.SLEEPY)) bodyA.wakeUp(); - if(bodyB.allowSleep && (bodyB.motionState & Body.DYNAMIC) && !(bodyA.motionState & Body.STATIC || bodyA.sleepState === Body.SLEEPY)) + if(bodyB.allowSleep && (bodyB.motionState == Body.DYNAMIC) && !(bodyA.motionState == Body.STATIC || bodyA.sleepState === Body.SLEEPY)) bodyB.wakeUp(); return c; @@ -4111,9 +4121,11 @@ SAPBroadphase.prototype.getCollisionPairs = function(world){ break; // add pair to preliminary list - var key = bi.id < bj.id ? bi.id+' '+bj.id : bj.id+' '+bi.id; - preliminaryList[key] = true; - preliminaryList.keys.push(key); + if(Broadphase.canCollide(bi,bj)){ + var key = bi.id < bj.id ? bi.id+' '+bj.id : bj.id+' '+bi.id; + preliminaryList[key] = true; + preliminaryList.keys.push(key); + } } } @@ -4128,9 +4140,11 @@ SAPBroadphase.prototype.getCollisionPairs = function(world){ break; // If in preliminary list, add to final result - var key = bi.id < bj.id ? bi.id+' '+bj.id : bj.id+' '+bi.id; - if(preliminaryList[key] && Broadphase.boundingRadiusCheck(bi,bj)) - result.push(bi,bj); + if(Broadphase.canCollide(bi,bj)){ + var key = bi.id < bj.id ? bi.id+' '+bj.id : bj.id+' '+bi.id; + if(preliminaryList[key] && Broadphase.boundingRadiusCheck(bi,bj)) + result.push(bi,bj); + } } } @@ -6793,6 +6807,12 @@ function Body(options){ */ this.sleepTimeLimit = 1; + /** + * Gravity scaling factor. If you want the body to ignore gravity, set this to zero. If you want to reverse gravity, set it to -1. + * @property {Number} gravityScale + */ + this.gravityScale = 1; + this.timeLastSleepy = 0; this.concavePath = null; @@ -7040,6 +7060,7 @@ Body.prototype.fromPolygon = function(path,options){ var p = new decomp.Polygon(); p.vertices = path; + // Make it counter-clockwise p.makeCCW(); @@ -7174,7 +7195,7 @@ Body.prototype.addConstraintVelocity = function(){ * @param {number} dt Current time step */ Body.prototype.applyDamping = function(dt){ - if(this.motionState & Body.DYNAMIC){ // Only for dynamic bodies + if(this.motionState == Body.DYNAMIC){ // Only for dynamic bodies // Since Math.pow generates garbage we check if we can reuse the scaling coefficient from last step if(dt != this.lastDampingTimeStep){ @@ -8619,7 +8640,7 @@ function getUnvisitedNode(nodes){ var Nnodes = nodes.length; for(var i=0; i!==Nnodes; i++){ var node = nodes[i]; - if(!node.visited && !(node.body.motionState & STATIC)){ // correct? + if(!node.visited && !(node.body.motionState == STATIC)){ // correct? return node; } } @@ -9401,7 +9422,7 @@ World.prototype.internalStep = function(dt){ for(var i=0; i!==Nbodies; i++){ var b = bodies[i], fi = b.force; - vec2.scale(mg,g,b.mass); // F=m*g + vec2.scale(mg,g,b.mass*b.gravityScale); // F=m*g add(fi,fi,mg); } } @@ -9417,7 +9438,8 @@ World.prototype.internalStep = function(dt){ if(this.applyDamping){ for(var i=0; i!==Nbodies; i++){ var b = bodies[i]; - b.applyDamping(dt); + if(b.motionState == Body.DYNAMIC) + b.applyDamping(dt); } } @@ -9466,13 +9488,14 @@ World.prototype.internalStep = function(dt){ // Emit shape end overlap events var last = this.overlappingShapesLastState; - for(var i=0; i0){ + if(body.sleepState !== Body.SLEEPING && body.motionState!=Body.STATIC){ World.integrateBody(body,dt); } } diff --git a/src/gameobjects/GameObjectFactory.js b/src/gameobjects/GameObjectFactory.js index 3dae41e8..be6ac2e4 100644 --- a/src/gameobjects/GameObjectFactory.js +++ b/src/gameobjects/GameObjectFactory.js @@ -95,7 +95,7 @@ Phaser.GameObjectFactory.prototype = { * A Group is a container for display objects that allows for fast pooling, recycling and collision checks. * * @method Phaser.GameObjectFactory#group - * @param {any} parent - The parent Group or DisplayObjectContainer that will hold this group, if any. + * @param {any} [parent] - The parent Group or DisplayObjectContainer that will hold this group, if any. If set to null the Group won't be added to the display list. If undefined it will be added to World by default. * @param {string} [name='group'] - A name for this Group. Not used internally but useful for debugging. * @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World. * @return {Phaser.Group} The newly created group.