# Euler integration

• October 22nd, 2009, 01:52 PM
Kakashi
Euler integration
Ok so I have to create a Java application that simulates a minimum of 3 spheres moving through a frictionless environment with gravity. The environment should be a confined cube area for the spheres to collide and respond. The application does need to display the positions of the spheres in real time, but the 3D vector positions should be saved for each sphere to an output file for future plotting. Set initial velocities and positions and run the simulation for at least 100 iterations. Design your application such that collisions and their associated responses will occur during the simulation.
I got the pseudocode for this I think but I cant get it into java code for the life of me.

Code :

```void main() { //variables needed by the sim Vector3D cur_S[2*N]; //S(t+delta_t) Vector3D prior_S[2*N]; Vector3D S_derivs[2*N]; float mass[N]; float t; float delta_t;   //set current state to initail conditions for(i = 0; i < N; i++){ mass[i] = mass of particle i; cur_S[2*i] = particle i initial linear momentum; sur_S[2*i+1] = particle i initial position; }   while (game simulation is running){ DoPhysicisSimulationStep(delta_t); for (i = 0; i < M; i ++){ Render particle i at postion cur_S[2*i +1]; } }   //update physics void DoPhysicisSimulationStep(delta_t){ copy cur_S to prior_s //calculate the stae derivative vector for(i = 0; i < N; i++){ S_derivs[2*i] = CalcForce(i); S_derivs[2*i+1] = prior_S[2*i] / mass[i]; } //integrate the equations of motion ExplicitEuler(2*N, cur_S, prior_S, S_derivs, delta_t);   t = t+ delta_t; } }```
I get most of it but I cant seam to get it into code.
• October 22nd, 2009, 06:21 PM
literallyjer
Re: Euler integration
Well, what do you know? Here's a little something to get you started. I basically show you how a method looks, how to declare variables, how to declare arrays, how the main method looks. Do you think you can take it from here?

Code :

```public class Assignment {   public static boolean physicsMethod(int value) { if (value > 0) return true; else return false; }   public static void main(String[] args) { int[] integerArray = new int[5]; float num; }   }```
• October 26th, 2009, 09:06 AM
Kakashi
Re: Euler integration
Thanks for the code. Thats is what I did not get. The loops thats what I can do.:-bd
Ill post the finished code when Im done with it.
• November 2nd, 2009, 08:49 AM
Kakashi
Re: Euler integration
Ok I hit a snag once again :((
I got this far with the codeing
Code :

```import java.lang.Math;     public class Euler {   public static void main(String[] args) {   //data needed int n = 3; //the 3 circles thingys float mass[] = null; float t = 0; float delta_t = 0; Vector3D cur_S[] = null; Vector3D prior_S[] = null; Vector3D s_derives[] = null; Vector3D initialV[] = null;//Initial velocity Vector3D initialP[] = null;//initial position Vector3D postionC[] = null; //current position of object     boolean gameOn = true; //keep game sim going   //set current state to initial conditions for(int i = 0; i < n; i++){ mass[i] = 50; cur_S[2*i] = initialV[i]; cur_S[2*i+1] = initialP[i]; }   while(gameOn == true){ physicSim(delta_t, null, null, null, n, n, postionC); for (int i = 0; i < n; i ++){ postionC[i] = cur_S[2*i +1]; } }   //do physic method     ExplicitEuler(2*n, cur_S, prior_S, s_derives, delta_t, n);   t = t + delta_t; }   private static void ExplicitEuler(int i, Vector3D[] curS, Vector3D[] prior_S, Vector3D[] s_derives, float delta_t, int n) { for(int g =0; g < n; g++){ curS[i] = prior_S[i] + delta_t * s_derives[i]; }   }   private static void physicSim(float delta_t,Vector3D cur_S, Vector3D prior_S, Vector3D s_derives, float mass, int n, Vector3D[] postionC ) { prior_S = cur_S; //calculate the state derivative vector for(int i = 0; i < n; i++){ s_derives[2*i] = CalcForce(i); s_derives[2*i+1] = prior_S[2*i]/mass; }   }       }```
And yes I do have the vecotr3D in a different class file
• November 2nd, 2009, 03:19 PM
helloworld922
Re: Euler integration
I'd imagine setting all your arrays to null is your problem. You need to create an array object in order to put stuff in it:

Code :

`Vector3D cur_S[] = new Vector3D[5];`

Note: Once you create an array of a certain size, it must stay that size. You can't change it! The only way around this is to create a second array, and copy everything you need over to that new array. Also, Java doesn't support operator overloading, so this makes no sense to the compiler:

Code :

`curS[i] = prior_S[i] + delta_t * s_derives[i];`

Instead, you must use a method, or extract the information from the object.
Code :

`curS[i] = vectorAdd(prior_S[i] , scalerMultiply(s_derives[i],delta_t) );`