Cryptonomicon Posted April 24, 2014 Share Posted April 24, 2014 A really hard problem to trace so please put up with my inability to post a simple example, but I am getting NaN's appearing in debug mode both in firefox an chrome that trace themselves to (so far) to infinities appearing deep within the Gauss Seidel GSSolver in p2 physics. It appears when I have a body on the screen and then I click on the display. The click throws the body to 0,0 which I assume is some form of default for NaN's that appear - that are in turn created by infinity in the solver. It does not appear in normal mode - only when I am debugging at around line 66290 in phaser .js Anyway has anyone had problems with bodies teleporting to the origin when the screen is clicked when using p2.js - I suspect it is an initialization problem NaN's and infinity should not appear in a maths solver - or at least without an exception 0 Equation { minForce=-1.7976931348623157e+308, maxForce=1.7976931348623157e+308, bodyA={...}, more...} G Float32Array { 0=-1, 1=0, 2=-4.032659530639648, more...} a Infinity b 0.9411764705882353 bodyA Object { id=5, world={...}, shapes=[6], more...} bodyB Object { id=6, world={...}, shapes=[1], more...} Link to comment Share on other sites More sharing options...
Cryptonomicon Posted April 24, 2014 Author Share Posted April 24, 2014 The cause is line 69398 in phaser.js /** * @method Phaser.Physics.P2#update */ update: function () { if (this.useElapsedTime) { this.world.step(this.game.time.physicsElapsed); } else { this.world.step(this.frameRate); } }, which when you are debugging has a this.game.time.physicsElapsed that is 0. game Object { id=0, width=960, height=640, more...} lastTime 1398305112131 msMax 388 msMin 0 now 1398305112131 pauseDuration 12263 pausedTime 0 physicsElapsed 0 time 1398305112131 timeToCall 0 That blows the following in the this.epsilon equation in the function around line 62918 . (h is zero in the denominator) /** * Compute SPOOK parameters .a, .b and .epsilon according to the current parameters. See equations 9, 10 and 11 in the <a href='http://www8.cs.umu.se/kurser/5DV058/VT09/lectures/spooknotes.pdf">SPOOK notes</a>. * @method update */ Equation.prototype.update = function(){ var k = this.stiffness, d = this.relaxation, h = this.timeStep; this.a = 4.0 / (h * (1 + 4 * d)); this.b = (4.0 * d) / (1 + 4 * d); this.epsilon = 4.0 / (h * h * k * (1 + 4 * d)); <-- This is the culprit. It expects h to be non zero this.needsUpdate = false; }; As it only occurs when debugging I might just give it a timeStep++ somewhere Am not sure if it is a P2 bug or phaser bug so will let wiser minds than me work that out. Link to comment Share on other sites More sharing options...
george Posted April 24, 2014 Share Posted April 24, 2014 Can you provide a minimal example for this?Could you then try one of those settings? //You use useElapsedTime at the moment? Try setting it to false:game.physics.p2.useElapsedTime = false//or with useElapsedTime enabled:game.stage.disableVisibilityChange = truegame.time.deltaCap = 1/30RegardsGeorge Link to comment Share on other sites More sharing options...
Cryptonomicon Posted April 24, 2014 Author Share Posted April 24, 2014 I found I needed elapsed time due to variances in browser speed - I was getting anything from 10fps to 50fps - and elapsed time fixed that. in the end I overrode the default update this.game.physics.startSystem(Phaser.Physics.P2JS); //start physics as normalthis.game.physics.p2.update=this.replacementP2PhysicsUpdate; //swap out the standard update //and swapped in my own as below - the change is bold in blue my.Class.prototype.replacementP2PhysicsUpdate=function () { if (this.useElapsedTime) { this.world.step(this.game.time.physicsElapsed+0.0001); } else { this.world.step(this.frameRate); }}; /* hack ends here */ and all seems fine. Tell you what though, in thirty years of coding phaser.js is probably the nicest to read through I have ever seen. To even be able to trace such an obscure error in 60,000 lines of code using the **cough** debugger **cough** in firefox in a couple of hours is saying a lot given it is written in the untyped mess that is JavaScript. Link to comment Share on other sites More sharing options...
george Posted April 24, 2014 Share Posted April 24, 2014 Well, you just have created a so called monkey patch By overriding the core of phaser you exclude yourself from updating phaser or you have to maintain all of your 'monkey patches'. The main problem here is that physicsElapsed has a value of zero. That's clearly a bug an could be related to the first timer tick of phaser. Is the error happening just after the start ? Then you could try to defer the creation of your physics for some milliseconds with a timer/setTimeout? Could you please try this ? Or maybe create an issue on phaser's github repository? If it's a bug it will happen for other people so we should fix it- maybe even with that type of fix you just created Link to comment Share on other sites More sharing options...
george Posted April 24, 2014 Share Posted April 24, 2014 That's funny. Just now I got a related error with physics elapsed. I provided a fix that may also fix your problem.https://github.com/photonstorm/phaser/pull/758 Regards George Link to comment Share on other sites More sharing options...
Cryptonomicon Posted April 24, 2014 Author Share Posted April 24, 2014 that is how the universe works today must be physics timing day. It seems to be only in debug mode - the debugger "freezes time" somewhat when you enter the debugger and when you reenter the browser comes back to where it left off. If it was C++ I would say it looks like the environment getting pushed onto a stack on debug entry and off again on exit - but this ain't C++ so I wont say that. maintaining this one will be easy - has a great big //TODO: in it telling me to check future releases. thanks Link to comment Share on other sites More sharing options...
Recommended Posts