I think one thing that could be causing your problem is whether or not your GraphicsDevice.RenderState.DepthBufferEnable
is set to true
. (If you are drawing spritebatches, this is very likely the problem.) I would check this, because I had a similar drawing problem and setting GraphicsDevice.RenderState.DepthBufferEnable
to true
every draw (before I drew the models) fixed the problem. If you are using XNA 4.0, instead of using the code above, you'll have to do something like this:
DepthStencilState depthBufferState = new DepthStencilState();
depthBufferState.DepthBufferEnable = true;
GraphicsDevice.DepthStencilState = depthBufferState;
Here is a link that might be helpful. XNA - Drawing Quads (and primitives) in right order
EDIT:
To answer your question about the windows on the cars not drawing properly, to fix this you need to draw the cars in the correct order: Farthest away cars first and then closer cars. You could try something like this:
Game Logic:
List<Model> modellist;
Public Override Void Update
{
//Update Logic
foreach (Model m in n)
{
m.Update(cameraPosition);
}
}
Public Override Void Draw(GameTime gametime)
{
//Draw Primitives and then sort models by distance from camera
List<Model> n = modellist.OrderByDescending(x => x.DistanceFromCamera).ToList<Model>();
foreach (Model m in n)
{
//Draw Model m
}
}
Model Class
class Model
{
private int distanceFromCamera = 0;
public int DistanceFromCamera
{
get { return distanceFromCamera; }
set { distanceFromCamera = value; }
}
public Vector3 Position;
public void Update(Vector3 CameraPos)
{
//...
distanceFromCamera = Vector3.Distance(CameraPos, this.Position);
}
}
You could also have call OrderByDescending() in the Update void; this would probably be more effective. But hopefully this will point you in the right direction. HTH