예제 #1
0
파일: Game1.cs 프로젝트: BigDub/MiniPhys
 void initCubes()
 {
     cubes = new Cube[gridSize + 1, gridSize + 1];
     for (int x = 0; x <= gridSize; x++)
     {
         for (int z = 0; z <= gridSize; z++)
         {
             Cube c = new Cube();
             c.pos = new Vector3(x - gridSize / 2, 0, z - gridSize / 2);
             //c.vel = Vector3.UnitY * 5;
             cubes[x, z] = c;
         }
     }
 }
예제 #2
0
파일: Game1.cs 프로젝트: BigDub/MiniPhys
        void UpdateCubes()
        {
            highestCur = 0;
            float mForce = 100, sForce = 50;

            for (int x = 0; x <= gridSize; x++)
            {
                for (int z = 0; z <= gridSize; z++)
                {
                    Cube  c    = cubes[x, z];
                    float dist = MathHelper.Max(1, 0.5f * (new Vector2(c.pos.X, c.pos.Z) - intersect).Length());
                    c.forces.Y  = 0;
                    c.forces.Y += -0.1f;//basic gravity
                    if (applyForce)
                    {
                        c.forces.Y += (mForce / dist);//applied force
                    }
                    c.forces.Y += sForce * -c.pos.Y;

                    /*if (c.pos.Y < 0)
                     * {
                     *  c.forces.Y += 0.1f * -c.pos.Y;//particles beneath pushing up
                     * }*/
                    c.vel *= 0.99f;//basic friction
                    #region Neighbors
                    if (x != 0)
                    {
                        dist        = cubes[x - 1, z].pos.Y - c.pos.Y;
                        c.forces.Y += dist * sForce;
                    }
                    else
                    {
                        dist        = -c.pos.Y;
                        c.forces.Y += dist * sForce;
                    }
                    if (x != gridSize)
                    {
                        dist        = cubes[x + 1, z].pos.Y - c.pos.Y;
                        c.forces.Y += dist * sForce;
                    }
                    else
                    {
                        dist        = -c.pos.Y;
                        c.forces.Y += dist * sForce;
                    }
                    if (z != 0)
                    {
                        dist        = cubes[x, z - 1].pos.Y - c.pos.Y;
                        c.forces.Y += dist * sForce;
                    }
                    else
                    {
                        dist        = -c.pos.Y;
                        c.forces.Y += dist * sForce;
                    }
                    if (z != gridSize)
                    {
                        dist        = cubes[x, z + 1].pos.Y - c.pos.Y;
                        c.forces.Y += dist * sForce;
                    }
                    else
                    {
                        dist        = -c.pos.Y;
                        c.forces.Y += dist * sForce;
                    }
                    #endregion
                }
            }
            for (int x = 0; x <= gridSize; x++)
            {
                for (int z = 0; z <= gridSize; z++)
                {
                    Cube c = cubes[x, z];
                    c.Update(Elapsed);
                    highestCur  = (c.pos.Y > highestCur) ? c.pos.Y : highestCur;
                    highestEver = (c.pos.Y > highestEver) ? c.pos.Y : highestEver;
                }
            }
        }