Jump to content

  • Log In with Google      Sign In   
  • Create Account

Purebe

Member Since 27 Jul 2010
Offline Last Active Mar 29 2012 09:26 PM

Topics I've Started

Drawing a 3-D grid?

12 March 2012 - 04:22 AM

Hey, I'm trying to really understand the idea of drawing a grid in 3D space, it seems fairly simple but on my own I'm just not getting it too well. I 've been able to only find a single source on the topic at all and it was just to give some code with no explanation. I can pour over this code if I have to in order to understand this concept but since it seems very important I'd like to understand it through and through and I was wondering if anyone knew of any good reviews of the topic?

Thanks! Here's the code if anyone is interested:

public class Grid
	{
		private GraphicsDevice device;
		private GraphicsDeviceManager graphics;
		private ContentManager content;
		private Matrix worldMatrix, viewMatrix, projectionMatrix;
		private BasicEffect basicEffect;
		private VertexBuffer vertexBuffer;

		public Grid(GraphicsDevice graphicsDevice, GraphicsDeviceManager graphicsDeviceManager, ContentManager contentManager)
		{
			device	  = graphicsDevice;
			graphics	= graphicsDeviceManager;
			content	 = contentManager;
		}

		public void LoadContent()
		{
			float aspectRatio	   = (float)device.Viewport.Width /
									  (float)device.Viewport.Height;

			float fov			   = MathHelper.PiOver4 * aspectRatio;

			worldMatrix			 = Matrix.Identity;
			viewMatrix			  = Matrix.CreateLookAt(new Vector3(0.0f, 2.0f, 2.0f), Vector3.Zero, Vector3.Up);
			projectionMatrix		= Matrix.CreatePerspectiveFieldOfView(fov, aspectRatio, 0.1f, 1000.0f);

			basicEffect			 = new BasicEffect(device);
			basicEffect.World	   = worldMatrix;
			basicEffect.View		= viewMatrix;
			basicEffect.Projection  = projectionMatrix;
			basicEffect.LightingEnabled = false;

			CreateGrid(10, 10, 5, 5, Vector3.Zero);
		}

		public void UnloadContent()
		{
			if (vertexBuffer != null)
			{
				vertexBuffer.Dispose();
				vertexBuffer = null;
			}

			if (basicEffect != null)
			{
				basicEffect.Dispose();
				basicEffect = null;
			}
		}

		public void Update(GameTime gameTime)
		{
			worldMatrix *= Matrix.CreateRotationY(0.005f) * Matrix.CreateRotationX(0.005f);
		}

		public void Draw()
		{
			device.SetVertexBuffer(vertexBuffer);

			basicEffect.World = worldMatrix;
			basicEffect.CurrentTechnique.Passes[0].Apply();
			foreach (EffectPass CurrentPass in basicEffect.CurrentTechnique.Passes)
			{
				device.DrawPrimitives(PrimitiveType.LineList, 0, vertexBuffer.VertexCount / 2);
			}
		}

		// Algorithm/code thanks to: http://8bitmemories.blogspot.com/2010/01/simple-grid-in-xna.html
		public void CreateGrid(int width, int height, int rows, int columns, Vector3 position)
		{
			List<VertexPositionColor> vertices;

			vertices = new List<VertexPositionColor>();
			int xDiff = width / columns;
			int zDiff = height / rows;
			float xBase = position.X - width / 2.0f;
			float zBase = position.Z - height / 2.0f;
			float yBase = position.Y;

			for (int i = 0; i <= rows; ++i)
			{
				vertices.Add(new VertexPositionColor(new Vector3(xBase + i * xDiff, yBase, zBase), Color.White));
				vertices.Add(new VertexPositionColor(new Vector3(xBase + i * xDiff, yBase, zBase + height), Color.White));
			}

			for (int i = 0; i <= columns; ++i)
			{
				vertices.Add(new VertexPositionColor(new Vector3(xBase, yBase, zBase + i * zDiff), Color.White));
				vertices.Add(new VertexPositionColor(new Vector3(xBase + width, yBase, zBase + i * zDiff), Color.White));
			}

			vertexBuffer = new VertexBuffer(device, typeof(VertexPositionColor), vertices.Count, BufferUsage.WriteOnly);
			vertexBuffer.SetData<VertexPositionColor>(vertices.ToArray());
		}
	}

Confused a bit on the matrix math in 3D programming

09 March 2012 - 07:32 PM

I'm reading 'Beginning XNA 3.0 Game Programming's chapter on 3D programming math. I had a univesity class (3D game design) where I pretty much screwed up a good chance to learn the math because I was slacking and now I'm actually interested and I need to learn the math behind it all so I can do it proficiently.

