Need orientation

Hi,

I’m a Java Programmer for about 4 years (3 profissionaly) now but only recently began with games and Java2D as an hobby. I’m trying to recreate a version of Arkanoid. I’m having some dificulties with the object collision algorithm, more exactly the collision between the ball and the bricks or the stick. I know that I’ll eventually overcome this problem with more or less coding but I wanted to do it right and avoid creating bad habits. I was wondering if anyone could advise me some good reading to learn about this ordinary algorithm problems in 2D coding?

I’m currently reading Killer Game Programming In Java which is not very advanced in this matter. (atleast not where i am currently reading)

Thanks

Hi,

The easiest way to start with collision detection would be to use the java2d classes (Rectangle2D / Ellipse2D), and their intersect() method.

Lilian :slight_smile:

Hi Lilian,

My problem is much more complex than that, knowing if the ball hitted the brick is not enough, i need to know how did it hit so I can calculate in which direction it will go and the angle it will take for example. I’m sure that this kind of problems have been heavly thought by many people and there has to be a lot of reading about this somewhere, i just need to know where ^^

I’m not looking to a solution for my problem exactly, i just want to read about collision algorithms in 2D and take my conclusions on the better way to solve my problems.

EDIT: You can check a VERY EARLY version here. As you can see the collision tests are still very bad ^^ (requires java1.5)

Assuming that you know:

  • The coordinates of the center of the ball
  • The radius of the ball
  • The vertices of the bricks

and also that the bottom of your screen has y-coordinate zero, and that bricks are rectangular and axis-aligned

For collision with bricks (assumed to be at the top of the screen)
First stage : Ultra quick rejection test
When you start your level, and every time a brick is destroyed, find the lowest vertex of any brick and store it. then every frame if

ball.y + ball.radius <= minBrickVertexY

You don’t have to do any more collision tests

Second stage : Per brick quick rejection
For each brick:


for( Brick brick : bricks )
{
   if( brick.bottomLeft.x - ball.radius < ball.x && brick.topRight.x + ball.radius > ball.x && brick.bottomLeft.y - ball.radius < ball.y && brick.topRight.y + ball.radius > ball.y )
   {
      // go to third stage
   }
   else
   {
      // no collision with this brick
   }
}

Third stage : Find which edge was hit
For each of the edges of the brick, use the java.awt.geom.Line2D.ptSegDist() methods to find the distance between the edge and the ball’s center. If the distance is less than the radius, you have found a collision.

Things to think about:
Finding collisions with more than one edge -> must decide which edge to calculate the reflection with.
Fast-moving balls & thin bricks -> Ball may pass straight through the brick

Well, you need vector voodoo. And the bounce off thing. In classic breakout games its basically like… there is a point below the centre of the paddle… and you “draw” a line from this point to the ball’s center. Thats the direction the ball will move.

In “modern” breakouts with a curved paddle you simply get the collision normal and handle it in the normal vector fashion.

A brief intro to vector stuff:
http://www.harveycartel.org/metanet/tutorials/tutorialA.html