コード例 #1
0
    private Vector3 ComputeInducedMagneticMoment(BarMagnet bm, FerroMagnet fm)
    {
        if (radius < 0.1f)
        {
            return(new Vector3(0.0f, 0.0f, 0.0f));
        }

        float volume    = 1.333f * Mathf.PI * radius * radius * radius;
        float calcScale = 3 * permiability / (3 + permiability);

        Vector3 magnetizeVal = new Vector3(0.0f, 0.0f, 0.0f);

        if (bm)
        {
            Vector3 bmEdgePos = bm.GetClosestEdgePosition(transform.position);
            Vector3 spEdgePos = GetClosestEdgePosition(bmEdgePos);
            magnetizeVal = calcScale * inverseU0 * MagneticSystem.Instance.CalcMField(spEdgePos, bm);
            //Debug.Log("bm" + magnetizeVal);
        }
        else
        {
            Vector3 spEdgePosFM = fm.GetClosestEdgePosition(transform.position);
            Vector3 spEdgePos   = GetClosestEdgePosition(spEdgePosFM);
            magnetizeVal = calcScale * inverseU0 * MagneticSystem.Instance.CalcMField(spEdgePos, fm);
            //Debug.Log("fm" + magnetizeVal);
        }

        return(volume * magnetizeVal);
    }
コード例 #2
0
 public void AddMagnet(FerroMagnet fm)
 {
     if (!fm.Equals(this))
     {
         magnetInfluences.AddMagnet(new MagnetInfluence(null, fm, ComputeInducedMagneticMoment(null, fm)));
     }
 }
コード例 #3
0
    public int AddFerroMagnet(FerroMagnet fm)
    {
        bool exist = false;

        foreach (FerroMagnet fmi in ferroMagnetList)
        {
            if (fmi.Equals(fm))
            {
                exist = true;
            }
        }

        if (exist)
        {
            return(-1);
        }

        ferroMagnetList.Add(fm);

        GameObject[] ferros = GameObject.FindGameObjectsWithTag("Ferro");
        foreach (GameObject ferro in ferros)
        {
            ferro.SendMessage("AddMagnet", fm);
        }

        return(ferroMagnetList.IndexOf(fm));
    }
コード例 #4
0
    private bool BeBlocked(Vector3 p, FerroMagnet fm)
    {
        Vector3 dir = fm.transform.position - p;

        bool       blocked = false;
        RaycastHit hitInfo;

        if (Physics.Raycast(p, dir, out hitInfo, dir.sqrMagnitude))
        {
            if (!hitInfo.collider.Equals(fm.collider))
            {
                blocked = true;
            }
        }
        return(blocked);
    }
コード例 #5
0
    public Vector3 CalcMField(Vector3 position, FerroMagnet fm)
    {
        //Vector3 dir = position - fm.transform.position;
        Vector3 dir = position - fm.GetClosestEdgePosition(position);

        if (dir.sqrMagnitude < 100.0f)
        {
            dir = position - fm.transform.position;
        }

        Vector3 mi    = fm.GetInducedMagneticMoment();
        float   midir = Vector3.Dot(mi, dir.normalized);

        Vector3 field = 3 * midir * dir.normalized - mi;

        field *= Mathf.Pow(dir.magnitude, -3);

        return(k * field);
    }
コード例 #6
0
 public MagnetInfluence(BarMagnet bar, FerroMagnet fs, Vector3 magnetiztion)
 {
     bm = bar;
     fm = fs;
     ma = magnetiztion;
 }
コード例 #7
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);
         */
    }
コード例 #8
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;
                 */
            }
        }
    }
コード例 #9
0
    private Vector3 CalcForce(Vector3 mo, Vector3 p, BarMagnet bmo, FerroMagnet fmo)
    {
        Vector3 force = new Vector3(0.0f, 0.0f, 0.0f);

        foreach (BarMagnet bmi in barMagnetList)
        {
            if (bmo && (bmi.Equals(bmo)))
            {
                continue;
            }

            if (bmo && BeBlocked(bmo, bmi))
            {
                continue;
            }

            if (fmo && fmo.IsAttachedBar(bmi))
            {
                continue;
            }

            Vector3 dir = new Vector3(0.0f, 0.0f, 0.0f);
            if (fmo)
            {
                dir = p - bmi.GetClosestEdgePosition(p);
            }
            else
            {
                dir = p - bmi.transform.position;
            }
            //Vector3 dir = p - bmi.GetClosestEdgePosition(p);
            Vector3 mi = bmi.GetMagneticMoment();

            force += CalcCellForce(dir, mi, mo);

            //Debug.Log((bmo ? "bmo" : "fmo" ) + "force" + force + "dir" + dir + "mi" + mi + "mo" + mo);
        }

        if (!bmo)
        {
            foreach (FerroMagnet fmi in ferroMagnetList)
            {
                if (fmo && (fmi.Equals(fmo)))
                {
                    continue;
                }

                if (fmo && BeBlocked(p, fmi))
                {
                    continue;
                }

                Vector3 dir = p - fmi.GetClosestEdgePosition(p);
                Vector3 mi  = fmi.GetInducedMagneticMoment();

                Vector3 ferroForce = CalcCellForce(dir, mi, mo);

                // ferro Magnet 의 영향은 강한 거 한 개만
                if (ferroForce.sqrMagnitude > force.sqrMagnitude)
                {
                    force = ferroForce;
                }
            }
        }

        return(force);
    }
コード例 #10
0
 public Vector3 CalcForceToFerro(Vector3 mo, Vector3 p, FerroMagnet fmo)
 {
     return(CalcForce(mo, p, null, fmo));
 }