Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Vector3.Project Question

Started by ironwill96 Jan 15, 2005 at 2:29 PM 1 replies 2.9k views
Original Post
ironwill96
ironwill96
Hey again all, I've used the search function and looked over other Vector3.Project questions/answers and havent been able to figure out why what i'm doing is not producing desired results. The relevant code is shown below and i'll ask my question about it afterward:

this.boundingRectangle.X = -15;
            this.boundingRectangle.Y = 0;
            this.boundingRectangle.Width = 15;
            this.boundingRectangle.Height = 15;

            float upperLeftX, upperLeftY, upperRightX, upperRightY,
                lowerRightX, lowerRightY, lowerLeftX, lowerLeftY;
            Vector3 pointVect = new Vector3();

            //for each corner of the rectangle, project that point onto the screen to make a new rectangle
            for (int i = 0; i < 4; i++)
            {
                switch (i)
                {
                    case 0:
                        //init x/y to upper left
                        upperLeftX = this.boundingRectangle.X;
                        upperLeftY = this.boundingRectangle.Y;
                        pointVect = new Vector3(upperLeftX, upperLeftY, 1.0f);
                        break;
                    case 1:
                        //init x/y to upper right
                        upperRightX = this.boundingRectangle.X + this.boundingRectangle.Width;
                        upperRightY = this.boundingRectangle.Y;
                        pointVect = new Vector3(upperRightX, upperRightY, 1.0f);
                        break;
                    case 2:
                        //init x/y to bottom right
                        lowerRightX = this.boundingRectangle.Width;
                        lowerRightY = this.boundingRectangle.Y + this.boundingRectangle.Height;
                        pointVect = new Vector3(lowerRightX, lowerRightY, 1.0f);
                        break;
                    case 3:
                        //init x/y to bottom left
                        lowerLeftX = this.boundingRectangle.X;
                        lowerLeftY = this.boundingRectangle.Y + this.boundingRectangle.Height;
                        pointVect = new Vector3(lowerLeftX, lowerLeftY, 1.0f);
                        break;                  
                }

                Viewport vpt = new Viewport();
                vpt.X = 0;
                vpt.Y = 0;
                vpt.Width = this.device.PresentationParameters.BackBufferWidth;
                vpt.Height = this.device.PresentationParameters.BackBufferHeight;
        
                Vector3.Project(pointVect, vpt, this.device.Transform.Projection,
                    this.device.Transform.View, this.device.Transform.World);
            
                switch (i)
                {
                    case 0:
                        //we just figured out the upper left in screen space
                        this.screenRectangle.X = (int) pointVect.X;
                        this.screenRectangle.Y = (int) pointVect.Y;
                        break;
                    case 1:
                        //we just figured out the upper right in screen space
                        this.screenRectangle.Width = ((int) pointVect.X) - this.screenRectangle.X;                        
                        break;
                    case 2:
                        //we just figured out the bottom right in screen space                        
                        this.screenRectangle.Y = ((int) pointVect.Y) - this.screenRectangle.Y;
                        break;
                    case 3:
                        //we just figured out the bottom left in screen space
                        //we should already have all of the points figured out (X, Y, Width, Height)
						//so this point is already mapped using that data..do nothing
                        break;                  
                }
            }          

The basic issue is that i'm not getting correct (expected) results from the Projected points. What i'm trying to do is take a Unit's (this is for an RTS game i'm working on) bounding-box base rectangle and Project it from World Space onto the screen so that I can use this to do drag-selection (figure out if the projected box lies within the drag box). For now I just plugged in some starting values, but eventually the GetBaseRectangle() method will provide the base rectangle for the current unit (where this code is being run at). Any suggestions as to why this doesnt work correctly, or a better way to do this would be greatly appreciated. I'm also not sure if this data should be updated every frame for every unit - it seems that this would cause major performance issues. Thanks in advance, Nathan NEW EDIT: I put in updated code that shouldn't have as many bugs in it. However, it is still telling me that my Projected points are identical to my unprojected points. It seems like the Vector3.Project method is doing nothing at all! [Edited by - ironwill96 on January 15, 2005 3:47:41 PM]
"There are those who said this day would never come, what do they have to say now?" - Halo 2 Trailer
_Flecko
_Flecko
Unlike Vector3's instance methods, the static methods do not modify the vector in place. They return a new Vector3 without modifying the original. The same is true of Vector2 and Vector4.
--------------Trans2D - 2D library for C# / Managed DirectX
ironwill96
ironwill96
THANK YOU!

That fixed the issue as well as the problems I used to have with Vector3.Unproject. I guess it pays to pay better attention to the method signatures in the DirectX 9.0 Documenation :-)

Nathan
"There are those who said this day would never come, what do they have to say now?" - Halo 2 Trailer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.