java - Problems with Brick Collision Detection in BreakOut game -
i've done first breakout game while completing assignment stanford university cs online course java.
however noticed during play testing when ball hits brick while travelling diagonally ends hitting several bricks in row in unnatural way.
i'm guessing have improve bit collision detection code , tried several things no avail.
i'm using acm library program. imaginary rectangle surrounds ball , use 4 corners of rectangle detect collision.
during game, since added several addons (items drop , give bonus) there lot of velocity changes in game - vx variable change lot.
i think related problem, because noticed it's when ball travels @ more speed more of several bricks destroyed in row problem.
i´ll add relevant code here. can check out of code here: https://gist.github.com/frodosda/5604272
// defines initial direction of ball - inside "moveball" method vx = rgen.nextdouble(1.0, 3.0); // random horizontal direction if (rgen.nextboolean(0.5)) vx = -vx; vy = 2.0; // vertical direction /** checks if ball collided or not object */ private void verificarcolisaobola () { gobject objcolisao = getobjcolisao(); // object wich ball colided if (objcolisao == raquete) { // if colidded paddle vy = -vy; // change vertical tranjectory of ball // prevents ball "glued" paddle bola.setlocation(bola.getx(),bola.gety() - paddle_height / 2); // changes direction of ball when hits borders of paddle - provides player more control if ((bola.getx() < raquete.getx() + 0.20 * raquete.getwidth() && vx > 0) || (bola.getx() > raquete.getx() + 0.80 * raquete.getwidth() && vx < 0)) { vx = -vx; } } else if (objcolisao != null) { // colision brick remove (objcolisao); // remove brick ntijolos--; // counts 1 less brick vy = -vy; // changes vertical direction } /** finds if limits of ball - 4 corners - hits object * @return object collided ball - or null */ private gobject getobjcolisao () { if (getelementat (bola.getx(), bola.gety()) != null) { // top left corner return getelementat (bola.getx(), bola.gety()); } else if (getelementat (bola.getx() + bola.getwidth(), bola.gety()) != null) { // top right corner return getelementat (bola.getx() + bola.getwidth(), bola.gety()); } else if (getelementat (bola.getx(), bola.gety() + bola.getwidth()) != null) { // bottom left corner return getelementat (bola.getx(), bola.gety() + bola.getwidth()); } else if (getelementat (bola.getx() + bola.getwidth(), bola.gety() + bola.getwidth()) != null) { // bottom right corner return getelementat (bola.getx() + bola.getwidth(), bola.gety() + bola.getwidth()); } else { return null; } }
thanks in advance help!
you need check time step. velocity large time steps cause objects hit multiple items in next time step. make time step smaller make collision detection occur more frequently.
you should smarter analysis , check first brick have hit, ( 1 max/min x or y value), stop there. need calculate final location of ball after collision large time step ball acts should.
Comments
Post a Comment