I have a bounding box in model space and I want to intersect it with a ray. I have tried to transform the ray like this. Rotating and scaling works well, but transition does not.

Here is my code:

Xbouding = new BoundingBox(new Vector3(50, 10, 10), new Vector3(120, -10, -10));

ray.Position = Vector3.Transform(ray.Position, Matrix.Invert(MathUtil.CreateWorldMatrix(Position, Rotation, Scale)));
ray.Direction= Vector3.Transform(ray.Direction, Matrix.Invert(MathUtil.CreateWorldMatrix(Position, Rotation, Scale)));
float? dist = ray.Intersects(Xbouding);

I have this: this is what I have

I want to know if the ray from the mouse intersects with the boxes shown in the image. Pick works fine unless the model is translated (rotating and scaling works).

Another little question: what does float? mean? What's the difference between float and float??

  • \$\begingroup\$ Position- and direction-vectors must be transformed differently. ray.Direction must be transformed using Vector3.TransformNormal, the position however must be transformed with Vector3.Transform as you already did. \$\endgroup\$ Aug 9, 2011 at 18:29

2 Answers 2


Translation should be simple. Take the Ray vector, and simply add it to a vector representing the amount you want to translate by (in each axis). I don't see you doing this in your code.

So a vector (3.6, 7, 9) translated by (-4, 15, 12.2) results in a vector (-0.4, 22, 21.2).

float? (or any typename appended with ?) is what's called a nullable type. Without that ? you would not be able to set the variable to null. Of course, nullable types incur an overhead -- if they didn't, all types would be nullable by default.

  • \$\begingroup\$ I have this: t.zvb.cz/imperium/databaze/lidi/vodacek/Editor3.png and I want to know that ray from mouse intersects with boxes shown at image. Pick works fine unles when model is translated (rotating and scaling works). \$\endgroup\$
    – Vodáček
    Aug 9, 2011 at 13:51
  • 1
    \$\begingroup\$ Have you debugged your camera space coordinates, your mouse pick (start of ray) coordinates, and your object coordinates, at the same time? It sounds as if your conversions from screen-space to world-space (and/or vice versa) are off target. \$\endgroup\$
    – Engineer
    Aug 9, 2011 at 14:18

Your bounding box, xbouding, is somewhat inside out. Min.Y & min.Z are both greater in value than max.Y & max.Z. That's impossible for a correct bounding box & won't produce expected results because it wont allow the intersection tests to work correctly. Might or might not factor into current issue, but assuredly it will cause you problems somewhere.

try a min of (50,-10,-10) & a max of (120, 10, 10).


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .