I am making a 2D sidescroller and I would like to draw my sprite to world space instead of client space so I do not have to lock it to the center of the screen and when the camera stops the sprite will walk off screen instead of being stuck at the center. In order to do this I wanted to make a transformation matrix that goes in my draw call.
I have seen something like this: https://stackoverflow.com/questions/3570192/xna-viewport-projection-and-spritebatch
I have seen Matrix.CreateOrthographic() used to go from Worldspace to client space but, how would I go about using it to go from clientspace to worldspace?
I was going to try putting my returns from the viewport.unproject method I have into a scale matrix such as:
blah = Matrix.CreateScale(unproject.X,unproject.Y,0);
however, that doesn't seem to work correctly.
Here is what I'm calling in my draw method(where X is the coordinate 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:
Vector3 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;
I would also like to be able to move the camera around without moving my sprite. Before I was doing something like:
if(directionofsprite == positive && sprite position is not less than 200)
{
sprite-=5;
map-=5;
}
This would make it appear as though the camera is moving to the right. I just want to simplify all of this by making my sprites draw to worldspace.