Beispiel #1
0
    public Vector3 CalcTorqueToMagnet(int bmIdx)
    {
        BarMagnet bmo = barMagnetList[bmIdx];
        Vector3   mo  = bmo.GetMagneticMoment();

        return(CalcTorque(mo, bmo.transform.position, bmo));
    }
    Vector3 seedSearch(Vector3 saddlepos, int mId, bool isForward)
    {
        int     i   = 0;
        Vector3 pos = saddlepos;

        BarMagnet bm = _magneticSystem.barMagnetList[mId];

        Bnorm = calcBnorm(pos);

        while (Terminate(pos, i++, isForward, out mId) == 0 && (pos - bm.transform.position).sqrMagnitude > gridScale * gridScale)
        {
            if (isForward)           //arrived by forward, then return by backward
            {
                pos -= stepsize * calcBnorm(pos);
            }
            else
            {
                pos += stepsize * calcBnorm(pos);
            }

            Bnorm = calcBnorm(pos);
        }

        return(pos);
    }
Beispiel #3
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);
    }
Beispiel #4
0
    public Vector3 CalcAFieldForVisual(Vector3 position, BarMagnet bm)
    {
        Vector3 dir = position - bm.transform.position;

        Vector3 mi = bm.GetMagneticMoment();

        Vector3 field = Vector3.Cross(mi, dir.normalized) * Mathf.Pow(dir.magnitude, -3);

        return(k * field);
    }
Beispiel #5
0
 void Update()
 {
     if (colBm)
     {
         float checkDist = (colBm.transform.position - transform.position).sqrMagnitude;
         if (checkDist > 40.0f)
         {
             collide = false;
             colBm   = null;
         }
     }
 }
Beispiel #6
0
    public int AddMagnet(BarMagnet bm)
    {
        barMagnetList.Add(bm);

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

        return(barMagnetList.IndexOf(bm));
    }
Beispiel #7
0
    public Vector3 CalcForceToMagnet(int bmIdx)
    {
        BarMagnet bmo = barMagnetList[bmIdx];
        Vector3   mo  = bmo.GetMagneticMoment();

        if (bmo.IsCollide())
        {
            return(new Vector3(0.0f, 0.0f, 0.0f));
        }

        return(CalcForce(mo, bmo.transform.position, bmo, null));
    }
Beispiel #8
0
    void OnCollisionEnter(Collision theObject)
    {
        Transform p = theObject.transform;

        if (p)
        {
            BarMagnet bm = p.GetComponent("BarMagnet") as BarMagnet;
            if (bm)
            {
                colBm   = bm;
                collide = true;
                rb.Sleep();
            }
        }
    }
    void updateSeeds(BarMagnet m, int id, int number)
    {
        Quaternion rot = Quaternion.identity;

        rot.eulerAngles = ellipseRot;        //new Vector3(ellipseRot.x, ellipseRot.y, ellipseRot.z);//new Vector3 (0, 0, 360f/number);

        for (int i = 0; i < number; i++)
        {
            seed[id * number + i] = Vector3.Scale(ellipseScale, seed[id * number + i]);
            seed[id * number + i] = rot * m.transform.localRotation * seed[id * number + i] + ellipseTrans + m.transform.position;

            //Debug.Log("trans"+ ":" + _transform.localPosition.x + " " + _transform.localPosition.y + " " + _transform.localPosition.z);
            //Debug.Log("trans"+ ":" + _transform.position.x + " " + _transform.position.y + " " + _transform.position.z);
        }
    }
Beispiel #10
0
    // Use this for initialization
    void Start()
    {
        _camera      = Camera.main;
        driverMagnet = null;

        if (Application.loadedLevelName == "FerromagneticMatter3")
        {
            isGravitySimul = true;
        }
        distanceMin = _camera.near;
        distanceMax = _camera.far;
        //lastRot = transform.rotation;

        ray             = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        _camera.enabled = false;
    }
    // 1 ellipse for Each magnet
    void initSeedPos(BarMagnet m, int id, int number)
    {
        int        i   = 0;
        Quaternion rot = Quaternion.identity;

        rot.eulerAngles = new Vector3(360f / number, 0, 0);

        // the 1st seed
        seed[id * number].Set(0, 1, 0);

        for (i = 1; i < number; i++)
        {
            seed[id * number + i] = rot * seed[id * number + i - 1];
            //Debug.Log("pos"+ i + ":" + initpos[i].x + " " + initpos[i].y + " " + initpos[i].z);
        }
    }
