コード例 #1
0
    // A broad phase collision detection method utilizing a spatial grid
    private void CheckCollisions_Grid(float timeStep)
    {
        List <int> keys = grid.GetKeys();        // get the list of existing cells

        // For each cell in the grid
        foreach (int key in keys)
        {
            // Get a list of balls in that cell
            List <Ball> ballsInCell = grid.GetObjectsInCell(key);

            // If there is more than one ball,
            if (ballsInCell.Count > 1)
            {
                // For each ball in the cell
                for (int i = 0; i < ballsInCell.Count - 1; i++)
                {
                    // Check whether it's colliding with another ball
                    for (int j = i + 1; j < ballsInCell.Count; j++)
                    {
                        if (ballsInCell[i].IsCollidingWith(ballsInCell[j]))
                        {
                            CollisionResolve(ballsInCell [i], ballsInCell [j]);                              // Trigger collision resolution
                        }
                    }
                }
            }
        }
    }