Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > XNA

XNA Tutorials and support for game development using Microsoft's XNA Game Studio framework.

Reply
 
Thread Tools Display Modes
Old 11-05-2007, 02:07 AM   #1 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default Tutorial 1 - Basics

What’s needed?
Visual C# Express (Yes, express not Visual Studio)
Visual C#

Visual C# Express Service Pack 1 (VS80sp1-KB926749-X86-INTL.exe)
Download details: Visual Studio® 2005 Express Editions SP1

XNA Game Studio Express 1.0 Refresh
Download details: Microsoft XNA Game Studio Express 1.0 Refresh

Additional Comments:
For these tutorials I am assuming you have at least a beginner to intermediate understanding of C#.
I also am assuming you’ve download and installed the items below, and have the project for this tutorial open in Visual C#.

Some thing first before I begin, I love to explain things with analogies. I love when things are presented this way to me, so I will bring the same to you whenever necessary. The other thing I love to do is to just jump right in and start playing with stuff. I don’t learn well from books because I’m too much of a “hands on” type of person. Everything I’ve learned about XNA and pretty much programming in general I’ve taught myself with very little reading. I learn best from example and trial and error. So without further ado, let’s jump into so code.

In this tutorial I will be showing you how to draw a simple triangle on the screen. This may sound dumb, and you may be thinking “I want to do something more fun”. Well, we will, but be patient because I assure you without the stuff I explain in this tutorial, you will be lost in the more heavy stuff I go over later on.

Ok, so a game loop in itself is extremely simple and basically looks somewhat like this.

Code:
	     while (game.Running)
            {
                Update();
                Draw();
            }
Pretty simple, right? There are some other things that really go on behind the scenes but most of that important stuff is taken care of by the XNA framework so that you don’t have to bother with it. The basics of 3d rendering for games is that everything is really a triangle. So when you are playing your favorite 3d game, the graphics card is really rendering tons of small triangles to the screen with textures or colors applied to them. The more triangles you render the better your character, building, vehicle, etc. appear to look. The side effect to this is the frames per second at which the video card can draw these triagles drops. So for this tutorial I will be showing you how to draw a simple triangle to the screen. So lets jump into the code.

Lets you look at line 14
Code:
	public class BasicsGame : Microsoft.Xna.Framework.Game
Notice we are inheriting from the Xna frameworks Game class. We do this because Game already contains the basics of a game engine. It has a render loop that we can override called Update and Draw. The basic Xna project, when created, contains 2 fields
Code:
	GraphicsDeviceManager graphics;
	ContentManager content;
These fields are extremely important to us. GraphicsDeviceManager contains our graphics device which will be used to draw objects to the screen. ContentManger is a resource management class that will help us load models, textures, shader, etc. in later Tutorials. In the code I’ve provided you will also notice a few more fields.

Code:
	//These are all the basic variables we need
        //to draw a 3d object to the screen.
        //Most of these are pretty self explanitory.
        //Those that aren't will be explained along the way.
        VertexDeclaration vertDec;
        VertexPositionColor[] vertices;
        BasicEffect effect;
        Matrix world;
        Matrix view;
        Matrix projection;
        Vector3 position;
        float yaw;
        float pitch;
        float roll;
        float scale;
        int[] indices;
Basically I’ve commented the entire file to explain what things are and what they do along the way. No real need to do it twice . But these are all important and you will need all of these for every 3d object you draw to the screen. The only exception to some of this is that they can be done in different methods. For instance, yaw, pitch, roll can be summed up with a class proviced by the Xna framework called Quanternion. The reason I didn’t include this though, is it isn’t as easy to understand.

Anyway enough babbling, lets look at more code.
Code:
	protected override void LoadGraphicsContent(bool loadAllContent)
This is also another important overridable part to Xna. This is called for every single game component, including the engine during the initialization of the game. It is also called whenever the graphics device gets reset or is lost. This can happen whenever you resize the screen, minimize it, or enter full screen mode. You should reinitialize al lthe objects in your game whenever loadAllContent is true. This means the graphics card was completely lost and was recreated. Thus all objects using it need to be recreated as well.

