基本的に 36 個の頂点の配列、原点 (0, 0, 0) の周りにブロックを構築するコンストラクター、および更新用のメソッドを含む Block クラスがあります。
ブロックを操作するために物理ライブラリを使用しています。ボディとコリジョン スキンをブロックに割り当て、更新ごとに物理ステップを更新します。更新するたびに、ブロックの新しい位置、向きなどを含むマトリックスが返されます。
さて、私が理解できないのは、マトリックスをブロックに適用し、フレームごとに頂点バッファーを効率的に更新するための最良の方法です。
これは基本的に私が現時点で持っているものです。より良い方法があるとほぼ確信しています...
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);
}
}