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

Convert 3d coordinates to 2d coordinates

Last post 9/27/2009 8:21 AM by Duckocide. 10 replies.
  • 6/24/2008 2:36 PM

    Convert 3d coordinates to 2d coordinates

    Hi, im trying to made a spaceship game and i need to draw a sprite in a target, i need to know how create a vector2 from a vector3 to draw the sprite on the enemy ship, thnx

     

    P.D.: This code not work

    GraphicsDevice.Viewport.Project(position, projectionMatrix, viewMatrix, worldMatrix);

    position is a Vector3

    worldMatrix is created from the quaternion of rotations and vector3 position of the model

    thnx a lot

  • 6/24/2008 2:55 PM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    I use the same method and it works for me. From your sample code, it seems like your not using the return value. Did you just forgot it? Because the Project Function returns a Vector, representing the given position Vector(First argument) in screenspace.

    Otherwise it would say your given Matrix are wrong. Try first only a projection matrix, and an Identity Matrix as View and World.

    The position must be in Object Space, if you supply a worldMatrix, otherwise it must be in World Space, and the worldMatrix needs to be the Identity Matrix.

  • 6/24/2008 3:11 PM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    i have made this method

             public Vector3 coordinates3dTo2d(ExtendedModel ex, camera c)
            {
                Matrix worldMatrix =
                   Matrix.CreateScale(ex.scales) *
                   Matrix.CreateFromQuaternion(ex.rotations) *
                   Matrix.CreateTranslation(ex.position);
               return GraphicsDevice.Viewport.Project(ex.position, c.projectionMatrix, c.viewMatrix, worldMatrix);
            }

     

    and then:

                    Vector2 pos = new Vector2(coordinates3dTo2d(model, cam).X, coordinates3dTo2d(model, cam).Y);
                    sb.Draw(m.tex,pos, Color.White);

    it draws the sprite but not on the model

    i dont know what is object space and world space , jejej, explain me

    thnx


  • 6/24/2008 3:45 PM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    Lets say you created a Model in your Model Editor. Just a plain Box which extends in any direction to 1. So you have Min from (-1, -1, -1) and Max to (1, 1, 1) These Coordinates are called "in model space" if you move the box now in your world, lets say 5 units to the right. In Model Space, Min and Max stay the same, but in World Space they change, because we moved the box Min (4, -1, -1) and Max (6, 1, 1) To actually move the box, we transform it from one space to another. These Coordinates are now in World Space, because we just use the World Matrix to transform it from Object to World Space. The Same happens with the Viewmatrix and the Camera Space and the projection Matrix and the Screen Space.

    So now it is important to now. If you place your Marker in Screen or in Object Space? Lets say you want a Arrow pointing down on your enemy Ship, I would give the Arrow coordinates in the object Space from your Ship, lets say the Ship has the dimension of the box i mentioned earlier, i would place the Arrow in the Middle of the Ship but slightly on top of it, so you would have coordinates like (0, 1.5, 0) ... The Project method now needs the worldMatrix of the ship, to transform the position(which is in model space) into world space. If the position is already in world space, of course we don't need to transform into world space. But if you do it, the result is of course completly wrong.

     

    Your First Method doesn't make any Sense. You need a point for your Marker, but it seems you first translate it and then project it. This is not correct.

    What you need is:

    a Vector3 you want to project into screenspace. Lets call it (MarkerPosition)

    a World, View and Projection Matrix.

    The World Matrix is there to Transform your MarkerPosition from Object to World Space. If it is already WorldSpace you set the WorldMatrix as Matrix.Identity.

  • 6/24/2008 9:50 PM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    thnx, this help me and now works, bue i have another problem, the things behind the camera, the objects that the player isnt facing is draw too

     how i can detect what things are behind me, thnx a lot

  • 6/24/2008 9:59 PM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    i have solve this!!!, the value of the z of the project method must be < 1 ^^, thnx!
  • 1/19/2009 2:39 AM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    I'm having a similar issue to this.  Can you post your corrected code for me to review? 
  • 2/16/2009 9:26 PM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    I too had this problem.  It was very upsetting that there was no response here.  So I am making one.



    you need:
    Vector3 point - this is your world space point
    Matrix world - this is the world matrix of your point
    Util.Math.CreateWorldMatrix(Position, Rotation, Scale) 

    Viewport wholeViewport - this is your viewport for your game
    Camera camera -  this should have a projection and view matrix for the camera

            private Vector3 Project(Vector3 point, Viewport wholeViewport, Camera camera, Matrix world) 
            { 
                Vector4 mp = Vector4.Transform(new Vector4(point, 1.0f), Matrix.Invert(world)); 
                Vector3 pt = wholeViewport.Project(new Vector3(mp.X, mp.Y, mp.Z), camera.Projection, camera.View, world); 
                return pt; 
            } 

    when you draw, be sure to insert an if statement to only draw the objects in front of you
    if (ScreenSpaceLocation.Z < 1.0f) 
      SpriteBatch.Draw(Image_planetMarker, new Vector2(ScreenSpaceLocations.X,ScreenSpaceLocations.Y), Color.White); 


  • 4/5/2009 7:23 PM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    I'm using the project method as well. Works just fine for me, all except i have the 2 sprites showing up. One in front and one behind me. I've tried using the if statement you've used (if Z <1.0f) but my problem is that my Z coordinate coming from the return of the project method is ALWAYS 0.0f Anybody know what i'm doing wrong here?
  • 9/25/2009 4:47 PM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    imlladris:
    I'm using the project method as well. Works just fine for me, all except i have the 2 sprites showing up. One in front and one behind me. I've tried using the if statement you've used (if Z <1.0f) but my problem is that my Z coordinate coming from the return of the project method is ALWAYS 0.0f Anybody know what i'm doing wrong here?


    Try doing the check on the vector before you use the project method.  That should do the trick.
  • 9/27/2009 8:21 AM In reply to

    Re: Convert 3d coordinates to 2d coordinates

    Using the above mentioned built in mechanisms to convert 3D co-ordinate to 2D is correct and I use the "Project" mechanism a lot in my game matrices.

    It has not been mentioned, but if you want to convert a 3D co-ordinate to 2D (and you are not worried about cameras/matricies etc. I.e. Z straight through the screen), the math is very simple.

    2d.x = (3d.x * fov) / 3d.z
    2d.y = (3d.y * fov) / 3d.z

    I.e. A simple vanishing point calculation with a pre-defined "field of view" constant (a big number normally). The high the Z value, the closer to the vanishing point 2d.xy tend.

    (You would need to add the centre of your sprite draw area to 2d.x and 2d.y)

    I know it will not help if you need world, view and projections matrices for say a heads-up display based on player orientation. I thought it worth throwing something simple in to the thread for he record.
    Resplicing some XNA...

    Play: Rail Gun Charlie (2009)

    Play: Tail Gun Charlie (2008)

    Duckocide
Page 1 of 1 (11 items) Previous Next