Beispiel #12
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);
    }
Beispiel #13
0
    public Vector3 CalcMFieldForVisual(Vector3 position, BarMagnet bm)
    {
        Vector3 dir = position - bm.transform.position;


        Vector3 mi    = bm.GetMagneticMoment();
        float   midir = Vector3.Dot(mi, dir.normalized);

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

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

        return(field);
        //return k * field

        //return Vector3.zero;
    }
Beispiel #14
0
    public Vector3 CalcMField(Vector3 position, BarMagnet bm)
    {
        Vector3 dir = position - bm.GetClosestEdgePosition(position);

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

        Vector3 mi    = bm.GetMagneticMoment();
        float   midir = Vector3.Dot(mi, dir.normalized);

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

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

        return(k * field);
    }
Beispiel #15
0
    private Vector3 CalcTorque(Vector3 mo, Vector3 p, BarMagnet bmo)
    {
        Vector3 torque = 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;
            }

            Vector3 dir = p - bmi.transform.position;
            //Vector3 dir = p - bmi.GetClosestEdgePosition(p);
            Vector3 mkcrossdir = Vector3.Cross(mo, dir.normalized);

            Vector3 mi    = bmi.GetMagneticMoment();
            float   midir = Vector3.Dot(mi, dir.normalized);

            Vector3 mkcrossmi = Vector3.Cross(mo, mi);

            Vector3 cellTorque = 3 * midir * mkcrossdir - mkcrossmi;

            if (dir.sqrMagnitude > 100.0f)
            {
                cellTorque /= Mathf.Pow(dir.magnitude, 3);
            }
            else
            {
                cellTorque /= 1000.0f;
            }

            torque += k * cellTorque;
        }

        return(torque);
    }
Beispiel #16
0
    private bool BeBlocked(BarMagnet b1, BarMagnet b2)
    {
        Vector3 b2Edge = b2.GetClosestEdgePosition(b1.transform.position);
        Vector3 b1Edge = b1.GetClosestEdgePosition(b2.transform.position);
        Vector3 dir    = b2Edge - b1Edge;

        bool       blocked = false;
        RaycastHit hitInfo;

        if (Physics.Raycast(b1Edge - dir * 0.1f, dir, out hitInfo, dir.sqrMagnitude))
        {
            if (!hitInfo.collider.transform.parent.Equals(b2.transform))
            {
                blocked = true;
            }
            //Debug.Log (hitInfo.collider);
        }

        //Debug.Log("blocked" + blocked);
        return(blocked);
    }
Beispiel #17
0
    public Vector3 CalcMFieldForVisualCompass(Vector3 position, BarMagnet bm)
    {
        Vector3 nDir = position - bm.GetNorthEdgePosition();
        Vector3 sDir = position - bm.GetSouthEdgePosition();
        Vector3 dir  = position - bm.transform.position;

        float edgefactor   = 0.15f;
        float centorfactor = 0.7f;

        if (dir.sqrMagnitude < 0.1f * bm.GetSize())
        {
            edgefactor   = 0.0f;
            centorfactor = 1.0f;
        }

        Vector3 mi  = bm.GetMagneticMoment() * edgefactor;
        Vector3 mic = bm.GetMagneticMoment() * centorfactor;

        float minDir = Vector3.Dot(mi, nDir.normalized);
        float misDir = Vector3.Dot(mi, sDir.normalized);
        float miDir  = Vector3.Dot(mi, dir.normalized);

        Vector3 nField = 3 * minDir * nDir.normalized - mi;

        nField *= Mathf.Pow(nDir.magnitude, -3);

        Vector3 sField = 3 * misDir * sDir.normalized - mi;

        sField *= Mathf.Pow(sDir.magnitude, -3);

        Vector3 cField = 3 * miDir * dir.normalized - mic;

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

        Vector3 field = nField + sField + cField;

        return(k * field);
    }
