magnetism simulation

喜你入骨 提交于 2019-12-07 12:21:31

问题


Say I have p nodes on a n by m pixel 2D surface, I want the nodes to be attracted to each other such that the further they are apart the strong the attraction. But if the distance between two nodes, say d(A,B) is less than some threshold say k then they start to repel. Could anyone get me started on some code on how to update the co-ordinates of the nodes over time.

I have something a little like the code below which is start to do the attraction, but looking for some advice. (P.S. I can not use an existing library to do this).

public class node{
 float posX;
 float posY;
}

public class mySimulator{

ArrayList<node> myNodes = new ArrayList<node>();

// Imagine I add a load of nodes to myNodes
myNodes.add(.....

// Now image this is the updating routine that is called at every fixed time increment

public void updateLocations(){
 for(int i =0; i <= myNodes.size(); i++){
  for(int i =0; i <= myNodes.size(); i++){
  myNodes.get(i).posX = myNodes.get(i).posX + "some constant"*(myNodes.get(j).posX -myNodes.get(i).posX);
  myNodes.get(i).posY = myNodes.get(i).posY + "some constant"*(myNodes.get(j).posY -myNodes.get(i).posY);
  }
 }
}

}


}

回答1:


This kinetic model of elastic collisions is completely unrelated to magnetism, but the design might give you some ideas on modeling an ensemble of interacting particles.




回答2:


Say I have p nodes on a n by m pixel 2D surface, I want the nodes to be attracted to each other such that the further they are apart the strong the attraction. But if the distance between two nodes, say d(A,B) is less than some threshold say k then they start to repel.

You realize, of course, that this is not how the physics of magnetism work?

Could anyone get me started on some code on how to update the co-ordinates of the nodes over time.

Nobody will be able to give you code to do this easily, because it's actually a difficult problem.

You can numerically integrate the ordinary differential equations for each particle over time. Given initial conditions for position, velocity, and acceleration vectors in 2D, you'll take a time step, integrate the equations to get the values at the end of the time step, update the values by adding the increment, and then doing it again.

It requires some knowledge of 2D vectors, numerical integration, ordinary differential equations, linear algebra, and physics. Do you know anything about those?

Even if you "make up" your own physical laws governing the interactions between your particles, you'll still have to integrate that set of equations.

I'd recommend looking at Runge-Kutta for systems of ODEs. "Numerical Recipes" has a nice chapter on it, even if you go elsewhere for the implementation.

"NR" is now in its third edition. It's a bit controversial, but the prose is very good.



来源:https://stackoverflow.com/questions/2127813/magnetism-simulation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!