I tried to do to fix this issue, but it didn’t work well… :/…
You know this example is bouncing ball and I want to show the obstacle until ball hits obstacle. I want to get some help to solve. :)… (The problem is this code showed the obstacle one time, I just need to show the obstacle before ball hits the obstacle.
Here is my code:
/* the speed of x and y , the position of x and y /
float xspeed; // x-velocity
float yspeed; // y-velocity
float xpos; // x-position
float ypos; // y-position
boolean stop=false; // Controller about changing ball’s color
boolean obstacledraw=false;
void setup(){
frameRate(40);
size(600,400); // set Canvas’ size
noStroke(); // Disables drawing the stroke.
colorMode(HSB,360,100,100); // changes the way Processing interprets color data.
fill(290,100,100,255); // set ball’s initial color and screen before we push the key button
background(0);
noLoop();
/ set initial position about x and y, create initial speed of x and y */
xpos = width/2; // xpos = 300
ypos = height/2; // ypos = 200
xspeed=random((width/150),(width/100)); // Velocity of x [4, 6]
yspeed=random((height/100),(height/80)); // Velocity of y [4, 5]
}
void draw(){
int r = (width/120); // radius
/* Variables of obstacles /
float obs_ox=-200; // obstacle x-position
float obs_oy=-200; // obstacle y-position
background(0); // cover previous balls
/ movement of x and y about balls */
xpos = xpos + xspeed;
if (xpos+r > width || xpos-r < 0) {
xspeed *= -1;
if(xpos <= r && (ypos <= 200) ){ // left top
obs_ox= 500;
obs_oy= 0;
// fill(255);
// rect(obs_ox,obs_oy,200,200);
}
else if(xpos <= r && (ypos > 200) ){ // left bottom
obs_ox = 500;
obs_oy= 300;
// fill(255);
// rect(obs_ox,obs_oy,200,200);
}
else if(xpos+r > width && (ypos <= 200) ){ // right top
obs_ox = 0;
obs_oy = 0;
// fill(255);
// rect(obs_ox,obs_oy,200,200);
}
else if(xpos+r > width && (ypos > 200) ){ // right bottom
obs_ox = 0;
obs_oy = 300;
//fill(255);
// rect(obs_ox,obs_oy,200,200);
}
obstacledraw=true;
}
ypos = ypos + yspeed;
if (ypos+r > height || ypos-r < 0) {
yspeed = -1;
}
/ obstacles */
/* When the ball hits the wall /
/ if(xpos <= r && (ypos < (height/2)) ){ // hit the left top
obs_ox= random(400,500);
obs_oy=0;
fill(255);
rect(obs_ox,obs_oy,200,100);
}
else if(xpos <= r && (ypos > (height/2)) ){ // hit the left bottom
obs_ox=random(400,500);
obs_oy=300;
fill(255);
rect(obs_ox,obs_oy,200,100);
}
else if(xpos+r >= width && (ypos < (height/2)) ){ // hit the right top
obs_ox=0;
obs_oy=0;
fill(255);
rect(obs_ox,obs_oy,200,100);
}
else if(xpos+r >= width && (ypos > (height/2)) ) { // hit the right bottom
obs_ox=0;
obs_oy=random(250,300);
fill(255);
rect(obs_ox,obs_oy,100,200);
}*/
//fill(255);
//rect(obs_ox,obs_oy,200,100);
if(obstacledraw == true){
fill(255);
rect(obs_ox,obs_oy,200,200);
if(xpos-r < obs_ox || xpos+r > obs_ox){
obstacledraw = false;
}
}
// draw the balls
if(stop == false){ // change the color, if the mouse is pressed randomly.
fill(random(255),random(255),random(255));
}
circle(xpos,ypos,2*r);
}
void keyPressed(){
final int k = keyCode; // k means keyCode to use s or S in if statements
if(k == ‘s’ || k == ‘S’)
if(looping) noLoop(); // stop if looping in the draw
else loop(); // start
}
void mousePressed(){
stop = false; // control the color, if mouse is pressed, color gradually be changed
}
void mouseReleased(){
stop = true; // if mouse is released, changing color will be stop.
}