Efficient way to check collision on a list of objects for Java/Android -
Efficient way to check collision on a list of objects for Java/Android -
so, i'm developing game android in mobile development class , want know if there more efficient way check collision have. right have 3 lists contain row of bricks each (total of 3 rows, equal in length). calling checkcollision() each brick in each list (depending on few conditions [see code]) every time ondraw() method called. works fine, sense if isn't efficient way of doing , don't want utilize rects. can offer suggestion? here code:
this called in ondraw(): public void checkbrickcollision() { // row 1 // checks collision if first 2 rows missing // elements (meaning player has nail elements) // , if list not empty if (bricklistthree.size() < 6 && bricklisttwo.size() < 6) { if (!bricklistone.isempty()) { (brick b1 : bricklistone) { if (b1.checkycollision(pongx, pongy, pong, pongup, pongdown)) { bricklistone.remove(b1); yspeed *= -1; break; } if (b1.checkxcollision(pongx, pongy, pong, pongright, pongleft)) { bricklistone.remove(b1); xspeed *= -1; break; } } } } // row 2 // checks collision if elements have been // nail in row 3 , list not empty if (bricklistthree.size() < 6) { if (!bricklisttwo.isempty()) { (brick b2 : bricklisttwo) { if (b2.checkycollision(pongx, pongy, pong, pongup, pongdown)) { bricklisttwo.remove(b2); yspeed *= -1; break; } if (b2.checkxcollision(pongx, pongy, pong, pongright, pongleft)) { bricklisttwo.remove(b2); xspeed *= -1; break; } } } } // row 3 // collision checked // long list not empty (brick b3 : bricklistthree) { if (!bricklistthree.isempty()) { if (b3.checkycollision(pongx, pongy, pong, pongup, pongdown)) { bricklistthree.remove(b3); yspeed *= -1; break; } if (b3.checkxcollision(pongx, pongy, pong, pongright, pongleft)) { bricklistthree.remove(b3); xspeed *= -1; break; } } } }
brick class functions public boolean checkxcollision(int ballx, int bally, bitmap ballimg, boolean ballright, boolean ballleft) { // if ball hits left side of brick if(bally + ballimg.getheight() >= bricky && bally <= bricky + brickh && ballx + ballimg.getwidth() >= brickx - 2 && ballx + ballimg.getwidth() <= brickx + 2 && ballright) { this.isvisible = false; homecoming true; } // if ball hits right side of brick else if(bally + ballimg.getheight() >= bricky && bally <= bricky + brickh && ballx <= (brickx + brickw) + 2 && ballx >= brickx + brickw - 2 && ballleft) { this.isvisible = false; homecoming true; } homecoming false; } public boolean checkycollision(int ballx, int bally, bitmap ballimg, boolean ballup, boolean balldown) { // if ball hits top of brick if (ballx + ballimg.getwidth() >= brickx && ballx <= brickx + brickw && bally + ballimg.getheight() >= bricky - 2 && bally + ballimg.getheight() <= bricky + 2 && balldown) { this.isvisible = false; homecoming true; } // if ball hits bottom of brick else if (ballx + ballimg.getwidth() >= brickx && ballx <= brickx + brickw && bally <= (bricky + brickh) + 2 && bally >= bricky + brickh - 2 && ballup) { this.isvisible = false; homecoming true; } homecoming false; }
java android performance collision
Comments
Post a Comment