Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Synopsis: What I have is an object rendered on the screen. I need to calculate the top-left / bottom-right of it's location on the screen in Screen Coordinates.

Explanation: The object is basically a box that I'm trying to draw sprite textures into at specific locations, but due to the screen resolution being a variable (dependent on player settings), I can't hard-code in these translations. So what I need to do is find out what the bounding box of the object is in Screen-Coordinates. Once I know the bounding box, I can calculate the offsets to the positions within it.

I can calculate the center position of the box already with a function I scavenged:

public Vector2 GetScreenPoint(Matrix objMatrix)
{
    Matrix mat = objMatrix * mCamera.mViewMatrix * mCamera.mProjMatrix;
    Vector4 v4 = Vector4.Transform(new Vector3(0, 0, 0), mat);
    return new Vector2((int)((v4.X / v4.W + 1) *    
        (pGame.GraphicsDevice.PresentationParameters.BackBufferWidth / 2)), 
        (int)((1 - v4.Y / v4.W) * 
        (pGame.GraphicsDevice.PresentationParameters.BackBufferHeight / 2)));
}

But now I need to go from that to the actual extents of the bounding box. I've tried converting the BoundingSphere of the model into a BoundingBox and then translating the object matrix by the BoundingBox.Min values, but I'm nowhere near the left side of the object.

Am I going about this backwards, or what am I not seeing? This would seem a simple enough thing to do, but I just can't figure it out. Or is there a complete different way I should be approaching this?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Instead of using new Vector3(0, 0, 0) do that calculation for the local space position of each corner of the cube, and then find the minimum and maximum of those 2D positions. Those are the corners of the 2D bounding box for the 3D object.

share|improve this answer
    
I've tried what you suggested with no luck still. There must be something fundamentally wrong with what I'm doing, I just can't see it. –  AzHole Jun 1 '13 at 18:22
    
I did some reworking and built my own bounding boxes around the meshes and then translated them as you suggested above, which DID work. The problem was with erroneous boxes unfortunately. Thank yah Adam! –  AzHole Jun 2 '13 at 18:50
add comment

Your Answer

 
discard

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.