Jump to content
  • Advertisement
Sign in to follow this  

[XNA] Slight confusion with CreatePerspective and BoundingFrustum

This topic is 2780 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

Hey everyone,

I am currently working on the camera code of my 2D game project. Now, although I have said "2D", entities have a 3D location, which is to be used both for a free parallax effect, as well as gameplay. I initially had an orthographic projection set up, in the following way :

mProjection = Matrix.CreateOrthographic(mRenderer.backBufferWidth * mDistance, mRenderer.backBufferHeight * mDistance, mNear, mFar);

The code is quite explicit, apart from mDistance maybe. This variable simply represents the distance/scaling factor of the camera, and otherwise expands the projection range to show a bigger part of the screen (in essence, zooming in and out).

I have switched this method to using "CreatePerspective" in the following way :

mProjection = Matrix.CreatePerspective(mRenderer.backBufferWidth, mRenderer.backBufferHeight, mNear, mFar);

The mDistance variable was moved to the actual view matrix computation, which is why it does not appear here. Although it works "visually", I found that the BoundingFrustum I compute for my Camera is no longer valid :

mFrustum.Matrix = this.view * this.projection;

The frustum is used for the culling of my entities, and the algorithm will work with the original orthographic projection, and perspective projection matrices defined in the following way :

mProjection = Matrix.CreatePerspective(1.0f, 1.0f/1.33f, mNear, mFar);

I am slightly confused, and failing to see why my solution fails : shouldn't I be allowed to project coordinates as I like. Essentially, with my setup, I am trying to map a unit to a single pixel. In other words, I would like an object of size 64*64 to be exactly 64*64 pixels on the screen, when it sits on the near plane.

Where have I gone wrong with my thinking process?

Your help and explanations would be appreciated biggrin.png !

Share this post

Link to post
Share on other sites
All pixels on the screen will be stretched over a planar projection with the specified field of view and aspect ratio.
To get the same resolution as the texture, you would need to use a fixed screen resolution or zoom out in higher resolutions.
Using almost the same resolution at a fixed distance and field of view will still look very sharp.

Share this post

Link to post
Share on other sites

Thanks for your reply.

I have found a temporary solution to my frustum problem. I say temporary, because even though it works and is stable, I am not happy with the overall implications/code. Essentially, I adapt the far plane according to the camera distance, because by using a fixed constant, it seems to stretch the far plane out too much, and include entities which are otherwise within the frustum, but not actually visible :

float adaptiveFar = mDistance + 1.0f;
mProjection = Matrix.CreatePerspective(mRenderer.backBufferWidth, mRenderer.backBufferHeight, mNear, adaptiveFar);

This solution outputs a valid frustum, and also provides me with a more suitable far plane distance, which is proportional to the zooming level I require for my game.

I am still open to explanations as to why my frustum is invalid when using a constant value biggrin.png .

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!