Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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??

share|improve this question
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. – Maik Semder Aug 9 '11 at 18:29
up vote 4 down vote accepted

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.

share|improve this answer
I have this: 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). – Vodáček Aug 9 '11 at 13:51
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. – Arcane Engineer Aug 9 '11 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).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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