Xna Equivalent to Viewport.Unproject in draw call as matrix transformation

I am doing 2D scrolling and I would like to draw my sprite in world space instead of client space, so I don't have to lock it in the center of the screen, and when the camera stops, the sprite will go off the screen instead of getting stuck in the center. For this, I wanted to make a transformation matrix that goes in my callback.

I've seen something like this: XNA ViewPort and SpriteBatch

I saw that Matrix.CreateOrthographic () is used to navigate from Worldspace to client space, but how can I use it to navigate from client space to worldspace?

I was going to try and put my results from the viewport.unproject method I have in the scale matrix, for example:

blah = Matrix.CreateScale(unproject.X,unproject.Y,0);

      

however, it doesn't seem to be working correctly.

This is what I call in the draw method (where X is the coordinate that my camera should follow):

 Vector3 test = screentoworld(X, graphics);
 var clienttoworld = Matrix.CreateScale(test.X,test.Y, 0);
 animationPlayer.Draw(theSpriteBatch, new Vector2(X.X, X.Y),false,false,0,Color.White,new Vector2(1,1),clienttoworld);

      

Here is my code in my unproject method:

screentoworld(Vector2 some, GraphicsDevice graphics){

 Vector2 Position =(some.X,some.Y);
 var project = Matrix.CreateOrthographic(5*graphicsdevice.Viewport.Width, graphicsdevice.Viewport.Height, 0, 1);
 var viewMatrix = Matrix.CreateLookAt(
      new Vector3(0, 0, -4.3f), new Vector3(X.X,X.Y,0), Vector3.Up);

//I have also tried substituting (cam.Position.X,cam.Position.Y,0) in for the (0,0,-4.3f)

 Vector3 nearSource = new Vector3(Position, 0f);
 Vector3 nearPoint = graphicsdevice.Viewport.Unproject(nearSource,
            project, viewMatrix, Matrix.Identity);

 return nearPoint;  
}

      

+3


source to share





All Articles