We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Re: Traer Physics Attraction Trouble
Page Index Toggle Pages: 1
Re: Traer Physics Attraction Trouble (Read 1714 times)
Re: Traer Physics Attraction Trouble
Mar 4th, 2010, 2:43am
 
You need to create all the particles before you create the attractions. In your code you are trying to calculate the attractions between none existent particles.

Also in the code where you create attractions you have
Code:
     if(j != i) {
       f[i][j] = physics.makeAttraction(p[i], p[j], k, 1);
     }
     else {
       f[i][j] = physics.makeAttraction(p[i], p[j], 0.0, 0.0);
     }

this is obviously to prevent attractions between a particle and itself but this is what happens in the else part. Making a few changes we have the following code which solves the null reference exception problem.
Cheesy

Code:
import traer.physics.*;

int n = 10, k = -5;
ParticleSystem physics;
Particle[] p = new Particle[n], a = new Particle[n];
Spring[] s = new Spring[n];
Attraction[][] f = new Attraction[n][n];

void setup() {
 size(400,400);
 smooth();
 fill(255, 200);
 stroke(255, 200);
 ellipseMode( CENTER );

 physics = new ParticleSystem( 1, 0.05 );

 for(int i = 0; i < n; i++) {
   p[i] = physics.makeParticle( 1.0, width/2, height/2, 0 );
   a[i] = physics.makeParticle( 1.0, random(0,width), height/2, 0 );

   a[i].makeFixed();
   s[i] = physics.makeSpring( p[i], a[i], 0.5, 0.1, 75 ); // Connects anchor to particle
 }
 // If this is commented out then the pde runs---Start
 // Create an attraction between all free particles
 for(int i = 0; i < n; i++) {
   for(int j = 0; j < n; j++) {
     if(j != i) {
       f[i][j] = physics.makeAttraction(p[i], p[j], k, 1);
     }
   }
 }
 // If this is commented out then the pde runs---End

}

void draw() {
 physics.tick();

 //background( 51, 181, 74 ); // Green
 background( 0, 182, 235 ); // Cyan

 for(int i = 0; i < n; i++) {
   line( p[i].position().x(), p[i].position().y(), a[i].position().x(), a[i].position().y() );
   ellipse( a[i].position().x(), a[i].position().y(), 5, 5 );
   ellipse( p[i].position().x(), p[i].position().y(), 20, 20 );
 }
}
Re: Traer Physics Attraction Trouble
Reply #1 - Mar 4th, 2010, 2:48am
 
Ahhhh, thanks so much! Also, I think I accidentally deleted the initial question so that is posted below:


I just started using Traer Physics and I am testing this based on the Simple Pendulum example on the website. I want to take a bunch of this simple Pendulums and apply Attraction to all free moving particles. I came up with this but I get a NullPointerException at physics.tick();

I tried looking it up on the forums and it looks like I'm declaring the Attraction correctly. Any thoughts or examples that might help?

Code:

import traer.physics.*;

int n = 10, k = -5;
ParticleSystem physics;
Particle[] p = new Particle[n], a = new Particle[n];
Spring[] s = new Spring[n];
Attraction[][] f = new Attraction[n][n];

void setup() {
 size(400,400);
 smooth();
 fill(255, 200);
 stroke(255, 200);
 ellipseMode( CENTER );

 physics = new ParticleSystem( 1, 0.05 );

 for(int i = 0; i < n; i++) {
   p[i] = physics.makeParticle( 1.0, width/2, height/2, 0 );
   a[i] = physics.makeParticle( 1.0, random(0,width), height/2, 0 );

   a[i].makeFixed();
   s[i] = physics.makeSpring( p[i], a[i], 0.5, 0.1, 75 ); // Connects anchor to particle

   // If this is commented out then the pde runs---Start
   // Create an attraction between all free particles
   for(int j = 0; j < n; j++) {
if(j != i) {
 f[i][j] = physics.makeAttraction(p[i], p[j], k, 1);
}
else {
 f[i][j] = physics.makeAttraction(p[i], p[j], 0.0, 0.0);
}
   }
   // If this is commented out then the pde runs---End

 }
}


void draw() {
 physics.tick();

 //background( 51, 181, 74 ); // Green
 background( 0, 182, 235 ); // Cyan

 for(int i = 0; i < n; i++) {
   line( p[i].position().x(), p[i].position().y(), a[i].position().x(), a[i].position().y() );
   ellipse( a[i].position().x(), a[i].position().y(), 5, 5 );
   ellipse( p[i].position().x(), p[i].position().y(), 20, 20 );
 }
}
Re: Traer Physics Attraction Trouble
Reply #2 - Mar 5th, 2010, 1:52am
 
> I get a NullPointerException  at physics.tick()

is there a stack trace that goes along with that? they contain useful information...

i guess that physics may not be initialised during the call to draw (draw is asynchronous and i often get the feeling that the draw loop starts before setup() finishes). try putting a check for null around it your call to tick. or a try catch.
Re: Traer Physics Attraction Trouble
Reply #3 - Mar 9th, 2010, 3:45am
 
whose code do you get a NullPointer, mine or Quark's?

It's counter intuitive, but my post is actually the original question and Quark's is the answer.
Page Index Toggle Pages: 1