With that out of the way...I feel like I'm getting this stuff (since I've already made a few 3D games before in XNA 3.1 my ideas here are somewhat clear, but I was really just trail n erroring my way through most of the 3D stuff) because it makes a lot of sense in retrospect. BUT...the book is not very clear on the following and I'd like come help to clear it up:

1. Since XNA uses a right-handed coordinate system objects with a negative Z values are what would be visible if the camera was at the origin. What I'm a bit confused on is this functions parameter and how it works:

Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10.0f)    // parameters: FOV, Aspect Ratio, Near View Plane, Far View Plane

The book explains it like this:

... then create a perspective projection matrix, "looking" in a 45-degree angle as the field of view. The rendering happens for objects from 1 to 10 units from the screen (z values from -1 to -10).


I'm confused because it seems to me if the near view plane is set to be 1.0f, it should be at 1.0f along the z axis, which would be behind the screen, going further out behind the screen to 10.0f at the far view plane. However, it's saying this isn't the case, and I have a few ideas about why that might be but none seem obvious enough to not warrant asking for clarification.

2. What are these matrices {'view', 'world', 'projection'...etc} It lists the following as definitions of each matrix:

View: defines the camera position and direction. Usually created using Matrix.CreateLookAt.

Projection: used to map the 3D scene coordinates to screen coordinates. Usually created through Matrix.CreatePerspective...etc

World: used to apply transformations to all objects in the 3D scene.


This gives me a great idea of what they are used for but I'm fuzzy on what they are. Here's my foggy understanding of each:

View: My understanding is that this is the camera "in concept." In other words, every frame a camera matrix (which is a hard idea for me to get, a camera is just a matrix? Not that it would be able to do anything just as matrix, but you know what I mean maybe?) at origin, and then it's translated, rotatated, or whatever by matrix multiplcation with this view matrix.

Projection: This seems to be the "functionality" of the camera. I have no idea what gets multiplied by this matrix or how it's used, REALLY foggy on this one.

World: I'm also really foggy about this one. It almost seems like if every objects position matrix is multiplied by the world matrix then that is what sets up the scene for rendering, but, if that's the case then you aren't really using cameras you are just using a camera at origin and moving the entire world to fit your scene? Confusing.

Sorry about the length of the post, trying to be as concise as I possibly can be.

A 'image manager' class?

28 July 2010 - 06:27 PM

I'm writing a level editor for my game and to simplify resource management I want to write a image manager class that I can use in both the level editor and my game. The idea that I currently have is basically every image is loaded into the image manager class and stored into a vector like this:

std::vector<pair<SDL_Surface *image, int id>> m_imageList;


Then have a header file that I'll share between them that looks like this:

#define GRASS_TILE 1
#define STONE_TILE 2
#define CRATE_SPRITE 3
// .. etc etc


Then, when I start up the image manager class I load each image in the same order that the header file uses.

I'm worried that this is going to bite me in the ass later on somehow, so I figured I'd ask if there is a better way of doing something like this that I could look into?

Proper game loop in SDL?

27 July 2010 - 09:46 AM

Hi, so, I'm trying to determine what the proper game-loop should look like in SDL, I currently use something like this - but I can't seem to find any resources on whether or not I'm doing it properly:

int main(int argc, char* args[])
{
// Initialize the game
InitGame();

// Load the game resources
LoadGame();

// Game Loop
while ( gamedata.game_loop ) // This is a bool set to true until exit
{
// At the start of a frame we want to start our FPS timer
timer_reg_fps.StartTimer();

// Grab the latest event in the event-queue
SDL_PollEvent(&gamedata.sdl_event);

// Update the game each frame
UpdateGame(); // This runs game-logic

// Render the game each frame
RenderGame(); // Effectively this calls an SDL_Flip() to update the screen

// Regulate the FPS - keeps the FPS around 60
if ( gamedata.vsync )
{
// Check if we need to sleep
if ( timer_reg_fps.GetTicks() < (1000 / gamedata.vsync_fps) )
{
// Sleep
SDL_Delay( ( 1000 / gamedata.vsync_fps ) - timer_reg_fps.GetTicks() );
}
}

// At the end of a frame we want to reset our frame timer
timer_reg_fps.StopTimer();
// Now reset it
timer_reg_fps.ResetTimer();
}

// Unload resources
UnloadGame();

// Exit program
return 0;
}



At the start of UpdateGame() function I call an ClearScreen() command that effectively does SDL_FillRect() on the screen that will be SDL_Flipp()'d each frame in RenderGame(), then I redraw the entire scene.

Unfortunately I don't know how to make this a clear and concise question, so I apologize for that - but my game tends to jitter a bit as I move along (it's a side-scroller very much like Mario) and I'm not sure if I'm doing something blatantly wrong here or if it's just to be expected when using SDL. (It's not a very noticeable jitter, I probably only notice it because I'm the one writing the game.)

PARTNERS