Archive for July, 2005

The Beginnings of a Useful Camera

Sunday, July 31st, 2005

By now I am sure some of you are thinking, “Great, I can draw stuff, but how am I supposed to go around and look at it once it is drawn!?!” Maybe not with that much excitement, but I’m sure someone was thinking it. Anyway, the solution to this problem requires two things. A Camera class and an Input class that will let us tell the camera what to do. If you aren’t familiar with trigonometry and polar coordinates, then you should try to brush up on those concepts before attempting to learn much from this and the following tutorial. I wrote a camera class a while back that had an explanation of the math involved here.

I will be implementing this camera a bit differently since it will be both 1st and 3rd person, but the math involved is the same. If you think you know enough trig to get through this, or you have a new understanding of if after having read my article, then here we go!

Step 1: Implementing the Basic Camera Framework

Start out by creating a new class in your 3D engine project and name it something with the word Camera in it. Mine is going to be HMCamera. Since we are going to be using this camera to control all the aspects of our screen display, and possibly for some fun zoom effects or the like later, I figure we should store all of the variables that we used to initialize the camera in our System class earlier so this one class will give us full control over all of those aspects of our games. Here is the basic class with its member variables and a few useful properties:

public class HMCamera { private Vector3 position = new Vector3(0, 0, 0); // These are the most important values in private Vector3 target = new Vector3(0, 0, 1); // the camera class, and they are what private Vector3 upVector = new Vector3(0, 1, 0); // we make our final View matrix with private int screenWidth; private int screenHeight; // These values tell the camera how wide our view angle is and how close/far we can see private float nearClip = 1.0f; private float farClip = 100.0f; private float fov = (float)Math.PI / 4.0f; // We will use these in the math processes that control movement later private float hRotation = (float)Math.PI / 2.0f; private float vRotation = 0.0f; private float radius = 1.0f; // Public matrix properties that our device can use to set its own matrices before rendering public Matrix World { get { return Matrix.Identity; } } public Matrix View { get { return Matrix.LookAtLH(position, target, upVector); } } public Matrix Projection { get { return Matrix.PerspectiveFovLH( fov, (float)screenWidth / (float)screenHeight, nearClip, farClip ); } } public HMCamera(int width, int height) { screenWidth = width; screenHeight = height; } }

Now that we have a basic camera put into place we need to add the code in the main system part of our engine to use it. First we need to add our member variable for the camera and a public property so we can access it outside the main system, and add an initializer into the constructor of the System as well.

private HMCamera myCamera; public HMCamera MyCamera { get { return myCamera; } } // In the constructor if(InitializeGraphics()) { myScene = new HMScene(); myCamera = new HMCamera(this.Width, this.Height); myInput = new HMInput(this); }

The next thing we can do is delete the SetupCamera() function that created our matrices in the system class and replace it with a new one in the camera that looks like this:

private void SetDeviceCamera() { myDevice.Transform.World = myCamera.World; myDevice.Transform.View = myCamera.View; myDevice.Transform.Projection = myCamera.Projection; }

Now add a call to the SetDeviceCamera() function at the beginning of our system’s main Render function.

Microsoft Found Me

Saturday, July 23rd, 2005

Since most of the people don’t see the comments that get posted in the 3D Engine section of this lovely blog, I thought I’d repost one of them here since I am a bit proud of it. Here ya go:

What up? I’m a Missoula kid who just landed a job at Microsoft and I was scanning through the tons and tons of distribution lists they have here and I came across a link to this set of tutorials. Good to see another montanaian… montuckian… montuckianan… guy from montana gettin’ some recognition. Just thought I’d let you know your blog’s getting some notice. Check yah.

Guess I really am as good at this kind of thing as I thought I was, or at least there is someone else in the world who seems to think so too :)

Thank God for the Wendy’s Dollar Menu

Saturday, July 9th, 2005

Not only the Wendy’s Dollar Menu, but good bathroom design as well. Now for a bit of an explanation. All day yesterday I felt completely shitty, nauseous, and tired. I didn’t know what the deal was, because I never get sick. The only thing I could think was food poisoning, but it was my favorite restaurant, how could I get food poisoning from them!?!?

Anyway, after about 20 minutes of rolling around in bed trying not to puke, I just gave up and went and sat in the bathroom and waited for it to come. After 20 more minutes of nothing happening, I decided to get in the shower and try to help the general soreness in the rest of my body when what I had waited for finally happened. This is where the thank God for good bathroom design comes in. I was able to lose my breakfast/lunch/dinner entirely into the toilet without making any sort of mess and without even having to put one foot outside the shower. Nice work Mr. Architect on that one.

Let me just tell you a hot shower feels really good immediately after throwing up, especially when you just happen to already be in there and you always have toothpaste/toothbrush on the shelf. Bad taste and most of the bad general sick feeling goes away almost instantaneously. I guess it still isn’t as bad as the apendix troubles Devon is having as you can probably tell since he hasn’t been able to post about it yet.

For a final thought, some advice. If you are going to puke a day’s worth of food, make sure it was from the dollar menu. My brother-in-law at one time was not so smart with his alcohol consumption after a very expensive steak dinner one night and managed to get only about 20 feet from the restaurant door when he wasted the whole cost of the evening in about 30 seconds. So Wendy’s thanks you much. Your place with Mr. Architect will be very high indeed in my mind.