T.T hit detect and laser beams. Verge of insanity.
in
Programming Questions
•
3 years ago
I have been trying for two days to figure out what's going on in my code and am really really stuck! I was wondering if you nice folks can help me sort out this mess...
The goal: Have a laser beam shoot upward from my green cursor. If it hits a quadrant, that quadrant turns black and begins fading away. In essence, I want it to look like I am blowing holes in a picture, but the picture slowly regenerates.
The current reality: The laser beam is no longer shooting from the green cursor (it's just starting at the top of the screen - is my translate broken??-) and the squares seem to be drawing themselves willy nilly...
Please help! High fives and cheers in advance!!!
The goal: Have a laser beam shoot upward from my green cursor. If it hits a quadrant, that quadrant turns black and begins fading away. In essence, I want it to look like I am blowing holes in a picture, but the picture slowly regenerates.
The current reality: The laser beam is no longer shooting from the green cursor (it's just starting at the top of the screen - is my translate broken??-) and the squares seem to be drawing themselves willy nilly...
Please help! High fives and cheers in advance!!!
- /*
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 = 8;
int cols = 8;
//grid sectors
float sqWidth;
float sqHeight;
//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 ();
sqWidth = width/cols;
sqHeight = height/rows;
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 = 10;
zapY = 10;
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();
translate(zapX, zapY);
ellipse(0,0,10,50);
popMatrix(); //end matrix
} // endzdrawLaser
void updateZap() {
zapX=laserX;
zapY-=10;
if (zapY < -(height-200)) {
zapVis = false;
}
}// end updatezap
////////////////////OBJECT BEHAVIORS
void hitDetect () {
println("zap X: " + zapX + " " + "zapY: " + zapY);
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*sqWidth && zapX <= (i*sqWidth+sqWidth) && zapY >=g*sqHeight && zapY <= (g*sqHeight+sqHeight)) {
sqVis[i][g] = true;
println("true "+i+" "+g);
sqAlpha[i][g] = 255;
println(sqAlpha[i][g]);
}
//maybe rewrite sqVis to include separate functions to control alpha
if (!sqVis[i][g]) {
//println("FALSE");
sqAlpha[i][g] = 255;
}
else {
//println("tru");
sqAlpha[i][g]--;
fill(0,0,0,sqAlpha[i][g]);
//pushMatrix();
//translate(zapX,i);
rect(g*sqWidth,i*sqHeight,sqWidth,sqHeight);
//popMatrix();
}
//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) {
zapX = laserX;
zapY = laserY;
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