|
Author |
Topic: Implementing Collision detection. (Read 805 times) |
|
jno
|
Implementing Collision detection.
« on: Sep 13th, 2004, 1:37pm » |
|
Hey there. I'm trying to implement collision detection in a little game of mine, but it won't work as it should. Someone who can give me some clues. (Code below). the whole code can be found @ http://jno.se/thegame/star_wars_2.pde http://jno.se/thegame/data //Global values for the meteors int meteor_width = 15; int meteor2_width = 18; int meteor3_width = 28; int meteor_height = 16; int meteor2_height = 18; int meteor3_height = 30; // speed int meteor_y = int(random(5,11)); int meteor2_y = int(random(4,7)); int meteor3_y = int(random(2,5)); //start int meteor_x = int(random(20,400)); int meteor2_x = int(random(20,400)); int meteor3_x = int(random(20,400)); //acc int acc, acc2, acc3; // Global variables for the ship int ship_width = 80; int ship_height = 80; int ship_dist_wall = 100; void meteor(){ acc = int(random(2,12)); acc2 = int(random(1,); acc3 = int(random(5,10)); if (meteor_y > 700){ meteor_y = int(random(5,11)); meteor_x = int(random(100,400)); meteor(); } if (meteor2_y > 700){ meteor2_y = int(random(4,7)); meteor2_x = int(random(100,400)); meteor(); } if (meteor3_y > 700){ meteor3_y = int(random(2,5)); meteor3_x = int(random(100,400)); meteor(); } meteor_y = meteor_y+acc; meteor2_y = meteor2_y+acc2; meteor3_y = meteor3_y+acc3; if (meteor_y < 0){ meteor_y = height; } if (meteor2_y < 0){ meteor2_y = height; } if (meteor3_y < 0){ meteor3_y = height; } if(meteor_y > 600) { if(meteor_x == mx){ println(meteor_y+" > "+600+" and "+meteor_x+" == "+mx); //run_thegame = false; } } println(meteor_x+" , "+mx); image(meteorImage1,meteor_x,meteor_y); //rect(start,speed,50,50); //image(meteorImage2,meteor2_x,meteor2_y); //rect(start2,speed2,50,50); //image(meteorImage3,meteor3_x,meteor3_y); //rect(start3,speed3,50,50); }
|
« Last Edit: Sep 13th, 2004, 4:27pm by jno » |
|
|
|
|
jno
|
Re: Implementing Collision detection.
« Reply #1 on: Sep 13th, 2004, 8:20pm » |
|
Solved it ------------------------------------- //Collision detection int deltaXSq = meteor_x - mx; int deltaXSq2 = meteor2_x - mx; int deltaXSq3 = meteor3_x - mx; deltaXSq *= deltaXSq; deltaXSq2 *= deltaXSq2; deltaXSq3 *= deltaXSq3; int deltaYSq = meteor_y - 680; int deltaYSq2 = meteor2_y - 680; int deltaYSq3 = meteor3_y - 680; deltaYSq *= deltaYSq; deltaYSq2 *= deltaYSq2; deltaYSq3 *= deltaYSq3; int sumRadiiSquared = meteor_width + ship_width; int sumRadiiSquared2 = meteor2_width + ship_width; int sumRadiiSquared3 = meteor3_width + ship_width; sumRadiiSquared *= sumRadiiSquared; sumRadiiSquared2 *= sumRadiiSquared2; sumRadiiSquared3 *= sumRadiiSquared3; if(deltaXSq + deltaYSq <= sumRadiiSquared){ run_thegame = false; } else if(deltaXSq2 + deltaYSq2 <= sumRadiiSquared2){ run_thegame = false; } else if(deltaXSq3 + deltaYSq3 <= sumRadiiSquared3){ run_thegame = false; } else{ image(meteorImage1,meteor_x,meteor_y); //rect(start,speed,50,50); image(meteorImage2,meteor2_x,meteor2_y); //rect(start2,speed2,50,50); image(meteorImage3,meteor3_x,meteor3_y); //rect(start3,speed3,50,50); }
|
|
|
|
|