//get the force vetor private Vector3 getMagneticDipole(MovingParticle mp) { Vector3 newForce = Vector3.zero; foreach (PermanentMagnet pm in permanentMagnets) { Vector3 position = getOriginPosition(mp, pm); Debug.LogError("Current position " + position.ToString()); // calculate magnetic force float x = position.x; float y = position.y; float z = position.z; float denominator = (float)(4 * Math.PI * Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2))); Vector3 multDirection = new Vector3(3 * z * x, 3 * z * y, (float)(2 * Math.Pow(z, 2) - Math.Pow(x, 2) - Math.Pow(y, 2))); Vector3 force = ((float)(pm.RemanenceField) / denominator) * multDirection; newForce += force; /*if (float.IsNaN(newForce.x)) * { * newForce = Vector3.zero; * }*/ } Debug.LogError("New Force vector: " + newForce.ToString()); //mp.rb.AddForce(newForce); //return newForce; Vector3 newLocation = getNewLocation(newForce, mp); Debug.LogError("New location " + newLocation.ToString()); return(newLocation); }
// get the coordinates of the particle if the permanent magnet was at the origin private static Vector3 getOriginPosition(MovingParticle mp, PermanentMagnet pm) { Vector3 aToB = mp.transform.position - pm.transform.position; Debug.LogError("PM World Position" + pm.transform.position.ToString()); Debug.LogError("MP World Position" + mp.transform.position.ToString()); Debug.LogError("aToB" + aToB.ToString()); //local space Vector3 relativePosition = aToB; //Debug.LogError("Relative Position" + relativePosition.ToString()); return(relativePosition); }
/*--------------------------------------------------------------------------------------*/ /* */ /* Cycle: Cylces through all particles to apply magnetic force */ /* param: */ /* MovingParticle particle - the particle the force is being applied to */ /* */ /*--------------------------------------------------------------------------------------*/ public IEnumerator Cycle(MovingParticle particle) { bool isFirst = true; // Applies force as long as game is running while (true) { // This statement is used to get rid of spike in processing for smooth updating if (isFirst) { isFirst = false; yield return(new WaitForSeconds(Random.Range(0, cycleInterval))); } // Applies force on particle ApplyMagneticForce(particle); yield return(new WaitForSeconds(cycleInterval)); } }
/*--------------------------------------------------------------------------------------*/ /* */ /* ApplyMagneticForce: Applies magnetic force to all moving particles */ /* param: */ /* MovingParticle particle - the particle being moved */ /* */ /*--------------------------------------------------------------------------------------*/ private void ApplyMagneticForce(MovingParticle particle) { // New force is always set at zero during the beginning Vector3 newFocrce = Vector3.zero; // Goes through all charged particles foreach (Particle thisParticle in particles) { // Skips particle so it's force isn't applied to itself if (thisParticle == particle) { continue; } // Taks distance between the current particle in the list and the particle we're applying the force to float distance = Vector3.Distance(particle.transform.position, thisParticle.gameObject.transform.position); // Coulomb's law: Force = (charge * charge) / (Distance * Distance) float force = 200 * particle.charge * thisParticle.charge / Mathf.Pow(distance, 2); // Gets direction the particle should move Vector3 direction = particle.transform.position - thisParticle.transform.position; // Normalize vector since we don't care about the magnitude direction.Normalize(); // A Summation of all forces applied to the moving particle newFocrce += force * direction * cycleInterval; // If force is undefined (divide by zero) the newForce is zero if (float.IsNaN(newFocrce.x)) { newFocrce = Vector3.zero; } Vector3 addedForce = Vector3.ClampMagnitude(newFocrce, maxForce); // Applies force to the Rigidbody2D particle.myRigidbody.AddForce(addedForce); } }
public IEnumerator Cycle(MovingParticle mp) { //Vector3[] positions = new[] { new Vector3(0f, 0f, 0f), new Vector3(1f, 1f, 1f) }; Vector3[] positions = new Vector3[numParticlesPerPath]; for (int i = 0; i < numParticlesPerPath; i++) { positions[i] = getMagneticDipole(mp); // update location of particle mp.transform.position = positions[i]; yield return(null); } Debug.LogError("Last Position in cycle: " + positions[numParticlesPerPath - 1].ToString()); Debug.LogError("First Positions in cycle: " + positions[1].ToString()); //Instantiate(prefabLine); //gameObject.AddComponent<FieldLines>().positions = positions; drawLine(positions); //Debug.LogError("All Positions" + positions.ToString()); Destroy(mp); }
private Vector3 getNewLocation(Vector3 force, MovingParticle mp) { Vector3 newLocation = force + mp.transform.position; return(newLocation); }