コード例 #1
0
ファイル: Magnet.cs プロジェクト: zoox101/Magnetize
 protected void Start()
 {
     player = GameObject.Find ("Player");
     magnet = gameObject;
     body = player.GetComponent<Rigidbody>();
     controller = player.GetComponent<PlayerController> ();
     playerTotalForce = player.GetComponent<TotalForce>();
 }
コード例 #2
0
        public void CalculateForces(double t)
        {
            TotalForce.X = 0; TotalForce.Y = 0;

            foreach (Force f in Forces) // merge all applied forces into one vector, then use it as acceleration
            {
                if (f.GetType() == typeof(Gravity))
                {
                    TotalForce += ((f.Direction * f.Amplitude) * Mass); // G in Newtons
                }
                else if (f.GetType() == typeof(AirResistance))
                {
                    var ar = (f as AirResistance);
                    ar.DragCoefficient = 1.05;
                    ar.ObjectVelocity  = Velocity.Length;
                    ar.Direction       = -(TotalForce.Normalized());
                    ar.Area            = Volume / Depth;
                    TotalForce        += (f.Direction * f.Amplitude); // other in Newtons
                }
                else
                {
                    TotalForce += (f.Direction * f.Amplitude); // other in Newtons
                }
            }

            // http://en.wikipedia.org/wiki/Equations_for_a_falling_body

            if (t != 0)
            {
                Velocity += (TotalForce / Mass) / GUI.FPS;
                // 2
                //this.PositionX += ((TotalForce.X / Mass) * t) - (Velocity.X / (2 * t));
                //this.PositionY += ((TotalForce.Y / Mass) * t) - (Velocity.Y / (2 * t));

                PositionX += (Velocity.X - (TotalForce.X / (Mass * 2))) / GUI.FPS;
                PositionY += (Velocity.Y - (TotalForce.Y / (Mass * 2))) / GUI.FPS;
            }
        }