0

それで、私は 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;
}
4

1 に答える 1

0

あなたは物事を複雑にしすぎていると思います。これが私の考えです:

class Ball
{
    public Vector3 Position;
    public float Radius;

    // ...
}

class Table
{
    public Vector3 Position;
    public SizeF Size;

    // ...

    public bool CheckForCollision(Ball ball)
    {
        Vector3 upperLeftCorner = new Vector3(
                this.Position.X - this.Size.Width,
                this.Position.Y,
                this.Position.Z - this.Size.Heigth),
            lowerRightCorner = new Vector3(
                this.Position.X + this.Size.Width,
                this.Position.Y,
                this.Position.Z + this.Size.Height);

        if(ball.Position.X < upperLeftCorner.X ||
            ball.Position.X > lowerRightCorner.X)
        {
            return false;
        }

        if(ball.Position.Z < upperLeftCorner.Z ||
            ball.Position.Z > lowerRightCorner.Z)
        {
            return false;
        }

        if(ball.Position.Y - ball.Radius > this.Position.Y)
        {
            return false;
        }

        return true;
    }
}

最後にXNAを使用してからしばらく経ちましたので、座標で何かを台無しにした可能性がありますが、これは私が意図したことです。

  origin
    +---->  X, Width
    |
    |  upperLeftCorner
    |     +----------------+
    v     |                |
          |                |
    y,    |                |
  height  |                |
          |                |
          |        +       |
          | table.position |
          |                |
          |                |
          |                |
          |                |
          +----------------+
                       lowerRightCorner
于 2012-03-05T21:02:59.313 に答える