Ejemplo n.º 1
0
    // 자석에 붙은 경우만 고려한다.
    public Vector3 GetInducedMagneticMoment()
    {
        Vector3 mm = new Vector3(0.0f, 0.0f, 0.0f);

        for (int i = 0; i < magnetInfluences.Count; ++i)
        {
            MagnetInfluence mi = magnetInfluences[i];

            if (mi.attached)
            {
                mm += transform.localScale.x * mi.Magnetization;
            }
        }

        return(mm);
    }
Ejemplo n.º 2
0
    public bool IsAttachedBar(BarMagnet bm)
    {
        bool attached = false;

        for (int i = 0; i < magnetInfluences.Count; ++i)
        {
            MagnetInfluence mi = magnetInfluences[i];

            if (mi.Bar && mi.Bar.Equals(bm) && mi.attached)
            {
                attached = true;
                break;
            }
        }

        return(attached);
    }
Ejemplo n.º 3
0
 public void AddMagnet(MagnetInfluence m)
 {
     arMI.Add(m);
 }
Ejemplo n.º 4
0
    private Vector3 ComputeInducedMagneticMoment()
    {
        // 0은 기본으로 막대자석이라 가정한다.
        BarMagnet inflBar = magnetInfluences.Count > 0 ? magnetInfluences[0].Bar : null;

        if (!inflBar)
        {
            return(new Vector3(0.0f, 0.0f, 0.0f));
        }

        Vector3 dir  = inflBar.transform.position - transform.position;
        float   dist = dir.sqrMagnitude;

        if (magnetInfluences[0].attached)
        {
            dist = float.MaxValue;
        }

        FerroMagnet inflFm = null;

        for (int i = 1; i < magnetInfluences.Count; ++i)
        {
            MagnetInfluence mi = magnetInfluences[i];
            dir = mi.Bar ? (mi.Bar.transform.position - transform.position) : (mi.FerroSphere.transform.position - transform.position);
            if (!mi.attached && dir.sqrMagnitude < dist)
            {
                dist    = dir.sqrMagnitude;
                inflBar = mi.Bar ? mi.Bar : null;
                inflFm  = mi.FerroSphere ? mi.FerroSphere : null;
            }
        }

        Vector3 cmm = ComputeInducedMagneticMoment(inflBar, inflFm);
        Vector3 imm = GetInducedMagneticMoment();

        BarMagnet attachedBar = null;
        float     maxDipole   = 0.0f;

        for (int i = 0; i < magnetInfluences.Count; ++i)
        {
            MagnetInfluence mi = magnetInfluences[i];
            if (mi.Bar && mi.attached && (maxDipole < mi.Bar.dipoleMoment))
            {
                attachedBar = mi.Bar;
            }
        }

        if (inflBar && attachedBar && dist > 50.0f)
        {
            //Debug.Log (imm);
            return(imm);
        }

        //Debug.Log(cmm);

        return(cmm);

        /*
         * if (imm.sqrMagnitude < cmm.sqrMagnitude) {
         *      Debug.Log ("cmm");
         *      return cmm;
         * }
         *
         * BarMagnet attachedBar = null;
         * float maxDipole = 0.0f;
         *
         * for (int i = 0; i < magnetInfluences.Count; ++i) {
         *      MagnetInfluence mi = magnetInfluences[i];
         *      if (mi.attached && (maxDipole < mi.Bar.dipoleMoment)) {
         *              attachedBar = mi.Bar;
         *      }
         * }
         *
         * if (attachedBar && dist < 50.0f)	{
         *      Debug.Log(dist + "imm" + imm.sqrMagnitude);
         *      return imm * 10.0f;
         *      //return ComputeInducedMagneticMoment(attachedBar, inflFm);
         * }
         *
         * Debug.Log("null");
         * return new Vector3(0.0f, 0.0f, 0.0f);
         */
    }
Ejemplo n.º 5
0
    void OnCollisionEnter(Collision theObject)
    {
        Transform p = theObject.transform;

        if (p)
        {
            BarMagnet   bm = p.GetComponent("BarMagnet") as BarMagnet;
            FerroMagnet fm = p.GetComponent("FerroMagnet") as FerroMagnet;

            if (!bm && p.transform.parent)
            {
                bm = p.transform.parent.GetComponent("BarMagnet") as BarMagnet;
            }

            if (bm)
            {
                rb.Sleep();
                bm.RB.Sleep();
                FixedJoint fj0 = rb.gameObject.AddComponent("FixedJoint") as FixedJoint;
                fj0.connectedBody = bm.RB;
                fj0.anchor        = theObject.contacts[0].point;

                FixedJoint fj1 = bm.RB.gameObject.AddComponent("FixedJoint") as FixedJoint;
                fj1.connectedBody = rb;
                fj1.anchor        = theObject.contacts[0].point;

                for (int i = 0; i < magnetInfluences.Count; ++i)
                {
                    MagnetInfluence mi = magnetInfluences[i];
                    if (mi.Bar && mi.Bar.Equals(bm))
                    {
                        mi.attached      = true;
                        mi.Magnetization = ComputeInducedMagneticMoment(bm, null);

                        //Debug.Log("Last" + mi.Magnetization);

                        MagneticSystem.Instance.AddFerroMagnet(this);
                    }
                }
            }
            else if (fm)
            {
                rb.Sleep();
                //fm.rb.Sleep();

                /*
                 * for (int i = 0; i < magnetInfluences.Count; ++i) {
                 *      MagnetInfluence mi = magnetInfluences[i];
                 *      if (mi.FerroSphere) {
                 *              mi.attached = true;
                 *              mi.Magnetization = ComputeInducedMagneticMoment(null, fm);
                 *      }
                 * }
                 */

                /*
                 * FixedJoint fj0 = rb.gameObject.AddComponent("FixedJoint") as FixedJoint;
                 * fj0.connectedBody = fm.rb;
                 * fj0.anchor = theObject.contacts[0].point;
                 *
                 * FixedJoint fj1 = fm.rb.gameObject.AddComponent("FixedJoint") as FixedJoint;
                 * fj1.connectedBody = rb;
                 * fj1.anchor = theObject.contacts[0].point;
                 */
            }
        }
    }