updating object locations
in
Programming Questions
•
3 years ago
I'm pretty new at all this, so.
I'm very, very stuck.
I'm trying to write a program that will draw a pattern of sine waves to the screen by updating the x and y locations of a bunch of "fibers" - objects that leave a trail behind them, basically.
My problem is, i want each line to draw / animate across the page, rather than just appearing (which is what's happening now).
For the life of me i cannot figure out how to work that in with what i've written so far. My code's pasted in below- i would be grateful if anyone could take a look and maybe point out what i ought to change.
Thanks xD
code ~
Fiber [] fiber;
int NUMFIBERS = 150;
float y = 0;
float x;
float nextX;
float nextY;
float angle = 0;
float amp;
float gap;
float freq;
PGraphics collisionMask;
void setup() {
size(800,600);
fiber = new Fiber[NUMFIBERS];
createFibers();//calls a function to create fibers
collisionMask = createGraphics(width, height, JAVA2D);
collisionMask.beginDraw();
// Clear the collision mask's background to white
collisionMask.background(255);
// Draw three black circles on the collision mask
collisionMask.fill(0);
collisionMask.ellipse(0.25*width, 0.50*height, 100, 100);
collisionMask.ellipse(0.50*width, 0.50*height, 200, 200);
collisionMask.ellipse(0.75*width, 0.50*height, 100, 100);
// Tell Processing that we've finished the drawing by calling endDraw()
collisionMask.endDraw();
}
void createFibers () {
for (int i = 0; i < NUMFIBERS; i++)
fiber[i] = new Fiber(x,y);
}
void draw() {
fill(255, 0) ;
for (int i = 0; i < NUMFIBERS; i++){
fiber[i].move();
}
}
class Fiber{
//location
float tempX, tempY;
float nextX, nextY;
int shade;
Fiber(float x, float y) {
tempX=x;
tempY=y;
nextX=x;
nextY=y;
shade = int (250);
}
void move(){
stroke(shade);
if (y<height){
amp=(40);
gap=(4);
freq=(4);
float py=0;
float tempY=0;
float tempX=0;
for(int i=0; i<width; i++){
py = y+sin(radians(angle))*amp;
if (collisionMask.get(int(py), int(i)) != color(0)){
nextY=py;
nextX=i;
line(tempX,tempY,nextX,nextY);
tempX=nextX;
tempY=nextY;
}
angle+=freq;
}
y+=gap;
}
}
}
I'm very, very stuck.
I'm trying to write a program that will draw a pattern of sine waves to the screen by updating the x and y locations of a bunch of "fibers" - objects that leave a trail behind them, basically.
My problem is, i want each line to draw / animate across the page, rather than just appearing (which is what's happening now).
For the life of me i cannot figure out how to work that in with what i've written so far. My code's pasted in below- i would be grateful if anyone could take a look and maybe point out what i ought to change.
Thanks xD
code ~
Fiber [] fiber;
int NUMFIBERS = 150;
float y = 0;
float x;
float nextX;
float nextY;
float angle = 0;
float amp;
float gap;
float freq;
PGraphics collisionMask;
void setup() {
size(800,600);
fiber = new Fiber[NUMFIBERS];
createFibers();//calls a function to create fibers
collisionMask = createGraphics(width, height, JAVA2D);
collisionMask.beginDraw();
// Clear the collision mask's background to white
collisionMask.background(255);
// Draw three black circles on the collision mask
collisionMask.fill(0);
collisionMask.ellipse(0.25*width, 0.50*height, 100, 100);
collisionMask.ellipse(0.50*width, 0.50*height, 200, 200);
collisionMask.ellipse(0.75*width, 0.50*height, 100, 100);
// Tell Processing that we've finished the drawing by calling endDraw()
collisionMask.endDraw();
}
void createFibers () {
for (int i = 0; i < NUMFIBERS; i++)
fiber[i] = new Fiber(x,y);
}
void draw() {
fill(255, 0) ;
for (int i = 0; i < NUMFIBERS; i++){
fiber[i].move();
}
}
class Fiber{
//location
float tempX, tempY;
float nextX, nextY;
int shade;
Fiber(float x, float y) {
tempX=x;
tempY=y;
nextX=x;
nextY=y;
shade = int (250);
}
void move(){
stroke(shade);
if (y<height){
amp=(40);
gap=(4);
freq=(4);
float py=0;
float tempY=0;
float tempX=0;
for(int i=0; i<width; i++){
py = y+sin(radians(angle))*amp;
if (collisionMask.get(int(py), int(i)) != color(0)){
nextY=py;
nextX=i;
line(tempX,tempY,nextX,nextY);
tempX=nextX;
tempY=nextY;
}
angle+=freq;
}
y+=gap;
}
}
}
1