それで、私は 3D 卓球ゲームを開発しようとしていますが、ボールがテーブルに衝突するときに問題が発生しています。ボールのバウンディング球とテーブルのバウンディング ボックスがありますが、交差はそれほど正確ではありません。ボールとラケットとの衝突は良好であるため、バウンディング ボックスが正しくないことを推測しています。
エラーがどこにあるかを簡単に確認できるように境界ボックスを表示しようとしていますが、コードで見つけたチュートリアルを実装できないか、方法がわかりません。そして、私のテーブルは動いていないので、AABB を作成するのは正しいですか? 誰かが助けてくれれば、それは大歓迎です。誰かがボールを箱に衝突させるより良い方法を提案できれば (もしあれば) 感謝します。バウンディング ボックスの検出と、球体とボックスの間の衝突を貼り付けました。さらにコードが必要な場合は、投稿します。ありがとう
private bool Ball_Table(Model model1, Matrix world1)
{
for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
{
BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
sphere1 = sphere1.Transform(world1);
if(table_box.Intersects(sphere1))
return true;
}
return false;
}
.
protected BoundingBox UpdateBoundingBox(Model model, Matrix worldTransform)
{
// Initialize minimum and maximum corners of the bounding box to max and min values
Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
// For each mesh of the model
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
// Vertex buffer parameters
int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
int vertexBufferSize = meshPart.NumVertices * vertexStride;
// Get vertex data as float
float[] vertexData = new float[vertexBufferSize / sizeof(float)];
meshPart.VertexBuffer.GetData<float>(vertexData);
// Iterate through vertices (possibly) growing bounding box, all calculations are done in world space
for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float))
{
Vector3 transformedPosition = Vector3.Transform(new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]), worldTransform);
min = Vector3.Min(min, transformedPosition);
max = Vector3.Max(max, transformedPosition);
}
}
}
// Create and return bounding box
table_box = new BoundingBox(min, max);
return table_box;
}