We are about to switch to a new forum software. Until then we have removed the registration on this forum.
so im trying to draw two body problem in 2D, it does quite good with out the PVectors, i tried to make the code look better with using vectors and the program doesnt run good for small time steps like it did before (same initial values in both case) how could i make this program better to run faster and calculate precisely?
float G=0.00000667408, m1=10, m2=900000000, t0=0, h; float ti=0; PVector x10, x20, x1, x2, v10, v20, v1, v2, r1e, r2e, v1t, v2t, xx1, xx2; float r1, r2; void setup() { // setup() runs once size(1150, 900); x10=new PVector(450.0,250.0); x20=new PVector(600.0,450.0); v10=new PVector(0.53,-0.15); v20=new PVector(-0.005,0.006); x1=x10.copy(); x2=x20.copy(); xx1=x10.copy(); xx2=x20.copy(); v1=v10.copy(); v2=v20.copy(); v1t=v1.copy(); v2t=v2.copy(); h=0.99; frameRate(500); } void draw() { background(204); ti=t0+h; //here if h is 1 its going "good" but for small values like 0.7 and below they crush r1e=PVector.sub(x2, x1); r2e=PVector.sub(x1, x2); r1=x1.mag(); r2=x2.mag(); v1=PVector.add(v1t, r1e.mult(G*m2*(ti-t0)/(r1*r1*r1))); v2=PVector.add(v2t, r2e.mult(G*m1*(ti-t0)/(r2*r2*r2))); x1=PVector.add(xx1, v1.mult(ti-t0)); x2=PVector.add(xx2, v2.mult(ti-t0)); xx1=x1.copy(); xx2=x2.copy(); v1t=v1.copy(); v2t=v2.copy(); t0=ti; fill(50); ellipse(x1.x, x1.y, 15, 15); ellipse(x2.x, x2.y, 45, 45); }
Answers
here is the original program which not have the PVectors and works for smaller time steps:
does your space bar work?
Pleasedon'tjokeaboutbrokenspacebars.:-(
Seriously though, if you want people to debug your code then make it easy for them to read. An unbroken stream of characters and symbols that long, coupled with all your variables being combinations of the same 3 letters, does not help.
where is the equiv of lines 73-77 in the first bit of code? i don't see it.
In the upper code i have the problem, where it is first order, so if order=0 in the second they are the same. It means:
v1tx=G m2/(r1 r1 r1) r1x (ti-t0)+v1ttx
v1ty=G m2/(r1 r1 r1) r1y (ti-t0)+v1tty
these are the new velocities which we use for the orbital calculation, and my problem is that in the second code i can use small time steps like 0.2 to calculate more precise orbits, and in the first code i need to use times step 1.0 (so h=1 and it works but that means big mistake in the calculation i think)
(use blank lines between paragraphs or it'll run them all together. i have fixed the above formatting)
no idea what yo are saying. pretend i know nothing about the two body problem.
In the original code the 73-77 line is (if we use first order, that means y(n+1)=yn+hy'n+O(h^2) ):
In the PVector code its
here i calculate the velocities and orbits in a 2D vectors, r1e, r2e means the directions. In xx1 and xx2 I store the last (i-1) value to the calculate the i-th value. Id like to use this second compact vector form because i think its easier to read it, but i have problems with the code, its only running if the ti-t0 time step is equal 1.
https://OpenProcessing.org/sketch/437853
I think you have to do this replacement in the PVector code at lines 27 and 28:
However, this does not solve your problem. Few suggestions. When you present two codes, ensure they have the initial set of parameters. This will debugging easier. Then, I'd test limits in your functions to see if both behave the same. For example, making your static planet massive. Do both codes behave the same? In your case, probably not. It could be a simple bug. It is not easy to find since you are working directly on the equations.
The solution to your initial problem relies on this difference:
x1=PVector.add(xx1, v1.mult(ti-t0));
vs.
x1=PVector.add(xx1, PVector.mult(v1,(ti-t0)));
Kf
Yes it solved it, thank you. Do you have any advise how to make it calculate faster? If anyone intrested about the working code here it is:
it's doing 170 fps here.