Ejemplo n.º 1
0
        private void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch        = new SpriteBatch(GraphicsDevice);
            WhiteCircleTexture = Content.Load <Texture2D>("Map/WhiteCircle512");


            RenderHelper.Ini(WhiteTexture, WhiteCircleTexture);

            sim.Initialize(sim);
            simRenderer.Initialize(Content, GraphicsDevice, spriteBatch);

            inputManager.Initialize(gameConfiguration, sim, simRenderer);
        }
Ejemplo n.º 2
0
    // Start is called before the first frame update
    void Start()
    {
        m_numCells   = m_gridResolution * m_gridResolution;
        m_iterations = (int)(1.0f / m_dt);

        // 1. Initialize the grid by filling the grid array with res x res cells
        m_grid = new NativeArray <Cell> (m_numCells, Allocator.Persistent);
        for (int i = 0; i < m_numCells; i++)
        {
            m_grid[i] = new Cell();
        }

        // 2. Create a bunch of particles and set their positions somewhere
        List <float2> tempPositions = new List <float2> ();
        const float   spacing       = 1.0f;
        int           boxX          = m_gridResolution / 4;
        int           boxY          = m_gridResolution / 4;
        float         sx            = m_gridResolution / 2.0f;
        float         sy            = m_gridResolution / 2.0f;

        for (float i = sx - boxX / 2; i < sx + boxX / 2; i += spacing)
        {
            for (float j = sy - boxY / 2; j < sy + boxY / 2; j += spacing)
            {
                var pos = math.float2(i, j);
                tempPositions.Add(pos);
            }
        }
        m_numParticles = tempPositions.Count;

        m_particles = new NativeArray <Particle> (m_numParticles, Allocator.Persistent);
        for (int i = 0; i < m_numParticles; i++)
        {
            Particle p = new Particle();
            p.x            = tempPositions[i];
            p.v            = math.float2(Random.value - 0.5f, Random.value - 0.5f + 2.75f) * 0.5f;
            p.C            = 0;
            p.mass         = 1.0f;
            m_particles[i] = p;
        }

        m_simulationRenderer.Initialize(m_numParticles, Marshal.SizeOf(new Particle()));
    }
Ejemplo n.º 3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch        = new SpriteBatch(GraphicsDevice);
            WhiteCircleTexture = Content.Load <Texture2D>("Map/WhiteCircle512");


            RenderHelper.Ini(WhiteTexture, WhiteCircleTexture);

            sim.Initialize(sim);
            simRenderer.Initialize(Content, GraphicsDevice, spriteBatch);


            float viewportWidth  = GraphicsDevice.Viewport.Width;
            float tileMapWidth   = sim.TileMap.GetWorldWidth();
            float viewportHeight = GraphicsDevice.Viewport.Height;
            float tileMapHeight  = sim.TileMap.GetWorldHeight();

            simRenderer.Camera.Scale = Mathf.Min(viewportWidth / tileMapWidth, viewportHeight / tileMapHeight);

            simRenderer.Camera.Translation = new Vector2(tileMapWidth / 2, 0);
        }
Ejemplo n.º 4
0
    void Start()
    {
        m_numCells   = m_gridResolution * m_gridResolution;
        m_iterations = (int)(1.0f / m_dt);

        // 1. Initialize the grid by filling the grid array with res x res cells
        m_grid = new NativeArray <Cell> (m_numCells, Allocator.Persistent);
        for (int i = 0; i < m_numCells; i++)
        {
            var c = new Cell();
            c.v       = 0;
            m_grid[i] = c;
        }

        // 2. Create a bunch of particles and set their positions somewhere
        List <float2> tempPositions = new List <float2> ();
        const float   spacing       = 1.0f;
        int           boxX          = m_gridResolution / 2;
        int           boxY          = m_gridResolution / 2;
        float         sx            = m_gridResolution / 2.0f;
        float         sy            = m_gridResolution / 2.0f;

        for (float i = sx - boxX / 2; i < sx + boxX / 2; i += spacing)
        {
            for (float j = sy - boxY / 2; j < sy + boxY / 2; j += spacing)
            {
                var pos = math.float2(i, j);
                tempPositions.Add(pos);
            }
        }
        m_numParticles = tempPositions.Count;

        m_particles = new NativeArray <Particle> (m_numParticles, Allocator.Persistent);
        m_Fs        = new NativeArray <float2x2> (m_numParticles, Allocator.Persistent);
        for (int i = 0; i < m_numParticles; i++)
        {
            Particle p = new Particle();
            p.x            = tempPositions[i];
            p.v            = 0;
            p.C            = 0;
            p.mass         = 1.0f;
            m_particles[i] = p;

            // Initialize the deformation gradient to the identity
            m_Fs[i] = math.float2x2(1, 0, 0, 1);
        }

        // Launch a job to scatter the particle mass to the grid
        // TODO: write here

        //  Precomuptation of particle volumes: mpm course eq.152
        for (int i = 0; i < m_numParticles; i++)
        {
            var p = m_particles[i];

            float2 cell_idx  = math.floor(p.x);
            float2 cell_diff = (p.x - cell_idx) - 0.5f;
            m_weights[0] = 0.5f * math.pow(0.5f - cell_diff, 2);
            m_weights[1] = 0.75f - math.pow(cell_diff, 2);
            m_weights[2] = 0.5f * math.pow(0.5f + cell_diff, 2);

            float density = 0.0f;
            for (int gx = 0; gx < 3; gx++)
            {
                for (int gy = 0; gy < 3; gy++)
                {
                    float weight     = m_weights[gx].x * m_weights[gy].y;
                    int   cell_index = ((int)cell_idx.x + (gx - 1)) * m_gridResolution + ((int)cell_idx.y + gy - 1);
                    density += m_grid[cell_index].mass * weight;
                }
            }
            p.volume_0     = p.mass / density;
            m_particles[i] = p;
        }

        m_simulationRenderer.Initialize(m_numParticles, Marshal.SizeOf(new Particle()));
    }