Example #1
0
    private bool changeColor = false;              // toogle for color colisions

    // BallList constructor
    public BallList(Transform parent, float inputBoxSize, int numBalls, bool varyRadii, bool changeColor)
    {
        gameObj.name             = "BallList"; // name the empty game object
        gameObj.transform.parent = parent;     // set as child
        boxSize          = inputBoxSize;       // get box size
        this.changeColor = changeColor;        // turn on color collisions
        AddBalls(numBalls, varyRadii);
        //AddBall_Test();

        // Make spatial grid
        // Calculate cell size such that the box will contain an integer number of cells, each
        //   at least twice the diameter of a ball, if possible
        float cellSize;
        int   numCellsInBoxSize = Mathf.FloorToInt(boxSize / (ballRadius * 2 * 2));                //  number of cells that in boxsize
        int   numCells          = 8 * (numCellsInBoxSize * numCellsInBoxSize * numCellsInBoxSize); // total number of cells in box

        if (numCellsInBoxSize < 1)                                                                 // if the size of a cell is larger than boxsize,
        {
            cellSize = 2 * boxSize;                                                                // make the box one big cell
            numCells = 1;
        }
        else
        {
            cellSize = boxSize / numCellsInBoxSize;
        }
        List <Vector3> positions = balls.Select(o => o.position).ToList(); // make a list of the ball positions

        grid = new Grid3D <Ball> (cellSize, numCells);                     // create the grid
        grid.Fill(positions, balls, ballRadius);                           // populate the grid
    }
Example #2
0
        public void Fill_OnlyTouchesCube()
        {
            var width  = 100;
            var height = 50;
            var depth  = 24;

            var grid = new Grid3D <int>(width, height, depth, 33);

            int minX = 5, minY = 10, minZ = 8;
            int maxX = width - 5, maxY = height - 10, maxZ = depth - 8;

            grid.Fill(1, minX, minY, minZ, maxX, maxY, maxZ);

            for (var x = 0; x < grid.Width; x++)
            {
                for (var y = 0; y < grid.Height; y++)
                {
                    for (var z = 0; z < grid.Depth; z++)
                    {
                        // outside cube
                        if (x < minX || x > maxX ||
                            y < minY || y > maxY ||
                            z < minZ || z > maxZ)
                        {
                            Assert.AreEqual(33, grid[x, y, z], "({0},{1},{2})", x, y, z);
                        }
                        else
                        {
                            Assert.AreEqual(1, grid[x, y, z], "({0},{1},{2})", x, y, z);
                        }
                    }
                }
            }
        }
Example #3
0
    // Integrate the motion of the balls, including collision detection and resolution
    public void IntegrateMotion(float timeStep)
    {
        CheckCollisions_Grid(timeStep);

        foreach (Ball ball in balls)
        {
            ball.CheckBoundary(boxSize);

            ball.MoveBall(timeStep);

            ball.Fade();
        }

        // Update grid
        grid.Empty();
        List <Vector3> positions = balls.Select(o => o.position).ToList();          // make a list of the ball positions

        grid.Fill(positions, balls, ballRadius);
    }