I'm by no means a 3d programmer but have recently taken it upon myself to build a Voxel based game and I'm having a little trouble understanding some of the concepts around matrices (what with my limited knowledge of mathematics en all).
So here's my situation ... (assume I've declared other stuff but omitted for simplicity)
I have my camera class in to which I have declared the 3 basic matrices (I think something's wrong here for a start)
class Camera
{
public Matrix World { get; set; }
public Matrix View { get; set; }
public Matrix Proj { get; set; }
}
Now I have a few other bits like a BaseModel class, a chunk class and a block class that gives me all the core bits i need for a voxel based modelling system ...
abstract class BaseModel
{
public Chunk[] { get; }
}
class Chunk
{
Block[,,] Blocks { get;set; }
}
class Block
{
public Bool IsActive { get;set; }
public int Type { get;set; }
}
class TerrainModel : BaseModel
{
... terrain specific stuff ...
}
class PlayerModel : BaseModel
{
... Player specific stuff ...
}
This is all good and works well.
So i can do something like (obviously i'm not doing this in my draw method):
TerrainModel terrain = new TerrainModel(heightMap);
terrain.Draw();
In my draw Method I have something like ...
if (updated)
{
// Load the vertex data in to the buffer
buffer.SetData(Vertices);
updated = false;
}
// Send the vertex buffer to the device
GameEngine.GraphicsDevice.SetVertexBuffer(buffer);
cubeEffect.World = GameEngine.Camera.World;
cubeEffect.View = GameEngine.Camera.View;
cubeEffect.Projection = GameEngine.Camera.Projection;
cubeEffect.TextureEnabled = true;
cubeEffect.Texture = cubeTexture;
foreach (EffectPass pass in Effect.CurrentTechnique.Passes)
{
pass.Apply();
GameEngine.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, Vertices.Length / 3);
}
Now here's where I get lost: Assuming when I setup my camera I have done this ...
camera.World = Matrix.CreateTranslation(0,0,0);
... from the cameras point of view the world is based on points starting at that location which should be correct I think.
Now assuming I want to render my terrain model at a different location do I create a new world matrix that represents the translation between the terrain model and the world that my game lives in?
This seems like the logically thing to do and everything I read suggests that maybe the "world matrix" is not a single object but represents the translation from model to world rather than that of all world space like I had previously thought.
When I do this ... (assume i'm only rendering this model for now, I know this has other issues)
camera.World = Matrix.CreateTranslation(10,0,10);
... all is good and everything renders the extra 10 pixels on the x and z axis. The bit I can't seem to figure out is how to move only (for example) my player model taking this approach because when I do this ...
Matrix playerWorld = Matrix.CreateTranslation(PlayerModel.Position);
... I would expect a translation from player model space to world space for each of the verts in question however instead the player object seems to disappear going to who knows where (trust me I looked).
The odd thing being that my camera class is a Third Person Camera and follows the PlayerModel around, pushing the keys I can move the camera but the playerModel stays still.
Have I missed something fundamental about Matrix calculations?
EDIT:
Ok something very odd ... If I tell all models (via the base class) to generate a World matrix and use that in their rendering it all starts working !! ... how peculiar?
That raises the question ... Is world space something that is considered "relative" to each model in the scene and their location in relation to the camera?
I've solved my problem but matrices still remain a scary mystery to me :)