Well, there really isnt a whole lot more I need to point out, that isnt already explained in the code itself. A lot of the things in the code that are explained also have examples of how they effect the game itself, the only one of these I wanted to go over in more detail is the projection matrix on line 153

Code:
	projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi / 				 3.0f, graphics.GraphicsDevice.Viewport.Width / 				 graphics.GraphicsDevice.Viewport.Height, 1, 1000);
I explained in the code that this is how First Person Shooters create a zoom effect with certain guns. Basically this messes with the view area of the current view point. When I watch how some people try to zoom, what they do is move the camera closer to the object they want to view, this is wrong. You want to decrease your projection of what you are viewing, this will give you a zoom effect. If you want to see what im talking about, run the program one time as its written, then change the 3.0f to something like 10.0f. This will look like you are REALLY close to the object when really you are the same distance away, but looking at it from a smaller perspective.

Well that’s about all I have for this tutorial, I will start working on a new one soon. If anyone has any questions, suggestions, or comments please feel free to leave them here. I will be happy to answer, any and all of them when I get a change.
Attached Files
File Type: zip Tutorial 1 - Basics.zip (15.7 KB, 19 views)
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO

Last edited by Jeff; 11-05-2007 at 11:50 AM.
Jeff is offline   Reply With Quote
Old 11-05-2007, 05:58 AM   #2 (permalink)
Forum Expert
 
Join Date: Dec 2005
Location: Germany
Age: 31
Posts: 401
Default

Neat.

Bugreport: in Main() you're using (Game1 game = new Game1()), while your game class is named BasicsGame. Either - or.
Setharnas is offline   Reply With Quote
Old 11-05-2007, 11:50 AM   #3 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Quote:
Originally Posted by Setharnas View Post
Neat.

Bugreport: in Main() you're using (Game1 game = new Game1()), while your game class is named BasicsGame. Either - or.
Ah ya, thanks.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 11-05-2007, 02:30 PM   #4 (permalink)
Forum Expert
 
Join Date: Jul 2003
Location: Arizona
Age: 18
Posts: 3,149
Send a message via AIM to Storm33229 Send a message via Yahoo to Storm33229
Default

Definitely a helpful tutorial, I'm wasn't at home at the time of veiwing it, so I haven't been able to open up the project yet and start playing around with things you have taught, but I read through it and understood most of it.

The main things that confused me are:
Code:
                //The rotating of our primitive
                //Yaw is the Y axis, so it rotates left and right(front to back).
                //Pitch would be X axis and rotate up and down
                //Roll would be Z axis and rolls left and right(top to bottom)
I suppose that it would make more sense to me if it read:
Y = up and down
X = left and right
Z = front to back

and
Code:
       //Scale of our primitive. 1 = 100% of the original size. 
                //2 would be 200% and so on
                scale = 1f;
I under stand everything here...but...why is there an 'f' after '1'? and I still don't really understand the concept of a matrix. Help?
__________________
Quote:
Originally Posted by Radwen
You should crush your pills and sniff them.
Storm33229 is offline   Reply With Quote
Old 11-05-2007, 02:38 PM   #5 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hiding in your room.
Age: 21
Posts: 1,272
Send a message via MSN to arul
Default

1f means that its float. If the variable is float by itself, it can be omitted, though it can be useful in some cases(for example to avoid an integral division).
__________________
arul is offline   Reply With Quote
Old 11-05-2007, 02:40 PM   #6 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Quote:
Originally Posted by Storm33229 View Post
Definitely a helpful tutorial, I'm wasn't at home at the time of veiwing it, so I haven't been able to open up the project yet and start playing around with things you have taught, but I read through it and understood most of it.

The main things that confused me are:
Code:
                //The rotating of our primitive
                //Yaw is the Y axis, so it rotates left and right(front to back).
                //Pitch would be X axis and rotate up and down
                //Roll would be Z axis and rolls left and right(top to bottom)
I suppose that it would make more sense to me if it read:
Y = up and down
X = left and right
Z = front to back

