Over a million developers have joined DZone.

Drawing 3D Helper Axes in XNA


I started with XNA a while ago, and while working on my 3D game development skills, I realized that sometimes it is sometimes useful to see where your objects are located. For this purpose, there are the XYZ axes. There is no built-in way to display helper axes, so I decided to design my own.

To get started, I created two matrices, that are the view matrix and the projection matrix. The view matrix defines the position of the camera, that will look at the world, while the projection matrix will determine the way it looks at it.

Matrix viewMatrix;
Matrix projectionMatrix; 

Here is a function that will set the proper location values for the matrices:

private void CameraInit()
viewMatrix = Matrix.CreateLookAt(new Vector3(positionX, positionY, positionZ), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.1f, 100.0f);

Notice that the first Vector3, that defines the camera position, gets the x, y and z values from three different variables. I use this to later on modify these values whenever the camera moves. Here is the declaration (also should be position in the class header):

int positionX = 30;
int positionY = 0;
int positionZ = 30; 

The values can be easily adjusted depending on the object that is being rendered, so that it fits in the camera “viewfinder”. Now, I needed to draw the lines that will represent the axes. First of all, I defined an array of VertexPositionColor:

VertexPositionColor[] lines;

To set the values for the reference points, I created a separate function that determines the size of the vertices:

protected void SetLineVerticles()

lines = new VertexPositionColor[6];

lines[0] = new VertexPositionColor(new Vector3(-10, 0, 0), Color.Red);

lines[1] = new VertexPositionColor(new Vector3(10,0,0), Color.Red);

lines[2] = new VertexPositionColor(new Vector3(0, -10, 0), Color.Green);

lines[3] = new VertexPositionColor(new Vector3(0,10,0), Color.Green);

lines[4] = new VertexPositionColor(new Vector3(0, 0, -10), Color.Blue);

lines[5] = new VertexPositionColor(new Vector3(0,0,10), Color.Blue);

Every two VertexPositionColor instances define two points that will be used to draw  a line as well as the line color. It is important to make these lines intersect in a single point and make sure that those are perpendicular to each other. Opposite values on the same axis will make this possible (see the correspondence between values – for the X axis it is –10 and 10, same applies to the Y and Z axes.

To draw the axes, I used the this snippet:

protected override void Draw(GameTime gameTime)


BasicEffect effect = new BasicEffect(device,null);

effect.World = Matrix.Identity;

effect.View = viewMatrix;

effect.Projection = projectionMatrix;

effect.VertexColorEnabled = true;


foreach (EffectPass pass in effect.CurrentTechnique.Passes)



device.VertexDeclaration = vertexDeclaration;

device.DrawUserPrimitives(PrimitiveType.LineList, lines, 0, 3);






It is the regular Draw function with primitive drawing code inside. Notice that I have a undeclared element here: vertexDeclaration. It is what it is named – a VertexDeclaration instance:

VertexDeclaration vertexDeclaration;

Another interesting part of the above code is effect.VertexColorEnabled = true; This part enables the colors for the drawn primitives. If I will not enable this, all my lines will be black, although I explicitly set the colors for them.

If you run the game now, you will see a set of lines drawn as seen from the front. What if I wanted to see those from an angled perspective? I added the code below to the Update function, so that when the up key is pressed, the Y position of the camera is shifted, therefore I can clearly see the axes.

if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up))




Now, you can easily place other objects in this space and you can visually track where those are located.If you enjoy watching video tutorials, you might want to look at this on YouTube.



Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}