Example #1
0
 /// Copy Constructor
 public Body(double mass, double radius, Vect2d velocity, Point position, ConsoleColor color)
 {
     Mass     = mass;
     Radius   = radius;
     Velocity = velocity;
     Position = position;
     Color    = color;
 }
Example #2
0
        public static Vect2d ComputeAcceleration(Body body, Body otherBody)
        {
            Vect2d       vector         = new Vect2d(body.Position, otherBody.Position);
            const double G              = 6.673e-11;
            double       squareDistance = vector.SquareLenght;
            double       acceleration   = (G * otherBody.Mass) / squareDistance;

            vector.Rescale(acceleration);
            return(vector);
        }
Example #3
0
        public void Update(double deltaT)
        {
            if (Collision())
            {
                CalculateRadius();
            }
            List <Body> newBodies = new List <Body>();

            newBodies.AddRange(m_lstBodies);

            foreach (Body body in m_lstBodies)
            {
                Vect2d acceleration = new Vect2d(0, 0);
                foreach (Body otherBody in m_lstBodies)
                {
                    if (body != otherBody)
                    {
                        acceleration += MathematicsUtils.ComputeAcceleration(body, otherBody);
                    }
                }
                //Step 1: update position
                Body newBody = new Body(body);
                //todo: find a solution
                //newBody.Position += body.Velocity * deltaT + acceleration * deltaT * deltaT * 0.5;

                //Step 2: compute acceleration
                Vect2d accelerationNext = new Vect2d(0, 0);

                foreach (Body otherBody in m_lstBodies)
                {
                    if (body != otherBody)
                    {
                        accelerationNext += MathematicsUtils.ComputeAcceleration(newBody, otherBody);
                    }
                }
                //Step 3: update Velocity

                newBody.Velocity += (acceleration + accelerationNext) * deltaT * 0.5;
                newBodies.Add(newBody);
            }
            //TODO: make swap method
            // m_lstBodies.swap(newBodies);
        }
Example #4
0
        public bool Collision()
        {
            bool   bIsCollision = false;
            double tolerance    = 0.001;
            int    numBodies    = m_lstBodies.Count;

            for (int index = 0; index < numBodies; index++)
            {
                for (int otherIndex = 0; otherIndex < numBodies; otherIndex++)
                {
                    if (index != otherIndex)
                    {
                        Vect2d vector = new Vect2d(m_lstBodies.ElementAt(index).Position, m_lstBodies.ElementAt(otherIndex).Position);
                        if (vector.SquareLenght < tolerance)
                        {
                            if (m_lstBodies.ElementAt(index).Mass >= m_lstBodies.ElementAt(otherIndex).Mass)
                            {
                                double procentOne = m_lstBodies.ElementAt(index).Mass / (m_lstBodies.ElementAt(index).Mass + m_lstBodies.ElementAt(otherIndex).Mass);
                                double procentTwo = m_lstBodies.ElementAt(otherIndex).Mass / (m_lstBodies.ElementAt(index).Mass + m_lstBodies.ElementAt(otherIndex).Mass);

                                m_lstBodies.ElementAt(index).Velocity = m_lstBodies.ElementAt(index).Velocity *procentOne + m_lstBodies.ElementAt(otherIndex).Velocity *procentTwo;
                                m_lstBodies.ElementAt(index).Mass     = m_lstBodies.ElementAt(index).Mass + m_lstBodies.ElementAt(otherIndex).Mass;

                                m_lstBodies.Remove(m_lstBodies.ElementAt(otherIndex));
                                bIsCollision = true;
                                otherIndex++;
                                numBodies--;
                                if (numBodies == index)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return(bIsCollision);
        }