Exemplo n.º 1
0
        // calulate all gravitational forces in game
        // then will apply the force to the given object
        public void ApplyGravityToObject(GameObject gameObject)
        {
            Vector2 netForce = new Vector2(0f, 0f);
            Vector2 position = gameObject.position;

            foreach (GameObject opposer in asteroids)
            {
                // find unit vector
                Vector2 diffVect = opposer.position - position;
                Vector2 unitVector;
                if (diffVect.Length() == 0.0f) continue;
                unitVector.X = diffVect.X / diffVect.Length();
                unitVector.Y = diffVect.Y / diffVect.Length();

                Vector2 currentForce;
                currentForce.X = (opposer.mass / diffVect.LengthSquared()) * GRAVITATIONAL_CONSTANT * unitVector.X;
                currentForce.Y = (opposer.mass / diffVect.LengthSquared()) * GRAVITATIONAL_CONSTANT * unitVector.Y;
                netForce += currentForce;
            }
            netForce.X = netForce.X * gameObject.mass;
            netForce.Y = netForce.Y * gameObject.mass;

            gameObject.UpdateVelocityWithForce(netForce);
        }