我有一个 Block 类,它基本上包含一个 36 个顶点的数组,一个围绕原点 (0, 0, 0) 构建块的构造函数,以及一个更新方法。
我正在使用物理库来操作块。我为块分配了一个身体和碰撞皮肤,并在每次更新时更新物理步骤。每次更新后,我都会得到一个矩阵,其中包含块的新位置、方向等。
现在,我无法理解的是将矩阵应用于块并有效地每帧更新顶点缓冲区的最佳方法。
这基本上就是我目前所拥有的,我几乎肯定有更好的方法......
class Block
{
const float FullBlockSize = 20f;
const float HalfBlockSize = FullBlockSize / 2;
public VertexPositionNormalTexture [] vertices = new VertexPositionNormalTexture[36];
public VertexPositionNormalTexture [] currentVertices = new VertexPositionNormalTexture[36];
public DynamicVertexBuffer vBuffer;
Vector3 position;
public Block(Vector3 blockPosition)
{
position = blockPosition * FullBlockSize;
/*
* Build the 6 faces of the block here.
* The block is built around the origin,
* (0, 0, 0) being the center of the block.
*/
vBuffer = new DynamicVertexBuffer(Game.Device, VertexPositionNormalTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
vBuffer.SetData(vertices);
}
public Matrix WorldMatrix
{
get
{
return Matrix.CreateTranslation(position);
}
}
public void Update()
{
currentVertices = (VertexPositionNormalTexture[])vertices.Clone();
Matrix currentWorld = WorldMatrix;
for(int i = 0; i < currentVertices.Length; ++i)
{
currentVertices[i].Position = Vector3.Transform(currentVertices[i].Position, currentWorld);
}
vBuffer.SetData(currentVertices);
}
}