XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

2D Bounding Rectangle over a 3D model

Last post 11/3/2009 7:55 AM by jadephoenix1988. 6 replies.
  • 10/27/2009 12:58 PM

    2D Bounding Rectangle over a 3D model

    Hi guys,

    I want to lay a 2D Rectangle at the same position where my 3D model is (x and y coordinates).

    My problem is following:

    I can't use the x and y coordinates of my model, because it's relativ to the camera.
    The coordinates of the rectangle refer to the screen resolution.

    I will use the rectangle to detect collisions with my 2D landscape.

    Has anyone a hint for me?
  • 10/27/2009 1:36 PM In reply to

    Re: 2D Bounding Rectangle over a 3D model

    Either drawing BoundingBoxes in 3D or use Viewport.Project to project your models 3D world space position into screen space. Heres an example using the 2nd method.
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 10/29/2009 5:08 PM In reply to

    Re: 2D Bounding Rectangle over a 3D model

    Hi, i tried this... but is there a way to use this with the standartcamera?

    If I try this, i get a negative X and Y-Coordinate

    protected override void Update(GameTime gameTime)
            {

                // Create a total bounding sphere for the mesh
                BoundingSphere totalbounds = new BoundingSphere();
                foreach (ModelMesh mesh in player.model.Meshes)
                {
                    totalbounds = BoundingSphere.CreateMerged(totalbounds, mesh.BoundingSphere);
                }

                // Project the center of the 3D object to the screen, and center the
                // sprite there
                Vector3 center = GraphicsDevice.Viewport.Project(totalbounds.Center,
                    cameraProjectionMatrix, cameraViewMatrix, Matrix.Identity);
                player.boundingRectangle.X = (int)center.X;
                player.boundingRectangle.Y = (int)center.Y;

                // Create a bounding box from the bounding sphere, and find the corner
                // that is farthest away from the center using Project
                BoundingBox extents = BoundingBox.CreateFromSphere(totalbounds);
                float maxdistance = 0;
                float distance;
                Vector3 screencorner;
                foreach (Vector3 corner in extents.GetCorners())
                {
                    screencorner = GraphicsDevice.Viewport.Project(corner,
                    cameraProjectionMatrix, cameraViewMatrix, Matrix.Identity);
                    distance = Vector3.Distance(screencorner, center);
                    if (distance > maxdistance)
                        maxdistance = distance;
                }


               



                UpdateMouse();
                UpdateMovement();
                
                base.Update(gameTime);
            }

    Thanks for helping,
    Robin
  • 10/31/2009 12:57 AM In reply to

    Re: 2D Bounding Rectangle over a 3D model

    jadephoenix1988:
    Hi, i tried this... but is there a way to use this with the standartcamera?

    If I try this, i get a negative X and Y-Coordinate


    What is 'the standartcamera'?

    What is the X and Y Coordinate - is it player.boundingRectangle.X and Y?
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 10/31/2009 2:08 AM In reply to

    Re: 2D Bounding Rectangle over a 3D model

    By if standard camera you mean View and Projection matricies, yea, you can do it...

    // This is a scale factor that depends on a lot of things, use guess and check to get it just right
    const Single SOMEFACTOR = 1f;

    // Transform the object's location to 2D space
    Vector4 ScreenPosition = Vector4.Transform(ObjectLocation, View * Projection);

    // Ok, now we need to get a representation of how "large" this object appears on our screen...
    Single Radius =  Projection.M11 * ScreenDimensions.Y / ScreenPosition.W;

    // Ok, now we know how far this object extends from ScreenPosition.X, ScreenPosition.Y but our would might be scaled differently
    Radius = Radius * SOMEFACTOR;
    Madness? This is SUNSHINE.
  • 11/2/2009 4:03 PM In reply to

    Re: 2D Bounding Rectangle over a 3D model

    Hi andorov!

    If I use Vector4.Transform, I get a x-coordinate with the value 0 and y with 36.... but the "movement" of my boundingrect is like the movement of the mesh... I'll try to find the fault!

    greetz,
    Robin
  • 11/3/2009 7:55 AM In reply to

    Re: 2D Bounding Rectangle over a 3D model

    Hey :-)! It worked with project() :)))
Page 1 of 1 (7 items) Previous Next