# XNA, how to draw two cubes standing in line parallelly?

I just got a problem with drawing two 3D cubes standing in line. In my code, I made a cube class, and in the game1 class, I built two cubes, A on the right side, B on the left side. I also setup an FPS camera in the 3D world. The problem is if I draw cube B first(Blue), and move the camera to the left side to cube B, A(Red) is still standing in front of B, which is apparently wrong.

I guess some pics can make much sense. Then, I move the camera to the other side, the situation is like: This is wrong.... From this view, the red cube, A should be behind the blue one, B....

Could somebody give me help please?

This is the draw in the Cube class

Matrix center = Matrix.CreateTranslation(
new Vector3(-0.5f, -0.5f, -0.5f));
Matrix scale = Matrix.CreateScale(0.5f);
Matrix translate = Matrix.CreateTranslation(location);
effect.World = center * scale * translate;
effect.View = camera.View;
effect.Projection = camera.Projection;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.SetVertexBuffer(cubeBuffer);

RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
rs.FillMode = FillMode.Solid;
device.RasterizerState = rs;

device.DrawPrimitives(
PrimitiveType.TriangleList,
0,
cubeBuffer.VertexCount / 3);
}


This is the Draw method in game1

 B.Draw(camera, effect);
A.Draw(camera, effect);


**

• Can you post your drawing code, so we can see what you are doing?
– Seta
May 29, 2014 at 10:08
• Sure, I have updated my question and add my draw function May 29, 2014 at 10:16

It seems to me that you don't have any depth testing going on. For that you have to have a depth buffer and write to that while drawing your geometry.
If you are drawing to a rendertarget then create it like this:

RenderTarget2D rt = new RenderTarget2D(device, device.Viewport.Width, device.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24);


If you are drawing directly to the backbuffer you already have a depth texture bound to it.

In your drawing code you need to clear the depth too (well it's optional but it should be cleared to avoid some artifacts, also this doesn't cost you much to do so) while clearing the backbuffer (or rendertarget) color:

 device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

 device.DepthStencilState = DepthStencilState.Default;

• While practically device.DepthStencilState = DepthStencilState.Default; does enable depth testing, technically the way to enable depth testing is with device.DepthStencilState.DepthBufferEnable = true;. DepthStencilState.Default is a depth stencil state in which it depth testing is already enabled, so setting it will (among other things) enable depth testing. May 29, 2014 at 11:57
• Actually he's wrong. You cannot modify a state object once it has been set onto a device. Nor can you modify the default states. You'd need to create a custom DepthStencilState object, if you wanted something other than Default. Source. Suggest you edit it back. May 29, 2014 at 13:25