Posts: 21 Technical: 9 Comments: 2

Dont trust Viewport

by: kobaj at 2010-03-08 17:19:59

While I love to sit and program for hours on end, I will only be able to do so if what I am programming is interesting, unique, and dynamic. Working on the same $#&*#(* class variable left myself crazy. But I learned a very valuable lesson, check out after the break of how I tamed the beast Viewport. (for XNA)
Mostly GraphicsDevice.Viewport works as anyone would expect. It contains some nifty variables such as width, and height, to help you with programming your drawing space's well...width and height. It also has some nice methods, two of the most famous are Viewport.Project and Viewport.Unproject. Which translate a point in 3d space and make it 2d and visa versa.

I had an issue most recent with my map maker with the Viewport.Unproject method, for some reason I would get estranged results. Some narrowing down provided me with the knowledge that the width of Viewport, and my own variable Width where of two different values. If I put in an IF statment...


if (Viewport.Width != Width)
{
throw new Error();
}

It SHOULD have thrown an error, but it didn't! Yet if I paused the debugger, the width was still incorrect!

Failing to figure out why this strangeness was happening, I decided it best not to argue and wrote my OWN Unproject method. It wasn't pretty and didn't give me the distance to the object like viewport.unproject did (in hindsight though, I think if I had normalized the vector, I would have been able to get distance...crap). Regardless, it worked somewhat, but I grew tired and wanted the Viewport.Unproject again.

Late late at night, it came to me. What if I could declare Viewport?!


Viewport mine = new Viewport();
mine.Width = this.Width;
mine.Height = this.Height;
GraphicsDevice.Viewport = mine;

ANGLES SANG OUT! It worked, in only 4 lines of code, it worked!

So what did I learn? Fuck Viewport.
Comments: 0
Name*:
Email* (wont be shown):
Post*:

* Denotes required field. URLs are not allowed in comment post.

CopyRight Kobaj Code and Design 2009 - 2010