public void UpdateSprings(RenderArrays springDebugArrays, Colour springColour, List <Vector2d> positions, List <Vector2d> velocities)
        {
#if DEBUG
            springDebugArrays.Positions.Clear();
            springDebugArrays.Colours.Clear();
#endif

            for (uint i = 0; i < EndPointIndices.Count; i++)
            {
                int startIndex = (int)StartPointIndices[(int)i];
                int endIndex   = (int)EndPointIndices[(int)i];

                Vector2d startPoint = positions[startIndex];
                Vector2d endPoint   = positions[endIndex];
                Vector2d delta      = startPoint - endPoint;

                float equilibriumLength = RestLengths[(int)i];
                float stiffness         = Stiffnesses[(int)i];
                float currentLenght     = (float)Math.Sqrt(Vector2d.DotProduct(delta, delta));
                if (currentLenght == 0)
                {
                    currentLenght = float.MinValue;
                }
                float lengthBeyondEquilibrium = currentLenght - equilibriumLength;
                float restoringForce          = lengthBeyondEquilibrium * stiffness * -1.0f;

                /*if (lengthBeyondEquilibrium > equilibriumLength * 2.0f)
                 * {
                 *  EndPointIndices.RemoveAt((int)i);
                 *  RestLengths.RemoveAt((int)i);
                 *  StartPointIndices.RemoveAt((int)i);
                 *  Stiffnesses.RemoveAt((int)i);
                 *  i--;
                 *  continue;
                 * }*/

                Vector2d direction = delta / currentLenght;

                // Assuming contant mass
                velocities[startIndex] += direction * restoringForce;
                velocities[endIndex]   -= direction * restoringForce;

#if DEBUG
                springDebugArrays.Positions.Add((startPoint + endPoint) / 2.0f);
                springDebugArrays.Colours.Add(springColour);
#endif
            }
        }
예제 #2
0
        public World(IRenderer renderer)
        {
            m_baseColours = new Colour[12];

            m_baseColours[00] = new Colour()
            {
                R = 1.0f, G = 0.0f, B = 0.0f, A = 0.0f
            };
            m_baseColours[01] = new Colour()
            {
                R = 0.0f, G = 1.0f, B = 0.0f, A = 0.0f
            };
            m_baseColours[02] = new Colour()
            {
                R = 0.0f, G = 0.0f, B = 1.0f, A = 0.0f
            };
            m_baseColours[03] = new Colour()
            {
                R = 1.0f, G = 1.0f, B = 0.0f, A = 0.0f
            };
            m_baseColours[04] = new Colour()
            {
                R = 0.0f, G = 1.0f, B = 1.0f, A = 0.0f
            };
            m_baseColours[05] = new Colour()
            {
                R = 1.0f, G = 0.0f, B = 1.0f, A = 0.0f
            };
            m_baseColours[06] = new Colour()
            {
                R = 1.0f, G = 0.5f, B = 0.5f, A = 0.0f
            };
            m_baseColours[07] = new Colour()
            {
                R = 0.5f, G = 1.0f, B = 0.5f, A = 0.0f
            };
            m_baseColours[08] = new Colour()
            {
                R = 0.5f, G = 0.5f, B = 1.0f, A = 0.0f
            };
            m_baseColours[09] = new Colour()
            {
                R = 1.0f, G = 1.0f, B = 0.5f, A = 0.0f
            };
            m_baseColours[10] = new Colour()
            {
                R = 0.5f, G = 1.0f, B = 1.0f, A = 0.0f
            };
            m_baseColours[11] = new Colour()
            {
                R = 1.0f, G = 0.5f, B = 1.0f, A = 0.0f
            };

            m_random = new Random();

            m_renderer = renderer;

            m_scene = m_renderer.GetNewScene();

            m_scene.CreateCamera();
            m_scene.SetCurrentCamera(0);

            m_basisRenderArrays  = new RenderArrays();
            m_springRenderArrays = new RenderArrays();
            m_bBoxRenderArrays   = new RenderArrays();
            m_renderArrays       = new RenderArrays();

            m_blobs = new BlobArrays();
            m_directionalSprings = new DirectionalSpringArray();
            m_collisions         = new List <Tuple <int, int> >();
            m_cellData           = new CellDataArrays();
            m_BBoxTree           = new BBoxTree();
            m_springs            = new SpringArray();

            m_renderArrays.Positions = m_blobs.Positions;
            m_renderArrays.Colours   = m_blobs.Colours;

            m_bBoxRenderArrays.Colours.Add(m_baseColours[0]);
            m_bBoxRenderArrays.Positions.Add(new Vector2d());

            //InitVisualisationTest();

            for (int i = -0; i < 1; i++)
            {
                bool grow = i == 0 ? true : false;
                InitCell(new Vector2d()
                {
                    X = (float)(250.0f * i + 150.0f), Y = 0
                }, i + 4, grow);
            }
            //InitCell(new Vector2d() { X = -100.0f, Y = 0.0f }, 0, true);

            m_scene.RenderArrays.Add(m_renderArrays);
            //m_scene.RenderArrays.Add(m_bBoxRenderArrays);
            m_scene.RenderArrays.Add(m_springRenderArrays);
            m_scene.RenderArrays.Add(m_basisRenderArrays);

            m_basisRenderArrays.Positions.Add(new Vector2d());
            m_basisRenderArrays.Colours.Add(m_baseColours[5]);
            m_basisRenderArrays.Positions.Add(new Vector2d());
            m_basisRenderArrays.Colours.Add(m_baseColours[5]);
        }
