Loading...
Logo
Processing Forum
This is my code, i have tried using the float and i just cant seem to get the faces to move only a certain part,

can someone please guide me thanks a lot in advance...



////////////////////////////////////////////////////////////////////////
PVector pos1, pos2; 
PVector vel1, vel2; 

///////////////////////////////////////////////////////////////////////
void setup() 
size(640, 480); 

pos1 = new PVector (random(0, width), random(0, height));
pos2 = new PVector (random(0, width), random(0, height)); 

vel1 = new PVector (random(-2.0, 2.0), random(-2.0, 2.0)); 
vel2 = new PVector (random(-2.0, 2.0), random(-2.0, 2.0));


//////////////////////////////////////////////////////////////////////
void drawFace() 
strokeWeight(4); 
fill(255, 255, 0); 
ellipse(40, 40, 80, 80); 
point (30 , 30); point (50 , 30); 
arc(40, 40, 50, 50, radians(0), radians(180));
}

//////////////////////////////////////////////////////////////////////
void drawFace2() 
strokeWeight(4); 
fill(255, 255, 0); 
ellipse(140, 40, 80, 80); 
point (130 , 30); point (150 , 30); 
arc(140, 40, 50, 50, radians(0), radians(180));

//////////////////////////////////////////////////////////////////////
void draw() 

background(255); 
drawFace(); 
drawFace();

drawFace2();
drawFace2();

}
//////////////////////////////////////////////////////////////////////

Replies(9)

I did one of them, you should look at what changed and do the same for the second face.

Also, strokeWeight() and fill() never change. If you put them in draw(), then you are updating them every frame, so they should be put in setup() instead:
Copy code
  1. PVector pos1, pos2;
  2. PVector vel1, vel2;

  3. void setup() {
  4.   size(640, 480);
  5.   
  6.   strokeWeight(4); // This never changes, no need to update it in draw()
  7.   fill(255, 255, 0); // This never changes, no need to update it in draw()
  8.   
  9.   pos1 = new PVector(random(0, width), random(0, height));
  10.   pos2 = new PVector(random(0, width), random(0, height));
  11.   vel1 = new PVector(random(-2.0, 2.0), random(-2.0, 2.0));
  12.   vel2 = new PVector(random(-2.0, 2.0), random(-2.0, 2.0));
  13. }

  14. void draw() {
  15.   background(255);
  16.   
  17.   pos1.x += vel1.x;
  18.   pos1.y += vel1.y;
  19.   
  20.   drawFace();
  21.   drawFace2();
  22. }

  23. void drawFace() {
  24.   ellipse(pos1.x+40, pos1.y+40, 80, 80);
  25.   point(pos1.x+30, pos1.y+30);
  26.   point(pos1.x+50, pos1.y+30);
  27.   arc(pos1.x+40, pos1.y+40, 50, 50, radians(0), radians(180));
  28. }

  29. void drawFace2() {
  30.   ellipse(140, 40, 80, 80);
  31.   point(130, 30);
  32.   point(150, 30);
  33.   arc(140, 40, 50, 50, radians(0), radians(180));
  34. }