Beispiel #18
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;
                 */
            }
        }
    }
Beispiel #19
0
    // Update is called once per frame
    void Update()
    {
        if (MagTile.checkMagTile)
        {
            mouseX   = Input.GetAxis("Mouse X") * xSpeed;
            mouseY   = Input.GetAxis("Mouse Y") * ySpeed;
            distance = Input.GetAxis("Mouse ScrollWheel") * zSpeed;

            if (Input.GetButtonDown("Fire1") || Input.GetButtonDown("Fire2"))
            {
                Ray        ray = _camera.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;
                float      castDist = 100.0f;
                if (isGravitySimul)
                {
                    castDist = 1000.0f;
                }

                if (Physics.Raycast(ray, out hitInfo, castDist))
                {
                    if (isGravitySimul)
                    {
                        hitDist = hitInfo.distance;
                    }
                    if (hitInfo.collider && hitInfo.collider.transform.parent && hitInfo.collider.transform.parent.tag == "MagnetBar")
                    {
                        driverMagnet = hitInfo.collider.transform.parent.GetComponent("BarMagnet") as BarMagnet;

                        /*if (driverMagnet && !driverMagnet.GetDriverSetting()) {
                         *      driverMagnet = null;
                         * }*/
                        hitDist = hitInfo.distance;
                    }
                }
            }

            if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                if (driverMagnet)
                {
                    driverMagnet.transform.Rotate(Vector3.forward, 90, Space.Self);
                }
            }

            if (driverMagnet)
            {
                if (Input.GetMouseButton(0))
                {
                    //hitDist += distance;
                    Vector3 pos = new Vector3(mouseX, distance, mouseY);                            //_camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, hitDist-0.1f));

                    if (isGravitySimul)
                    {
                        float x = driverMagnet.transform.position.x;
                        float y = pos.y;
                        float z = driverMagnet.transform.position.z;

                        driverMagnet.transform.position = new Vector3(x, y, z);
                    }
                    else
                    {
                        //pos.y += distance;
                        driverMagnet.transform.position += pos;
                    }
                }
                if (Input.GetMouseButton(1))                         // Rotate magnet
                //Quaternion rotation = Quaternion.Euler(0, 0, mouseX);
                {
                    driverMagnet.transform.Rotate(Vector3.forward, 5, Space.Self);
                }
            }
            else                             //no driverMagnet
            {
                if (Input.GetMouseButton(0)) // Move view
                {
                    transform.position += transform.rotation * new Vector3(-mouseX, -mouseY, 0);
                }


                if (Input.GetMouseButton(1))                         // Rotate view
                {
                    Quaternion rotation = Quaternion.Euler(-mouseY, mouseX, 0);
                    //transform.rotation *= rotation;
                    Physics.Raycast(ray, out hit);
                    transform.RotateAround(hit.point, camera.transform.up, mouseX);
                    transform.RotateAround(hit.point, camera.transform.right, -mouseY);
                }

                transform.position += transform.rotation * new Vector3(0, 0, distance);
            }

            if (Input.GetButtonUp("Fire1") || Input.GetMouseButtonUp(1))
            {
                if (driverMagnet)
                {
                    driverMagnet = null;
                }
                mouseY = mouseX = 0;
            }
        }
    }
Beispiel #20
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);
    }
Beispiel #21
0
 public MagnetInfluence(BarMagnet bar, FerroMagnet fs, Vector3 magnetiztion)
 {
     bm = bar;
     fm = fs;
     ma = magnetiztion;
 }
Beispiel #22
0
 public void AddMagnet(BarMagnet bm)
 {
     magnetInfluences.AddMagnet(new MagnetInfluence(bm, null, ComputeInducedMagneticMoment(bm, null)));
 }
Beispiel #23
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);
         */
    }