예제 #3
0
        public void UpdateDirectionalSprings(List <Vector2d> positions, List <Vector2d> velocities, RenderArrays springRenderArrays, Colour springColour, List <int> cellIds, List <Vector2d> basis1Array, List <Vector2d> basis2Array)
        {
            for (uint i = 0; i < EndPointIndices.Count; i++)
            {
                int startIndex = (int)StartPointIndices[(int)i];
                int endIndex   = (int)EndPointIndices[(int)i];

                Vector2d startPoint = positions[startIndex];
                Vector2d endPoint   = positions[endIndex];
                Vector2d delta      = endPoint - startPoint;


                // Assumes both spring points are inside the same cell
                Vector2d basis1           = basis1Array[cellIds[startIndex]];
                Vector2d basis2           = basis2Array[cellIds[startIndex]];
                Vector2d transformedDelta = delta;
                transformedDelta.X = Vector2d.DotProduct(delta, basis1);
                transformedDelta.Y = Vector2d.DotProduct(delta, basis2);

                Vector2d equilibriumVector = RestVectors[(int)i];
                float    stiffness         = Stiffnesses[(int)i];

                /*
                 * if (equilibriumVector.X > equilibriumVector.Y)
                 * {
                 *  if (transformedDelta.X < 0.0f)
                 *  {
                 *      //endPoint.X = startPoint.X;
                 *      positions[endIndex] = endPoint;
                 *  }
                 * }
                 * else
                 * {
                 *  if (transformedDelta.Y < 0.0f)
                 *  {
                 *      //endPoint.Y = startPoint.Y;
                 *      positions[endIndex] = endPoint;
                 *  }
                 * }
                 *
                 * delta = endPoint - startPoint;
                 * transformedDelta.X = Vector2d.DotProduct(delta, basis1);
                 * transformedDelta.Y = Vector2d.DotProduct(delta, basis2);*/


                Vector2d lengthBeyondEquilibrium = equilibriumVector - transformedDelta;
                Vector2d restoringForce          = lengthBeyondEquilibrium * stiffness;

                // Assuming contant mass
                velocities[startIndex] -= restoringForce;
                velocities[endIndex]   += restoringForce;

#if DEBUG
                springRenderArrays.Positions.Add((startPoint + endPoint) / 2.0f);
                springRenderArrays.Colours.Add(springColour);
#endif
            }
        }