The point is this: I am writing an arcanoid on a project at the institute. The task now is the organization of the ball collision algorithm with blocks. Those. need to determine when he collides with them to change the direction of the ball. What did I do? I tried to write a touchBounds method on the Block class (bricks on the screen), which determined which side of the block touched the ball and, depending on this, returns a whole from 0 to 3. It causes doubts about optimization. Since every 1000/60 milliseconds I need to perform this operation with all the available blocks. Since I am a layman in this matter, I ask for advice, can anyone know how these situations in programming are usually handled? Screen

Code:

public int touchBounds( int[][] a, int inc ) { int xC = ( a[0][0] + a[1][0] ) / 2; int yC = ( a[0][1] + a[2][1] ) / 2; if ( between( yC, offset[0][1] - ( inc + 1 ), offset[0][1] ) && between( xC, offset[0][0], offset[1][0] ) ) { return 0; } else if ( between( yC, offset[0][1], offset[2][1] ) && between( xC, offset[1][0], offset[1][0] + ( inc + 1 ) ) ) { return 1; } else if ( between( yC, offset[2][1], offset[2][1] + ( inc + 1 ) ) && between( xC, offset[0][0], offset[1][0] ) ) { return 2; } else if ( between( yC, offset[0][1], offset[2][1] ) && between( xC, offset[0][0] - ( inc + 1 ), offset[0][0] ) ) { return 3; } return -1; } private boolean between( int a, int b, int c ) { if ( a >= b && a <= c ) { return true; } return false; } 

where inc is the trim position of the ball, int [] [] a is the offset of the ball, xC is the center of the ball in X, xY is the center of the ball in Y

  • I wrote a long comment here that was in the restriction on symbols, then I decided to remove it and send it to Google to read about collision detection , because in your "algorithm" there is nothing about searching for intersections of two lines. The ball is not static and has speed as soon as the speed exceeds the ball radius - the method of searching for the intersection that you use will start to falter, so you need to look for the intersection of two lines .... By optimization - Crush the screen into pieces and check the intersections only with those blocks are in the same part with the ball, this is the first thing that should be optimized. - Vladimir Klykov

1 answer 1

Before the collision, the ball flies in a straight line (or along some other, usually predictable, trajectory). Therefore, at the beginning of the movement, it is possible to find once , with which brick it will collide first, at what time, and until that time, only to redraw the ball in new coordinates.

When a collision occurs, the configuration may change, and after a rebound, it is necessary to recalculate with which collision will be next.

I think that with the number of bricks real for an arkanoid (dozens), you should not use complex data structures that allow you to limit the checks - it will be enough just to search.

  • Plus to you from me, this is the second optimization, but (I don’t know the full TZ of the "game") it may not work if the "blocks" have their own movement, which will also take into account collisions between them ... and also the author considers the collision is not true initially = ) - Vladimir Klykov
  • @ Vladimir Klykov Yes, I was thinking about the movement of blocks, but the author did not mention anything about it. - MBo