Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. 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.

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.  Reply With Quote

3. 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?

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;
float num;
}

}  Reply With Quote

4. Re: Euler integration

Thanks for the code. Thats is what I did not get. The loops thats what I can do. Ill post the finished code when Im done with it.  Reply With Quote

5. Re: Euler integration

Ok I hit a snag once again I got this far with the codeing
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  Reply With Quote

6. 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:

Vector3D cur_S[] = new Vector3D;

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:

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

Instead, you must use a method, or extract the information from the object.
curS[i] = vectorAdd(prior_S[i] , scalerMultiply(s_derives[i],delta_t) );  Reply With Quote