Note that there are some explanatory texts on larger screens.

plurals
  1. PODrag value in simulation blows up and then NaN
    primarykey
    data
    text
    <p>I'm writing a "physics simulation" (so to speak) where using the keyboard arrows one applies forces to a sphere. In this latest iteration I added drag, like the sphere is in the air. Then, for certain values, the drag calculation starts to blow up! The number gets too big, and then Infinity and right after that NaN (cause of the Infinity / Infinity division).</p> <p>You can see it happening here: <a href="http://gool.jit.su/the-drag" rel="nofollow noreferrer">http://gool.jit.su/the-drag</a>. Open the console and start moving, left or right. In a few seconds it breaks. You can see in the console the drag value being logged until right before it becomes NaN. (we have a great force and a very small mass)</p> <p>I'm really trying to understand what is happening and why it is happening, but perhaps there are already information on similar problemas that I was unable to find out or some sort of best practice to keep the number under control... Perhaps choose a value to be my MAX and test agains it every time...</p> <p>Any idea, help and suggestion is very welcome. I think I am about to learn something important, but still needing a push in the right direction. :)</p> <p><strong>UPDATE 2</strong>: @Beta's test (sorta)</p> <p>After @Beta's comment I did this test and indeed his calculations seems to show where my simulation breaks apart. (console log velocity in the x axis when the test returns true, false otherwise)</p> <p><img src="https://i.stack.imgur.com/waj5k.png" alt="enter image description here"></p> <p><strong>UPDATE 1</strong>: Some code</p> <p>Most "physics" happen here:</p> <pre><code>update: function update(k, dt) { var up = k.UP, right = k.RIGHT, down = k.DOWN, left = k.LEFT; up = up &amp;&amp; -this.output; right = right &amp;&amp; this.output; down = down &amp;&amp; this.output; left = left &amp;&amp; -this.output; this.force.x = left + right; this.force.y = up + down; this.calculate_drag(this.velocity); if (!isNaN(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); } </code></pre> <p>And here:</p> <pre><code>calculate_drag: function calculate_drag() { var c = 0.47, a = PI * this.radius * this.radius, rho = 1.22, direction = function direction(velocity) { if (velocity === 0) { return 1; } return velocity / abs(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); } </code></pre> <p>Both methods of gPrototype in gee.js.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload