chunkyorkshireman
YaBB Newbies
Offline
Posts: 40
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); } }