and
Code:
       //Scale of our primitive. 1 = 100% of the original size. 
                //2 would be 200% and so on
                scale = 1f;
I under stand everything here...but...why is there an 'f' after '1'? and I still don't really understand the concept of a matrix. Help?
1f represents a float. if you say 1, it can be ambiguous from int and float. same as something like 1.01, however, i believe 1.01 would throw a compiler error and say it cant implicitely compile 1.01 from a double to a float.

As far as matrix stuff goes, i don't think its important to understand what exactly they do at this point. Just more you need to know why, Basically all the information in a matrix tells the graphics card how to draw each vertex. So if you have a Matrix.Identity, the graphics card applies this matrix to the vertex data and it wont change because Matrix.Identity is like multiplying 9 * 1. The answer is 9 cause nothing changes. Matrix math is quite confusing. The point here is to not try and understand it to much but just remember the steps you need to take to get a proper matrix to apply to the shader effect.

As for this
Quote:
I suppose that it would make more sense to me if it read:
Y = up and down
X = left and right
Z = front to back
This is wrong. My explination is right, Y is not up and down. Take a pencil and tape a piece of paper to it, hold the pencil by the eraser with the tip of the pencil facing the ground and twist your fingers, this is the effect of rotating on the Y axis, the paper moves left and right, not up and down...same applys to all other axis. Basically when you drive your car, you are turning left and right on a Y axis, if you were in a plane and turn left and right the plane rotates on its X axis in the air. If you look up Yaw Pitch Roll and how they apply to a plane, you will see exactly how it works...infact Ill post a picture



This isnt the best example but you can sorta see what i mean.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO

Last edited by Jeff; 11-05-2007 at 02:52 PM.
Jeff is offline   Reply With Quote
Old 11-05-2007, 05:00 PM   #7 (permalink)
Forum Expert
 
Join Date: Jul 2003
Location: Arizona
Age: 18
Posts: 3,149
Send a message via AIM to Storm33229 Send a message via Yahoo to Storm33229
Default

Quote:
Originally Posted by Jeff View Post
1f represents a float. if you say 1, it can be ambiguous from int and float. same as something like 1.01, however, i believe 1.01 would throw a compiler error and say it cant implicitely compile 1.01 from a double to a float.

As far as matrix stuff goes, i don't think its important to understand what exactly they do at this point. Just more you need to know why, Basically all the information in a matrix tells the graphics card how to draw each vertex. So if you have a Matrix.Identity, the graphics card applies this matrix to the vertex data and it wont change because Matrix.Identity is like multiplying 9 * 1. The answer is 9 cause nothing changes. Matrix math is quite confusing. The point here is to not try and understand it to much but just remember the steps you need to take to get a proper matrix to apply to the shader effect.

As for this

This is wrong. My explination is right, Y is not up and down. Take a pencil and tape a piece of paper to it, hold the pencil by the eraser with the tip of the pencil facing the ground and twist your fingers, this is the effect of rotating on the Y axis, the paper moves left and right, not up and down...same applys to all other axis. Basically when you drive your car, you are turning left and right on a Y axis, if you were in a plane and turn left and right the plane rotates on its X axis in the air. If you look up Yaw Pitch Roll and how they apply to a plane, you will see exactly how it works...infact Ill post a picture



This isnt the best example but you can sorta see what i mean.
Never meant to imply that you were wrong, just that this was a new way of thinking for me. Thanks for the picture hahah! It really does help.
__________________
Quote:
Originally Posted by Radwen
You should crush your pills and sniff them.
Storm33229 is offline   Reply With Quote
Old 11-05-2007, 07:58 PM   #8 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Quote:
Originally Posted by Storm33229 View Post
Never meant to imply that you were wrong, just that this was a new way of thinking for me. Thanks for the picture hahah! It really does help.
I was more pointing out to you that the way you thought of it was wrong There is something misleading about that picture though, it should be rotated on the Y axies 90 degrees.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO

Last edited by Jeff; 11-05-2007 at 08:02 PM.
Jeff is offline   Reply With Quote
Old 11-05-2007, 08:06 PM   #9 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Would it help if I added a coordinate system to these tutorials? So in the game you would see something like this?


At all times?
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 11-05-2007, 09:00 PM   #10 (permalink)
Forum Expert
 
crackrat's Avatar
 
Join Date: Feb 2004
Location: colorado
Age: 47
Posts: 473
Default

Another quik easy way to remember the Cartesian Coordinate System is to put your right hand palm up and parallel to the default world view with your thumb extended. for terrains the defualt view is your top view looking down this is also considered the global coordinate system. When viewing a model it is usually the front view of the model, also known as the construction plane. The thumb points to x+, the finger to y+, and the palm to z+. if the positive x or y axis rolls toward you it is rolling in the positive direction away from you is in a negative direction. the z axis rolls positive clockwise and negatively counter clockwise.

Where it really gets screwed up is when you start viewing an object that due to movement or placement is no longer inline with the world view of the scene, in those cases you must remember the models construction plane and use your hand accordingly. the models construction plane will always add up to the total movement and rotational vectors.

Be aware the each individual feature or piece of any model may have its own distinct construction plane that may differ from the models collective construction plane. Usually it is a good idea to align the different construction planes to the collective construction plane, lol but thats way above the scope of the current teachings

interesting note from a graphics arts perspective when using 3dstudio(and may be true of other applications using this file format), when saving an model as an .3ds file it will twist the model, so that the accumulated construction plane matches the the global coordinates. this can lead to some unexpected results .
__________________
"Some Scientists claim that hydrogen, because it is so plentiful, is the basic building block of the universe. I dispute that. I say there is more stupidity than hydrogen, and that is the basic building block of the universe."
~ Frank Zappa
crackrat is offline   Reply With Quote
Old 11-06-2007, 10:38 AM   #11 (permalink)
Forum Expert
 
Join Date: Jul 2003
Location: Arizona
Age: 18
Posts: 3,149
Send a message via AIM to Storm33229 Send a message via Yahoo to Storm33229
Default

Quote:
Originally Posted by Jeff View Post
Would it help if I added a coordinate system to these tutorials? So in the game you would see something like this?


At all times?
I suppose its not necessary, I'll probably understand it more as we move along.
__________________
Quote:
Originally Posted by Radwen
You should crush your pills and sniff them.
Storm33229 is offline   Reply With Quote
Old 11-06-2007, 01:47 PM   #12 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Quote:
Originally Posted by Storm33229 View Post
I suppose its not necessary, I'll probably understand it more as we move along.
Well ill prolly create it jsut because, but ask all the questions you want, thats what this forum is here for, if you dont understand something, ask. Ill answer best I can.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 11-07-2007, 06:24 PM   #13 (permalink)
Forum Expert
 
FingersMcSteal's Avatar
 
Join Date: Mar 2006
Location: North East, England, UK
Posts: 827
Send a message via ICQ to FingersMcSteal Send a message via MSN to FingersMcSteal
Arrow

Quote:
Originally Posted by Jeff View Post
Would it help if I added a coordinate system to these tutorials? So in the game you would see something like this?


At all times?
Yes i think so, something small in the corner so i know which way 'I' am looking at it from, or which way the 3D world is.

Might be helpful if you could draw a grid going out into the distance to get some spatial awareness of where you 'The Cam' are and where the 'object' is in 3d space, that would help me greatly and might help with knowing which axis things are spinning on.

If i remember a good few years ago myself and some friends wanted to get into this sort of thing but the CPU power wasn't around in the 80's-90's... yeah i'm an old fart.

Good work tho, hope to see more of this stuff here.

** Edit **
Don't most app's use Z for height ?
FingersMcSteal is offline   Reply With Quote
Old 11-07-2007, 08:03 PM   #14 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Quote:
Originally Posted by FingersMcSteal View Post
Don't most app's use Z for height ?
Ya, and thats just oreintation, you can turn on the x axis 90 degrees and Z will indicate height.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5