Programming a n-body problem simulator in JavaScript with three.js

Hi, guys,
I’m glad to share my program, it display the motion of bodies that under law of universal gravitation. You can add/remove body from the space, and modify it’s position, mass and velocity. You can also watch those values changing after click “Start”: https://www.h53d.xyz/static/n3d-objects.html



it will be my pleasure to have any suggestions from you :slight_smile:

3 Likes

I think it looks great. I notice that the system is moving over time, though. Not sure if that is an error or if it is caused by non-zero initial momentum. In either case, I think it would be nice if the OrbitControls.target (assuming you use OrbitControls) were moved with the center of mass.

The system is moving, because the camera was dynamically set to lookAt a moving object of the space. I do this to simulate the perspective of space movement.
And you could pick up other object by mouse click(or finger touch on phone), then the camera would be change to lookAt the object.
Yes, I use the OrbitControls.

Thanks for your message :slight_smile:

I do this to simulate the perspective of space movement.

I am not sure I understand what you mean. The curves that log positions also indicate drifting in one direction. The practical problem for me was that all the action was moving away from the center, and thus harder to follow with the controls. (I changed nothing from the initial setup.)

OutdatedNote that calling `camera.lookAt` has no effect if you apply OrbitControls afterwards. You will have to move OrbitControls.target to follow an object.
EDIT: Now I get it. The camera follows a tiny body from the start, and once that body crashes, the camera is stuck where it crashed. You use the controls correctly. But I still miss an explanation for the drifting of the system.

I notice that the bodies may start with non-zero velocities. I recommend making sure that the total initial momentum is 0 and the mass center is also 0. Maybe you could add a button for automatically enforcing this by calculating minimal offsets to initial positions and velocities that are applied to all bodies (i.e. not changing the behavior of the system in the inertial frame of reference).

An alternative is to add an option for following the center of mass (if that is not supposed to be constant), although knowing that I can click on the largest body to approximate this behavior is also helpful.

Another (minor) issue was that sometimes the curves would disappear. This may be related to the settings of camera.near and camera.far.

Another thing: Make sure to supply OrbitControls with the domElement parameter, so that it doesn’t hijack mouse clicks elsewhere, i.e. in the GUI.

I have supply OrbitControls with renderer.domElement, but failed to get mouse/touch event, so hijack the OrbitControls source code and changed it. Maybe I am not using it the right way :slightly_frowning_face:

The camera postion is fixed, and its lookAt target is moving, maybe this is why the system look like drifting.

At the beginning, one of my purpose is to simulate solar system operation with the planetary parameters. I searched those values from Internet and set them as objects initial state, but them run not well, at least not like our real solar system, so I changed those values…

Well, I’ll change the ‘Reset’ button to set all objects’s initial velocity as 0, but I could not get why need to set mass center as 0. Does it just for setting most of objects in center of view window?

I’ve chang the camera’s parameter ‘near’ to smaller for reducing the probability of curves disappearance.

The camera postion is fixed, and its lookAt target is moving, maybe this is why the system look like drifting.

No, the system is drifting because the sum of momentum is non-zero.

At the beginning, one of my purpose is to simulate solar system operation with the planetary parameters. I searched those values from Internet and set then as objects initial state, but them run not well, at least not like our real solar system, so I changed those values…

OK, there may be bugs too. I can’t tell that by visual inspection. What kind of numerical integration do you use?

Well, I’ll change the ‘Reset’ button to set all objects’s initial velocity as 0

You only need to subtract mass-weighted mean velocity from all, as far as I can tell.

but I could not get why need to set mass center as 0. Does it just for setting most of objects in center of view window?

Yes. You will only need to subtract the computed mass center from all positions.

I’ve chang the camera’s parameter ‘near’ to smaller for reducing the probability of curves disappearance.

For the best quality projection, also make sure the near plane is not farther than necessary to capture all objects.

Yes, the system is drifting, I set those objects as not a balance system, so the system not only look like drifting, but also movig as a total staff.

I just calculate the force between every two objects according to the Law of Universal Gravitation, and add up the forces for earch one object, then calculate the object’s acceleration, velocity and position accrording to Newton’s Three Laws. Not considered take any numberical inegration for calculating yet :sweat_smile:. Do you have any suggestions about the numberical inegration?

Yes, “subtract mass-weighted mean velocity from all” is better than simply set them as 0, I’ll try.

I just want to set the view window follow one object, but not take care of the system center. :slight_smile:

1 Like

Then you are doing some kind of simple numerical integration, like forward Euler. Numerical integration will always be an approximation to the real behavior, because you assume the forces remain constant during the timestep. There exist methods for achieving smaller errors wrt. timestep duration than is achieved with simple Euler integration, but I don’t know them well enough to give advice. If you are going to investigate this, I will be happy to hear what you find out, as it is useful for me too.

Very nice stuff! What you’re doing to propagate the bodies through space and time is indeed numerical integration… More specifically you’re using the leapfrog method, which is actually a little bit better than forward Euler: nice job!

2 Likes

If you’re looking for advise on integrators, I can probably help! What do you need it for and do you want it to have a fixed or adaptive time step???

1 Like

Thank you! The case is that of relatively infrequent control signals, approximately once per 1-10 seconds that I will apply to a dynamical model (which may be of several forms) over the same time step, but with minimal runtime effort and result bias (and not too big programming effort).

I think the Leapfrog integration looks really nice and simple, and the “kick-drift-kick” form even allows adaptive timesteps (but then I would have to adapt the timesteps…). Also, it seems Leapfrog is meant for systems of a particular form.

The Leapfrog article on Wikipedia mentions that it is similar to Verlet integration, another name I have noticed before, and that I consider checking out again soon.

Cool! If conserving energy is important (for instance if you have a pendulum that you don’t want to keep on swinging higher and higher, that said energy conservation usually only becomes an issue if you run a simulation for a very long time), leapfrog is indeed an excellent choice. It’s a second order integrator and it’s very performant. If conserving energy is not important, the best general purpose integrator in terms of accuracy, performance and ease of development is the RK4 routine, which is of the 4’th order. Here are the integrators used in my space app: https://github.com/TheHappyKoala/Harmony-of-the-Spheres/tree/master/src/js/Physics/Integrators

If you have any questions, let me know!

2 Likes

Thank you very much. I have not taken so much attention on numerical integration before, I just wanted to get much better performance and at the same time could make the system run as real ojbects as much not only on PC but also most phones.

Now, I should pay much time on advanced mathimatics :slight_smile:

Your Harmony-of-the-Spheres is a great job, it’s a course for me to learn.

Thanks!

2 Likes

Aceway, HappyKoala: I’m looking for your level of knowledge for doing a prototype of an Associative Search (a 3D forest in each of at least 2 windows to allow for drag-drop between contexts for reclassifications, etc). I have a couple of patents on it (each is impossible to read, but the citations show who we beat out: Patent US20140075004 - System And Method For Fuzzy Concept Mapping, Voting Ontology Crowd Sourcing … - Google Patents . Patent US20170235848 - System and method for fuzzy concept mapping, voting ontology crowd sourcing … - Google Patents . This is used for crowdsourcing creativity, and much more. If you (or anyone else) would like to take on a challenge I will explain more. This patent is a leading patent in machine learning and in statistics so there is some value here. Note that the math in this is not as difficult as what you have already accomplished because positioning is set before display and loaded as json.
Dennis

hi, DennisV, I would Iike to know more about it. Please send message to aceway@qq.com.