code_junkie

C# foreach within a foreach loop

Nov 12th, 2011
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. // Integrate forces for each body.
  2. foreach (RigidBodyBase body in doc.Bodies)
  3. {
  4. // Don't move background-anchored bodies.
  5. if (body.anchored) continue;
  6.  
  7. // This is where we will add Each Body's gravitational force
  8. // to the total force exerted on the object.
  9.  
  10. // For each other body, get it's point and it's mass.
  11.  
  12. // Find the gravitational force exterted between target body and looped body.
  13. // Find distance between bodies.
  14. // vector addition
  15. // Force = G*mass1*mass2/distance^2
  16. // Find vector of that force.
  17. // Add Force to TotalGravityForce
  18. // loop until there are no more bodies.
  19. // Add TotalGravityForce to body.totalForce
  20.  
  21. }
  22.  
  23. foreach( RigidBodyBase body in doc.Bodies)
  24. foreach ( RigidBodyBase otherBody in doc.Bodies)
  25. if (!otherBody.Anchored && otherBody != body) // or otherBody.Id != body.Id -- whatever is required...
  26. // then do the work here
  27.  
  28. foreach (RigidBodyBase body in doc.Bodies)
  29. body.TotalForce += body.GravityForce;
  30.  
  31. int l = doc.Bodies.Count;
  32. for ( int i = 0; i < l; i++ )
  33. for ( int j = i + 1; j < l; j++ )
  34. // Do stuff
  35.  
  36. foreach (RigidBodyBase body in doc.Bodies)
  37. {
  38. Integrateforces(ref body, Bodies);
  39. }
  40.  
  41. ...
  42.  
  43. public void Integrateforces(RigidBodyBase out body, RigidBodyBase[] Bodies)
  44. {
  45. //Put your integration logic here
  46. }
Add Comment
Please, Sign In to add comment