i forgot to mention i need them to hit of the edges do you know what coding i need im really just tired ive spent 13 hours trying to do this :(
Read this, you need to test if a face has passed the screen on the x or y axis:  http://processing.org/learning/topics/bounce.html
i have its really difficult... ill give it another shot but I'm sure i wont be able to
I have added the following comes up saying void (Drawshape and drawshape2) 

i just dont know is it where i am placing it?

int rad = 60;        // Width of the shape
float xpos, ypos;    // Starting position of shape    

float xspeed = 2.8;  // Speed of the shape
float yspeed = 2.2;  // Speed of the shape

int xdirection = 1;  // Left or Right
int ydirection = 1;  // Top to Bottom


PVector pos1, pos2;
PVector vel1, vel2;

void setup() {
  size(640, 480);
  
  strokeWeight(4); // This never changes, no need to update it in draw()
  fill(255, 255, 0); // This never changes, no need to update it in draw()
  
  pos1 = new PVector(random(0, width), random(0, height));
  pos2 = new PVector(random(0, width), random(0, height));
  vel1 = new PVector(random(-2.0, 2.0), random(-2.0, 2.0));
  vel2 = new PVector(random(-2.0, 2.0), random(-2.0, 2.0));
  
  noStroke();
  frameRate(30);
  ellipseMode(RADIUS);
  // Set the starting position of the shape
  xpos = width/2;
  ypos = height/2;
}

void draw() {
  background(255);
  
  pos1.x += vel1.x;
  pos1.y += vel1.y;
  
  pos2.x += vel2.x;
  pos2.y += vel2.y;
  
  drawFace();
  drawFace2();
  
  // Update the position of the shape
  xpos = xpos + ( xspeed * xdirection );
  ypos = ypos + ( yspeed * ydirection );
  
  // Test to see if the shape exceeds the boundaries of the screen
  // If it does, reverse its direction by multiplying by -1
  if (xpos > width-rad || xpos < rad) {
    xdirection *= -1;
  }
  if (ypos > height-rad || ypos < rad) {
    ydirection *= -1;
}

void drawFace() {
  ellipse(pos1.x+40, pos1.y+40, 80, 80);
  point(pos1.x+30, pos1.y+30);
  point(pos1.x+50, pos1.y+30);
  arc(pos1.x+40, pos1.y+40, 50, 50, radians(0), radians(180));
}

void drawFace2() {
  ellipse(pos2.x+140, pos2.y+40, 80, 80);
  point(pos2.x+130, pos2.y+30);
  point(pos2.x+150, pos2.y+30);
  arc(pos2.x+140, pos2.y+40, 50, 50, radians(0), radians(180));
}

As before, one is done for you.

Copy code
  1. PVector pos1, pos2;
  2. PVector vel1, vel2;

  3. void setup() {
  4.   size(640, 480);

  5.   // Static drawing settings.
  6.   strokeWeight(4);
  7.   fill(255, 255, 0);
  8.   smooth();

  9.   // Initial positions and velocities.
  10.   pos1 = new PVector(random(0, width), random(0, height));
  11.   pos2 = new PVector(random(0, width), random(0, height));
  12.   vel1 = new PVector(random(-2.0, 2.0), random(-2.0, 2.0));
  13.   vel2 = new PVector(random(-2.0, 2.0), random(-2.0, 2.0));
  14. }

  15. void draw() {
  16.   background(255);

  17.   // Update positions.
  18.   pos1.x += vel1.x;
  19.   pos1.y += vel1.y;
  20.   pos2.x += vel2.x;
  21.   pos2.y += vel2.y;

  22.   // Check for collisions.
  23.   if ( pos1.x < 2 || pos1.x > width-82 ) {
  24.     pos1.x=constrain(pos1.x, 2, width-82);
  25.     vel1.x*=-1;
  26.   }
  27.   if ( pos1.y < 2 || pos1.y > height-82 ) {
  28.     pos1.y=constrain(pos1.y, 2, height-82);
  29.     vel1.y*=-1;
  30.   }

  31.   // Draw first face.
  32.   pushMatrix();
  33.   translate(pos1.x, pos1.y);
  34.   drawFace();
  35.   popMatrix();

  36.   // Draw second face.
  37.   pushMatrix();
  38.   translate(pos2.x, pos2.y);
  39.   drawFace();
  40.   popMatrix();
  41. }

  42. // Function to draw one face.
  43. void drawFace() {
  44.   ellipse(40, 40, 80, 80);
  45.   point(30, 30);
  46.   point(50, 30);
  47.   arc(40, 40, 50, 50, radians(0), radians(180));
  48. }

I have this but still says void? help again please :"(

PVector pos1, pos2;
PVector vel1, vel2;

void setup() {
  size(640, 480);

  // Static drawing settings.
  strokeWeight(4);
  fill(255, 255, 0);
  smooth();

  // Initial positions and velocities.
  pos1 = new PVector(random(0, width), random(0, height));
  pos2 = new PVector(random(0, width), random(0, height));
  vel1 = new PVector(random(-2.0, 2.0), random(-2.0, 2.0));
  vel2 = new PVector(random(-2.0, 2.0), random(-2.0, 2.0));
}

void draw() {
  background(255);

  // Update positions.
  pos1.x += vel1.x;
  pos1.y += vel1.y;
  pos2.x += vel2.x;
  pos2.y += vel2.y;

  // Check for collisions.
  if ( pos1.x < 2 || pos1.x > width-82 ) {
    pos1.x=constrain(pos1.x, 2, width-82);
    vel1.x*=-1;
  }
  if ( pos1.y < 2 || pos1.y > height-82 ) {
    pos1.y=constrain(pos1.y, 2, height-82);
    vel1.y*=-1;
  }

  if ( pos2.x < 2 || pos2.x > width-82 ) {
    pos2.x=constrain(pos2.x, 3, width-82);
    vel2.x*=-1;
  }
  if ( pos2.y < 2 || pos2.y > height-82 ) {
    pos2.y=constrain(pos2.y, 3, height-82);
    vel2.y*=-1;
    
    
  // Draw first face.
  pushMatrix();
  translate(pos1.x, pos1.y);
  drawFace();
  popMatrix();

  // Draw second face.
  pushMatrix();
  translate(pos2.x, pos2.y);
  drawFace();
  popMatrix();
}

// Function to draw one face.
void drawFace() {
  ellipse(40, 40, 80, 80);
  point(30, 30);
  point(50, 30);
  arc(40, 40, 50, 50, radians(0), radians(180));
  



}




you forgot this : }

at
Copy code
  1.   if ( pos2.y < 2 || pos2.y > height-82 ) {
        pos2.y=constrain(pos2.y, 3, height-82);
        vel2.y*=-1;
    }
haha thank you very much it is finally done... thanks to everyone that helped me