Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to implement Object picking. My BasicModel.cs has a function:

public bool CheckRayIntersection(Ray ray)
    {
        //Where aabb is the BasicModel's BoundingBox
        if (ray.Intersects(aabb) != null)
        { return true; }
        else
        {

            return false;
        }
    }

I create the Ray in my Camera.cs

public Ray GetMouseRay(Vector2 mousePosition, Viewport viewport)
    {
        Vector3 near = new Vector3(mousePosition, 0);
        Vector3 far = new Vector3(mousePosition, 1);

        near = viewport.Unproject(near, projection, view, Matrix.Identity);
        far = viewport.Unproject(far, projection, view, Matrix.Identity);

        return new Ray(near, Vector3.Normalize(far - near));
    }

And finally in game1.cs I check for Intersection in every BasicModel and write "Intersects" to Console.

foreach(BasicModel model in assetManager._modelsOnscreen)
                {
                    if(model.CheckRayIntersection(camera.GetMouseRay(mouseLocation,viewport)))
                    {
                        winForm.ConsoleWrite = "intersects";
                    }
                }

Somehow it always returns true for intersection with offset (to the right) to the actual Boundingbox. I also don't have any clue how I could visualize the problem or step through it.

Any advice is welcome, BC++

share|improve this question
Everything looks good in your code as far as I can tell, are you sure the AABB is being properly created (correct dimensions)? – Nic Foster Feb 8 '12 at 21:43
@Nic Foster: I'm pretty sure it is drawn correct. You can see it here. img19.imageshack.us/img19/9926/basica.png and here youtube.com/watch?v=_ltrsVsH_qw&feature=youtu.be – bodycountPP Feb 9 '12 at 9:56
That AABB looks a bit large for that sphere, but I'm not sure that would be causing your problem. – Nic Foster Feb 9 '12 at 15:41
Any idea how I could visuilize or step through this issue? – bodycountPP Feb 10 '12 at 12:11
1  
Simplify your steps, put only simple 1x1x1 box to start with, then test against that. Do the math for your test case on paper first so you understand what should happen, then place a breakpoint and test it in-game. – Nic Foster Feb 10 '12 at 15:12
show 3 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.