hit detection, 2d arrays, not working :(
              in 
             Programming Questions 
              •  
              3 years ago    
            
 
           
             Hi again,
            
            
I got the thing KIND OF working, but something is wrong with my hit detection now T-T it seems my sector height and width are always zero.... sqVis is never true...
            
Any ideas? Thanks ahead of time
            
 
            
           I got the thing KIND OF working, but something is wrong with my hit detection now T-T it seems my sector height and width are always zero.... sqVis is never true...
Any ideas? Thanks ahead of time
- /*
 Bree Rubin's quarter of the destruction project.
 A lot of this code was helped by attending the workshop
 wit Ramsey and Eric on making Asteroids.
 
 --- got more help later *sigh*
 */
 //laser gun == RADICAL!!!
 float laserX, laserY, laserSpeed, startPoint;
 //shooter
 float zapX, zapY, zapSpeed;
 boolean zapVis; //whether or not the zap exists
 //Key input
 int keyDownArraySize = 127;
 boolean[] keyDown = new boolean [keyDownArraySize]; //not 100% on this one
 //bgimg
 PImage zeImg;
 //rows cols for grid
 int rows = 12;
 int cols = 12;
 //grid sectors
 int sqWidth = width/cols;
 int sqHeight = height/rows;
 
 //array visibility of blackbox
 boolean[][] sqVis = new boolean[rows][cols];
 int[][] sqAlpha = new int[rows][cols];
 ////////////////////////BEGIN SETUP --------------
 void setup(){
 // frameRate(20);
 size(1024 , 768);
 zeImg = loadImage("logo_bottomleft.jpg");
 smooth();
 
 initLaser();
 initZap ();
 
 for (int i = 0; i<rows; i++){
 for (int g = 0; g<cols; g++){
 sqVis[i][g]=false;
 }
 }
 
 }//end setup
 ////////////////////////BEGIN INITIALIZING STUFF-------
 void initLaser (){
 laserX = 0;
 laserY = 0;
 laserSpeed = 5;
 startPoint = height-100;
 }
 void initZap (){
 zapX = 0;
 zapY = 0;
 zapSpeed = 1;
 zapVis = false;
 }
 /////////////////////BEGIN DRAW LOOP ------
 void draw() {
 //background(0);
 
 image(zeImg, 0, 0);
 drawLaser();
 updateLaser();
 if (zapVis) {
 drawZap();
 updateZap();
 }
 inputHandler();
 hitDetect();
 
 }//end draw loop
 ////////////////////OBJECTS
 void drawLaser() {
 stroke(255);
 fill(0,255,0);
 pushMatrix();
 //christmas
 //height-200 should also be a variable like... bottomStart or something
 translate(laserX, startPoint);
 rect(0,0,60,10);
 popMatrix(); //end matrix
 } // end drawLaser
 void updateLaser() {
 if ( laserX+50 > width) {
 laserX = width-50;
 }
 if ( laserX < 0 ) {
 laserX = 0;
 }
 }// end updateLaser
 void drawZap() {
 noStroke();
 fill(random(0,255),random(0,255),random(0,255));
 pushMatrix();
 //christmas
 //make the 40 a variable, laserWidth, 30 is half what the width is right now
 translate(laserX, height-200);
 ellipse(30,zapY,10,50);
 popMatrix(); //end matrix
 } // end drawLaser
 void updateZap() {
 zapX=laserX;
 zapY-=10;
 
 //christmas
 //it's doing something strange from the translation, i'm not sure why but it works
 //you will probably want to dbl check what it is later... zapY is 0 when it is fired
 //and goes negative, that's why it's -(height-200)
 if (zapY < -(height-200)) {
 zapVis = false;
 }
 }// end updatezap
 ////////////////////OBJECT BEHAVIORS
 void hitDetect (){
 
 for (int i=0; i<rows; i++){
 for (int g=0; g<cols; g++){
 println("g "+g+" "+"i "+i);
 println("sector width and height "+sqWidth +sqHeight);
 
 if (zapX >= i && zapX <= (i+sqWidth) || zapY >=g && zapY <= (g+sqHeight)){
 sqVis[i][g] = true;
 println("true "+i+" "+g);
 }
 if (!sqVis[i][g]){
 println("FALSE");
 }
 
 else if (sqVis[i][g]){
 println("tru");
 fill(0);
 rect(i,g,(i+sqWidth), (g-sqHeight));
 sqAlpha[i][g]=255; //decreases alpha
 }
 println("bullet location "+zapX+" "+zapY);
 println("gun location "+laserX+" "+laserY);
 
 }//end g for
 }//end i for
 
 }
 /////////////////////INPUT HANDLERS ETC
 void inputHandler() {
 // begin controlling my laserz
 //christmas
 //fixed your left right stuff
 if (keyDown[LEFT] || keyDown[37]) {
 laserX-=5;
 }
 if (keyDown[RIGHT] || keyDown[39]) {
 laserX+=5;
 }
 //space is pressed to fire ze laserz!!!
 if (keyDown[32]) {
 if(!zapVis) {
 //christmas
 //it's not resetting the values of the bullet so i call initZap() here to clear
 //the previous bullet's coords
 //deleted the extraneous code
 initZap();
 zapVis = true;
 }
 }
 } //end inputHandler
 void keyPressed() {
 if(keyCode < keyDownArraySize) {
 keyDown[keyCode] = true;
 } //end if
 } //this function also allows for pressing TWO+ keys at once
 //so you can move and rotate at the same time!
 void keyReleased() {
 if(keyCode < keyDownArraySize) {
 keyDown[keyCode] = false;
 } //end if
 }
 
              
              1  
            
 
            
 
 
          