2014-05-21 1 views
0
b2GravityController.prototype.Step = function (step) { 
    var i = null; 
    var body1 = null; 
    var p1 = null; 
    var mass1 = 0; 
    var j = null; 
    var body2 = null; 
    var p2 = null; 
    var dx = 0; 
    var dy = 0; 
    var r2 = 0; 
    var f = null; 
    if (this.invSqr) { 
    for (i = this.m_bodyList; 
    i; i = i.nextBody) { 
     body1 = i.body; 
     p1 = body1.GetWorldCenter(); 
     mass1 = body1.GetMass(); 
     for (j = this.m_bodyList; 
     j != i; j = j.nextBody) { 
      body2 = j.body; 
      p2 = body2.GetWorldCenter(); 
      dx = p2.x - p1.x; 
      dy = p2.y - p1.y; 
      r2 = dx * dx + dy * dy; 
      if (r2 < Number.MIN_VALUE) continue; 
      f = new b2Vec2(dx, dy); 
      f.Multiply(this.G/r2/Math.sqrt(r2) * mass1 * body2.GetMass()); 
      if (body1.IsAwake()) body1.ApplyForce(f, p1); 
      f.Multiply((-1)); 
      if (body2.IsAwake()) body2.ApplyForce(f, p2); 
     } 
    } 
    } 
    else { 
    for (i = this.m_bodyList; 
    i; i = i.nextBody) { 
     body1 = i.body; 
     p1 = body1.GetWorldCenter(); 
     mass1 = body1.GetMass(); 
     for (j = this.m_bodyList; 
     j != i; j = j.nextBody) { 
      body2 = j.body; 
      p2 = body2.GetWorldCenter(); 
      dx = p2.x - p1.x; 
      dy = p2.y - p1.y; 
      r2 = dx * dx + dy * dy; 
      if (r2 < Number.MIN_VALUE) continue; 
      f = new b2Vec2(dx, dy); 
      f.Multiply(this.G/r2 * mass1 * body2.GetMass()); 
      if (body1.IsAwake()) body1.ApplyForce(f, p1); 
      f.Multiply((-1)); 
      if (body2.IsAwake()) body2.ApplyForce(f, p2); 
     } 
     } 
    } 
} 

이 코드는 box2dweb에서 보편적 인 중력을 시뮬레이션하는 데 사용됩니다. 왜 만유 인력을 계산하는 데는 두 가지 다른 방법이 있습니까? if 문은 무엇을 위해 사용됩니까? 그리고 나는 이것을 인터넷에서 보았습니다 invSqr: Boolean; /// If true, gravity is proportional to r^-2, otherwise r^-1, 그러나 나는이 invsqr의 의미를 이해하지 못합니다.보편적 인 중력을 시뮬레이션하는 box2dweb에서이 코드는 어떻게 발생하며 if 문을 사용하는 이유는 무엇입니까?

답변

0

당신은 큰 차이점을 볼 수 있습니다 모든 물리학의 계산에 비용이 많이 드는 sqrt 이후 정상화를 피하기 위해이 줄을

if (this.invSqr) { 
    ... 
    f.Multiply(this.G/r2/Math.sqrt(r2) * mass1 * body2.GetMass()); 
} 
else { 
    ... 
    f.Multiply(this.G/r2 * mass1 * body2.GetMass()); 
} 

그것입니다. 문제가되는 도서관을 이해하지 못하면서이 일의 파급 효과를 설명 할 수 없습니다.

관련 문제