Jump to content
  • Advertisement
Sign in to follow this  

Converting from using CreateOrthographicOffCenter() to CreatePerspective() or similar

This topic is 2101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I have an existing codebase that uses Matrix.CreateOrthographicOffCenter() to draw lists of primitives, which make up a very 2D undulating landscape.  
The code to create the matrix looks like:
return Matrix.CreateOrthographicOffCenter(      cameraPosition.X - screenSize.X / scaleFactor / 2f,      cameraPosition.X + screenSize.X / scaleFactor / 2f,      cameraPosition.Y - screenSize.Y / scaleFactor / 2f,      cameraPosition.Y + screenSize.Y / scaleFactor / 2f,      -10, 10); 
There is a scaleFactor to figure in zooming.  I never set a view matrix, leaving it to the default value.  This has all been working very well so far.
I've decided I need a 3D effect to further develop my game - I want the base of the landscape to appear closer to the camera.  I've been looking into using CreatePerspectiveOffCenter() instead, but I couldn't for the life of me figure out how to use it to create a similar view.  I was looking through Reimer's tutorials & found he usually used CreatePerspectiveFieldOfView() and a view matrix, so I tried with that.  I can actually get it rendering exactly what I want, just using magic numbers I don't really understand:
return Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), screenSize.X / screenSize.Y, 0.01f, 1000); 
and a view matrix:
return Matrix.CreateLookAt(new Vector3(cameraPosition.X, cameraPosition.Y, 870),      new Vector3(cameraPosition.X, cameraPosition.Y, -10), Vector3.Up); 
Not sure if you can see my highlighted numbers (1000 in the first code block, 870 in the second), but why do they have to be so big?  Why cant I just use a perspective of 0.001 to 10 like before?  Anyway, problems occur if I try to zoom my view, which is a feature built in to that original CreateOrthographicOffCenter() code.  If I try:
return Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45 + ((1 / scaleFactor) - 1) * 30), screenSize.X / screenSize.Y, 0.01f, 1000); 
there is another magic number (30).  The problem here is this multiplication is generally too little at small zoom levels, and too much at large zoom levels.  I think it increases exponentially instead of linearly.
So my question really is - how do I get 3d primitives displaying with perspective, instead of orthographically, but still keeping the same coordinate system (with zoom) my game currently uses?  Thanks for any help! smile.png

Share this post

Link to post
Share on other sites

Well I've kind of answered the question myself - I should of course use the view matrix to manage zooming, not the world matrix.  Turns out the way I've drawn my primitives is quite hacky, but hey, if it works, it works :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!