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 & HelpSyntax Questions › resetting the origin
Page Index Toggle Pages: 1
resetting the origin? (Read 1391 times)
resetting the origin?
Aug 19th, 2009, 9:03am
 
Hi there, i have just recently taken up processing and this is my first attempt at any code - which i obviously have a problem with. Apologees if this is a daft question?

The code below simply creates some agents with a random heading which move forward ahead-left, ahead or ahead-right. An agent is a line of two points, current xy and next xy.

I seems to work ok, other than they revert back to standard origin, after i set them up at centre. I know that can't pass data from setup to draw so how could i fix this please?

Thanks for any help.

the code:

Ant [] army = new Ant [300];

void setup() {
 size(300,300);
 smooth();
 for (int i = 0; i < army.length; i++) {
 army[i] = new Ant();
 }
}

void draw() {
 background(255);
 for (int i = 0; i < army.length; i++) {
   army[i].heading(army);
   army[i].draw();
 }
}

class Ant {
 PVector position;
 PVector heading;
 int [][] coordinates = { {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1}, {-1,-1}, {-1,0}, {-1,1} };  
 int dir;  

 Ant() {
   position = new PVector(width/2,height/2); //initialise at centre
   float a = random(8);
   int b = int(a);
   //ensure elements of same 1Darray,  
   heading = new PVector(coordinates[b][0], coordinates[b][1]);
   translate(position.x,position.y);
   line(0,0,heading.x,heading.y);
 }

 void heading(Ant [] army) {
   //set heading
   float a = random(-2, 2); //generate random number -1, 0 or 1
   int b = int(a); //generated number defines heading
   dir = (dir + b);    
   if (dir == -1) {int(dir = 7);} //loop around
   if (dir == 8) {int(dir = 0);}
   // constrain to screen:
   heading.x = (heading.x + width) % width;
   heading.y = (heading.y + width) % width;
 }
 
 void draw() {
   //draw a line, current xy to next xy
   position = heading.get();
   //current xy becomes previous next xy
   heading = new PVector(position.x+coordinates[dir][0],  position.y+coordinates[dir][1]); //new next xy
   line(position.x,position.y,heading.x,heading.y);
 }
}
Re: resetting the origin?
Reply #1 - Aug 19th, 2009, 11:53am
 
Can you clarify what is your problem or what you are trying to do?

You don't need to pass data from setup() to draw() (beside, you can, using "global" variables): the Ant objects can carry such data. You can pass a parameter (or more) to the Ant constructor, for example.
Re: resetting the origin?
Reply #2 - Aug 19th, 2009, 12:15pm
 
the setup position is 0,0, which i translate to centre of window
but immediatly after when they start to move they jump to standard origin; 0,0 in the top left hand corner.

I want them to move out from the centre.

thanks
Re: resetting the origin?
Reply #3 - Aug 19th, 2009, 12:27pm
 
You do: position = heading.get(); where heading is initially set to one of coordinates, ie. near origin.
You also might want to update the PVector instead of creating a bunch of new ones, it would lead to better performances.
Re: resetting the origin?
Reply #4 - Aug 24th, 2009, 2:51am
 
Thanks PhiLho,

forgive me if i'm being a total noob here but i thought the problem is that in setup i translate the starting position (origin of 0,0) to the centre of the window.

then in draw i make the currentXY the previous nextXY - and as the nextXY is one of coordinates (which is an array of moores neighbourhood, being 0,1; 1,0 etc) it causes the agent to jump to the top-left corner of window, as this is the standard origin. After this the programme works fine - so i think its that having translated the origin in setup to the centre it is jumping back to standard topleft corner.

how can i stop it jumping back - i really hope i'm not misisng the point in your last mail, apols if so - just trying to understand. Thankyou for any help.

best.
Re: resetting the origin?
Reply #5 - Aug 24th, 2009, 3:28am
 
chunkyorkshireman wrote on Aug 24th, 2009, 2:51am:
in setup i translate the starting position (origin of 0,0) to the centre of the window.

You do a call to translate() in Ant constructor, but it is useless: translate() is a global state, each call does an additional translation from previous ones (resulting in a big translation!) and the draw() call resets it anyway.
You have to do pushMatrix(), translate() and popMatrix() around the line() call in Ant's draw().
Re: resetting the origin?
Reply #6 - Aug 24th, 2009, 3:33am
 
OK, quick hack to show the idea:
Code:
class Ant {
PVector position;
PVector heading;
int [][] coordinates = { {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1}, {-1,-1}, {-1,0}, {-1,1} };
int dir;

Ant() {
float a = random(8);
int b = int(a);
//ensure elements of same 1Darray,
heading = new PVector(coordinates[b][0], coordinates[b][1]);
}

void heading(Ant [] army) {
//set heading
float a = random(-2, 2); //generate random number -1, 0 or 1
int b = int(a); //generated number defines heading
dir = (dir + b);
if (dir == -1) {int(dir = 7);} //loop around
if (dir == 8) {int(dir = 0);}
// constrain to screen:
heading.x = heading.x % width;
heading.y = heading.y % width;
}

void draw() {
//draw a line, current xy to next xy
position = heading.get();
//current xy becomes previous next xy
heading.x = position.x+coordinates[dir][0];
heading.y = position.y+coordinates[dir][1]; //new next xy
pushMatrix();
translate(width/2, height/2); //move to centre
line(position.x,position.y,heading.x,heading.y);
popMatrix();
}
}
Re: resetting the origin?
Reply #7 - Aug 24th, 2009, 4:51am
 
solved it now with your suggestion - the translate just moved the problem to the centre - i omitted that bit and added;
heading = new PVector(width/2 + coordinates[b][0], height/2 + coordinates[b][1]);  in the setup.

now they swarm out from the centre!!  Smiley

THANKYOU.
Page Index Toggle Pages: 1