1
\$\begingroup\$

I've got a huge problem with BoundingSpheres and Rays in XNA and I really don't know how to solve it.

The problem:

foreach (BoundingSphere s1 in GetCollisionHullBoundingSpheres())
   if (ray.Intersects(s1) != null)
   {
      System.Console.WriteLine("Sphere: " + s1.Center + " Radius: " + s1.Radius);
      System.Console.WriteLine("Ray: " + ray.Position + " Direction: " + ray.Direction);
      System.Console.WriteLine("Ray: " + ray.Intersects(s1).Value + "\n");
      return true;
   }

The collision hull bounding spheres are the spheres from the mesh and the ray is calculated by using the playerposition and the vector (0,-5,0) as the direction.

Here is the output:

Sphere: {X:-16,52211 Y:-3,34731 Z:-15,66445} Radius: 5,077909
Ray: {X:75,29767 Y:29,15445 Z:-45,73476} Direction: {X:0 Y:-5 Z:0}
Ray: 35,84612

I can't understand how that ray can intersect with the sphere.

\$\endgroup\$
2
\$\begingroup\$

Xna's Ray.Intersect() tests that utilize the Ray's direction assumes that the direction vector is unit length.

If you normalize ray.Direction before running the test you will find it will not intersect.

the documentation supports the direction vector being unit length.: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.ray_members.aspx Unit length vectors have a magnitude of 1, your 0,-5,0 has a magnitude of 5. A by product of the math behind the intersection test is that the sphere's radius is scaled by the magnitude of the direction vector... so it needs to be kept at 1

\$\endgroup\$
  • 1
    \$\begingroup\$ the documentation supports the direction vector being unit length.: msdn.microsoft.com/en-us/library/… Unit length vectors have a magnitude of 1, your 0,-5,0 has a magnitude of 5. A by product of the math behind the intersection test is that the sphere's radius is scaled by the magnitude of the direction vector... so it needs to be kept at 1. \$\endgroup\$ – Steve H Jul 13 '13 at 16:41
  • \$\begingroup\$ Steve, you should add your comment as an edit to your answer. \$\endgroup\$ – MichaelHouse Jul 13 '13 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.