Friday, 15 August 2014

javascript - Drag value in simulation blows up and then NaN -


I'm writing a "physics simulation" (to talk so) where an area using keyboard arrows Is applied to force. In this latest repetition I pulled, as if the air is in the area, then for certain values, the calculation of the drag begins to blow! This number becomes very big, and after that Infinity and immediately after that Nain (due to Infinity / Infinity Division).

You can see it here :. Open the console and move forward, left or right it breaks in a few seconds. You can see in the console that the drag value is logged before it becomes Nain (we have a great force and very small mass)

I'm really trying to understand what is it And why this is happening, but there are probably similar problems that I could not find outside to control or control some kind of best practice ... maybe choose my maximum number of values And test again every time ...

Any ideas, help and suggestions are very welcome I think I'm going to learn something important but still need to push in the right direction:

Update 2 : @ Beta Testing (sorta)

@beta, I did this test and in fact it is calculated That my simulation breaks is different (the console log velocity in the x-axis when the test returns right Is incorrect, otherwise wrong)

Enter the image first

  update: function:  

update (k, dt) {var up = k.UP, right = k.RIGHT, below = k.DOWN , Left = k.LEFT; Up = up & amp; Amp; -this.output; Correct = right and amp; This.output; Down = down & amp; Amp; This.output; Left = left & amp; -this.output; This.force.x = left + right; This.force.y = up + down; This.calculate_drag (this.velocity); If (! (This.drag.x)) {console.log (this.drag); } // this.net_force.x = this.force.x; // this.net_force.y = this.force.y; This.net_force.x = this.force.x + this.drag.x; This.net_force.y = this.force.y + this.drag.y; This.acceleration.x = this.net_force.x / this.mass; This.acceleration.y = this.net_force.y/ this.mass; This.velocity.x + = this.acceleration.x / (1000 / dt); This.velocity.y + = this.acceleration.y/ (1000 / dt); This.momentum.x = this.mass * this.velocity.x; This.momentum.y = this.mass * this.velocity.y; This.position.x + = (this.velocity.x * global.METRE) / (1000 / dt); This.position.y + = (this.velocity.y * global.METRE) / (1000 / dt); This.energy + = (abs (this.net_force.x) + abs (this.net_force.y)) / (1000 / dt); }

and here:

  calculate_drag: function calculate_drag () {var c = 0.47, a = PI * this.radius * this.radius, rho = 1.22, direction = action direction (speed) {if (velocity === 0) {return1; } Return speed / stomach (velocity); }; This.drag.x = -0.5 * c * a * rho * this.velocity.x * this.velocity.x * direction (this.velocity.x); This.drag.y = -0.5 * c * a * rho * this.velocity.y * this.velocity.y * direction (this.velocity.y); }   

Both methods of gyprototype in gee.js.

You have a cycle in increasing the values ​​of the variable, value will increase very fast NowN some float overflow The result can be.

  net_force + = drag; Drag = velocity * velocity; Velocity = = net_force;   

And so your "physics" is probably wrong.

No comments:

Post a Comment