public void generateEFieldLines() { if (charges.Count == 0) { eFieldLineVerts = null; return; } int numLinesPerCharge = 25; List <VertexPositionColor> tempVerts = new List <VertexPositionColor>(); Random random = new Random(); int lineSegmentsPerLine = 65535 / (charges.Count * numLinesPerCharge); float minLon = 0; float maxLon = (float)Math.PI; float minLat = (float)-Math.PI; float maxLat = (float)Math.PI; int numLoops = (int)Math.Sqrt(numLinesPerCharge); for (int i = 0; i < charges.Count; i++) { // Non-random line starting algorithm PointCharge charge = charges[i]; for (int lonIndex = 0; lonIndex < numLoops; lonIndex++) { float lon = MathHelper.Lerp(minLon, maxLon, (float)lonIndex / (float)numLoops); for (int latIndex = 0; latIndex < numLoops; latIndex++) { float lat = MathHelper.Lerp(minLat, maxLat, (float)latIndex / (float)numLoops); Vector3 startOffset = 2 * charge.radius * Vector3.Transform(Vector3.UnitX, Matrix.CreateRotationY(lon) * Matrix.CreateRotationX(lat)); Vector3 startPos = charge.center + startOffset; VertexPositionColor[] line = calculateVerticesForEFieldLineFromStartPosition(startPos, charge.charge < 0, lineSegmentsPerLine); tempVerts.AddRange(line); } } // Random line starting algorithm /*PointCharge charge = charges[i]; * for (int n = 0; n < numLinesPerCharge; n++) * { * float lon = (float)(random.NextDouble() * Math.PI); * float lat = (float)(random.NextDouble() * MathHelper.TwoPi - Math.PI); * * Vector3 startOffset = 2 * charge.radius * Vector3.Transform(Vector3.UnitX, Matrix.CreateRotationY(lon) * Matrix.CreateRotationX(lat)); * Vector3 startPos = charge.center + startOffset; * * VertexPositionColor[] line = calculateVerticesForEFieldLineFromStartPosition(startPos, charge.charge < 0, lineSegmentsPerLine); * tempVerts.AddRange(line); * }*/ } eFieldLineVerts = tempVerts.ToArray(); }
public PointCharge GetChargeWithID(int id) { for (int i = 0; i < charges.Count; i++) { PointCharge c = charges[i]; if (c.id == id) { return(c); } } return(null); }
public void AddCharge(PointCharge charge) { idCounter++; charges.Add(charge); float minX = float.MaxValue; float minY = float.MaxValue; float minZ = float.MaxValue; float maxX = float.MinValue; float maxY = float.MinValue; float maxZ = float.MinValue; for (int i = 0; i < charges.Count; i++) { PointCharge c = charges[i]; if (c.center.X < minX) { minX = c.center.X; } if (c.center.Y < minY) { minY = c.center.Y; } if (c.center.Z < minZ) { minZ = c.center.Z; } if (c.center.X > maxX) { maxX = c.center.X; } if (c.center.Y > maxY) { maxY = c.center.Y; } if (c.center.Z > maxZ) { maxZ = c.center.Z; } } minX -= 5; minY -= 5; minZ -= 5; maxX += 5; maxY += 5; maxZ += 5; this.SetBounds(minX, minY, minZ, maxX, maxY, maxZ); }
protected override void LoadContent() { device = this.GraphicsDevice; cam = new Camera(new Vector3(-5, 5, 5), -MathHelper.PiOver4, -MathHelper.PiOver4, 0.001f, 0.1f, device); // Setup axes grid grid = new Grid(device, 0.1f); // Test charge testCharge = new PointCharge(device, 1.0f, -1.0f); }
public void DeleteChargeWithID(int id) { for (int i = 0; i < charges.Count; i++) { PointCharge c = charges[i]; if (c.id == id) { charges.RemoveAt(i); generateArrows(); generateEFieldLines(); return; } } }
public static void ModifyChargeID(int id, string property, float value) { PointCharge oldC = sim.GetField().GetChargeWithID(id); if (oldC == null) { console.Log("Charge with ID " + id + " not defined"); return; } float x = oldC.center.X; float y = oldC.center.Y; float z = oldC.center.Z; float charge = oldC.charge * 1000000; if (property == "x") { x = value; } else if (property == "y") { y = value; } else if (property == "z") { z = value; } else if (property == "charge") { charge = value; } else { console.Log("Invalid property \"" + value + "\""); return; } PointCharge c = new PointCharge(sim.GraphicsDevice, new Vector3(x, y, z), charge, id); sim.GetField().DeleteChargeWithID(id); sim.GetField().AddCharge(c); if (shouldAutogen) { sim.GetField().generateArrows(); sim.GetField().generateEFieldLines(); } }
public override Vector3 r(float x, float y, float z) { Vector3 pos = new Vector3(x, y, z); Vector3 sum = Vector3.Zero; for (int i = 0; i < charges.Count; i++) { PointCharge charge = charges[i]; float distance = Vector3.Distance(pos, charge.center); float magnitude = -k * charge.charge / (distance * distance); Vector3 vec = charge.center - pos; vec.Normalize(); vec *= magnitude; sum += vec; } return(sum); }
public void AddCharge(Vector3 pos, float chargeInMicroCoulombs) { PointCharge pointCharge = new PointCharge(this.device, pos, chargeInMicroCoulombs, idCounter); AddCharge(pointCharge); }