title: Ray vs ellipsoid intersection
body: <p>I am trying to implement ray vs ellipsoid intersection by "squishing" space and doing ray vs sphere:</p>
  
  <ol>
  <li><p>create mat3 S with ellipsoid radius at diagonal</p></li>
  <li><p>squish ray by multiplying start and direction by an inverse of S</p></li>
  <li><p>intersect ray with sphere of radius 1.0 in local space</p></li>
  <li><p>multiply hitPoint by S to unsquish it.</p></li>
  </ol>
  
  <p>Here is ray vs sphere:</p>
  
  <pre><code>float P = glm::dot(dir, sphereCenter-start);
  float L = glm::distance(start, sphereCenter);
  float d = sqrt(L*L - P*P);
  if (d &lt; radius) {
      float x0 = sqrt(1.f - d*d);
      hitPoint = start + dir*(P - x0);
      hitNormal = glm::normalize(hitPoint - sphereCenter);
  }
  else if (d == radius) {
      hitPoint = start + dir*P;
      hitNormal = glm::normalize(hitPoint - sphereCenter);
  }
  else {
      return false;
  }
  if (glm::distance(start, hitPoint) &gt; dist) return false;
  return true;
  </code></pre>
  
  <p>Here is the squishing part:</p>
  
  <pre><code>glm::vec3 S = start;
      glm::vec3 Dir = dir;
  
      auto sphereCenter = thisEntity()-&gt;transform()-&gt;getPosition();
      auto scale = thisEntity()-&gt;transform()-&gt;getScale();
  
      glm::mat3 q = glm::mat3(0);
      float x = _radius.x * scale.x;
      float y = _radius.y * scale.y;
      float z = _radius.z * scale.z;
      q[0][0] = x;
      q[1][1] = y;
      q[2][2] = z;
      glm::mat3 qI = glm::inverse(q);
  
      S = qI * S;
      Dir = qI * Dir;
  
      //calculate hit point in world space squished
      glm::vec3 hitPoint, hitNormal;
      if (!IntersectionsMath::instance()-&gt;segmentVsSphere(sphereCenter, S, Dir, dist, 1.f, hitPoint, hitNormal)) return;
  
      hitPoint = q * hitPoint;
  
      hit.pushHit(hitPoint, hitNormal, this);
  </code></pre>
  
  <p>Current ray sphere code is for world position, i'm trying to make it work at the origin so it shouldn't matter. Ray vs regular sphere works fine, ellipsoid is the problem.
  I spent a lot of time on this and something somewhere is wrong.</p>