Hi there , I have been working (playworking), on this this P3D program and am confused as to why the collision check loop does not detect any of the posts other than the last in the array (which I have coloured red.) !
Could anyone please explain to me why this is so ?
//***BALL MAZE********NIGEL WOOLLARD
// *** KEY CONTROLS ***
//
// ‘,’ = LEFT
// ‘/’ = RIGHT
// ’ .’= FORWARD
// ‘x’ = BACK
float x=683;
float y=636; //sets initial ball position
float z=-282;
float s=3;
float altitude=1;
float py=600;
float d; // Ball to Post distance
int[]postx = new int[50];
int[]posty = new int[50]; // declare post coordinates
int[]postz = new int[50];
int bc=255; //background colour var
void setup(){
size(1200, 700, P3D);
//directionalLight(255,255,255, 0, 0,0);
for (int p=0;p<50;p++){
postx[p]= int (random(200,1160));
posty[p]= int (random(100,1100)); // Fill arrays with random post coordinates
postz[p]= int (random(-900,80));
}
}
void draw(){
shapeMode(CENTER);
background(bc);
//lights
directionalLight(251, 202, 226, -1, 0, 0);
spotLight (255,255,255,300,100,100,width/2,height/2,-100,PI/2,2);
//stage
pushMatrix();
translate(width/2,height-100,-400);
fill(#257C13);
box(1000,5,1000);
popMatrix();
//posts
for (int p=0;p<50;p++){
pushMatrix();
translate (postx[p],py,postz[p]);
fill(#BC9B20);
box( 20,100,20);
popMatrix();
}
pushMatrix();
translate (postx[49],py,postz[49]); // color last post in array red .(done this just to make sure it was the last one.)
fill(#D62B18);
box(20,100,20);
popMatrix();
// pointer sphere
pushMatrix();
translate(x,y,z);
fill(#FF0313);
noStroke();
sphere(10);
popMatrix();
// create backward forward movement
if (keyPressed){
if (key == ‘/’){x=x+s;} //RIGHT
if (key == ‘,’){x=x-s;} //LEFT
//if (key == ‘c’){y=y+altitude;} //ALTITUDE UP
//if (key == ‘z’){y=y-altitude;} //ALTITUDE DOWN
if (key == ‘x’){z=z+s;}
if (key == ‘.’){z=z-s;}
}
// position coordinates
fill(255);
textSize(32);
text (“x=”,25,40);
text ((x)-width/2,70,40);
text (“y=”,25,75);
text (-(y)+636,70,75);
text (“z=”,25,110);
text ((z)+282,70,110);
// text (“postx”,25,150);
// text ((postx[0]),120,150);
for (int p=0;p<50;p++){
d= dist(x,y,z,postx[p],y,postz[p]);
if (d<=20) {bc=0;}//else{bc=255;} // collision check
if (d>=20) {bc=255;}//else {bc=0;}
// text (d,100,200);
}
// (x>postx[0])-10)
}