actionscript 3 - Breakout with Flash: I need help to improve my Brick n Ball collision -
i've been stuck on problem long time now, i've searched around alot , tried stuff, nothing works. explanations hard me understand im pretty new programming overall , got alot learn.
i have 2 problems
1: ball wont collide bricks when speed fast.
2: ball capable of hitting 2 bricks. both problems related fact 60 fps isnt enough type of collision detection work properly.
i need explain in simple way possible need make collision detection prevent happen.
here's current collision code:
private function checkcollision(): void { grdx = math.floor((ball.x) / 28); grdy = math.floor((ball.y) / 14); ngrdx = math.floor((ball.x + dx) / 28); ngrdy = math.floor((ball.y + dy) / 14); var flipx: boolean = false; var flipy: boolean = false; if ((grdy <= level.length - 1) && (ngrdy <= level.length - 1) && (grdy >= 0 && ngrdy >= 0)) { if (testblock(grdx, ngrdy)) { flipy = true; paddleflag = 1; } if (testblock(ngrdx, grdy)) { flipx = true; paddleflag = 1; } if (testblock(ngrdx, ngrdy)) { flipx = true; flipy = true; paddleflag = 1; } dx *= flipx ? -1 : 1; dy *= flipy ? -1 : 1; } } private function testblock(xpos: int, ypos: int): boolean { if (level[ypos][xpos] > 0 && level[ypos][xpos] != 13) { trace("hit on x,y"); level[ypos][xpos] = 0; breakblock("block_" + ypos + "_" + xpos); trace("block: " + totalbreaks + " / " + totalblocks); return true; } return false; } private function breakblock(blockname: string): void { if (this.getchildbyname(blockname)) { this.removechild(this.getchildbyname(blockname)); totalbreaks++; } }
thank , sorry bad english, not motherlanguage.
one solution move ball in smaller iterations, multiple times in given frame.
for example, , giving solution assuming moving ball based on time elapsed last frame.
suppose 30 milliseconds have elapsed since last frame update. in case update movement/collision twice in frame using 15 millisecond time elapsed.
the higher resolution of collision want, more iterations do.
here's example :
// class declarations var lastframe:number; var iterationsperframe:int; function startgame():void { // lets specify 3 updates per frame iterationsperframe = 3; // save initial time lastframe = gettimer(); // create listener addeventlistener(event.enter_frame, update); } function update(e:event):void { var currentframe:number = gettimer(); var deltatime:number = (currentframe - lastframe)/1000; var iterationdelta:number = deltatime/iterationsperframe; (var index:int = 0;index < iterationsperframe;index++) { // i'm assuming dx,dy velocity of ball, in pixels per second ball.x += dx * iterationdelta; ball.y += dy * iterationdelta; // check collision } // set lastframe currentframe time, preparing next frame lastframe = currentframe; // after this, frame going render }