[java] Collision detection between objects in an array

Started by
3 comments, last by n0rmanfreak 13 years ago
I'm doing a simple breakout clone and have completed it. I now want to add an extra element to it. I want the bricks to move from one side of the screen to the other. For example every brick on every row starts off moving right. When the last brick on the row (the first brick to hit the right border of the JFrame) hits the border it should bounce off it and start moving left. This should cause it to collide with the brick to the left of it causing that brick to bounce off it and move left hitting the next brick and so on. Basically the bricks switch direction when they hit either each other or the borders.

so far I can have an array of two bricks move from left to right bouncing off the sides but not each other. I've tried using intersect() but this isn't really working.

I can't think of a nice way of checking if element i in the array intersects with element [i - 1] or element [i + 1] as this causes Array out of bounds exceptions with the first and last element.

Wondering if anyone has any thoughts?

Many Thanks,
Advertisement
Theoretically speaking (as im not a java programmer) i think you would make a loop that goes through each element in your first array and get a usable object out of it. Then inside that loop go through each of the elements inside the second array and check the intersection between them.

Heres some pseudo if its understandable:


For each element in array1;
Extract element i from the array;
For each element in array2;
Extract element j from the array;
Do intersection checks against the two objects;
End Loop
End Loop


In C++ it would look something like this (You may be able to convert)

for(int i = 0; i < NumberOfElementsInArray1; i++)
{
ElementType Object1 = Array1;
for(int j = 0; j < NumberOfElementsInArray2;j++)
{
ElementType Object2 = Array2[j];
if(Object1 intersects Object2)
{
React_to_collision();
}
}
}

Thanks for replying, the objects are all in one array though. I've had a change of mind and I'm now trying to have the bricks moving in a space invaders type pattern but not moving down one each time. When the first brick to hit the right border reverses its movement I want all bricks on that row to do the same. Then when the first brick to hit the left border reverses its movement I want all bricks on that row to do the same. So they never collide and just keep moving right to left.

This is how I init the array.

public void initBrickArray()
{
int k = 0;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
myBrick[k] = new Brick(j * 80 + 14, i * 40 + 40);
k++;
}
}
}


This is how I test to see if any brick hits the borders and make them reverse direction.

public void brickTest(int width, int height)
{
if(x > width - brickWidth - OFFSET)
{
direction = -2;
}

if(x <= 0)
{
direction = 2;
}

x = x + direction;
}


This is how I check to see if the first brick hits the border (not working)!

public void brickCollisionTest(int width, int height)
{
for(int i = 0; i < 30; i++)
{
myBrick.brickTest(width, height);

if(i < 29)
{
// if(myBrick.area().intersects(myBrick[i + 1].area()))
if(myBrick.getX() == myBrick[i + 1].getX())
{
myBrick.setDirection(-myBrick.getDirection());
}
}
}


Anyone help?
You should store and process bricks row by row. This way, you could keep rows sorted by position and check only the rightmost brick for reaching the right edge and the leftmost brick for reaching the left edge.
I'd keep actual movement velocities per-brick, to allow every brick to bounce, accelerate etc. independently.

Testing intersections as if(myBrick.getX() == myBrick[i + 1].getX()){
//bounce
}


is wrong: if the witth of a brick is W units the test (assuming brick i is left of brick i+1) should be if(myBrick.getX() + W >= myBrick[i + 1].getX()){
//bounce
}


Of course you also have to collide bricks only with bricks in the same row, not with those of other rows.

Omae Wa Mou Shindeiru

just a couple of comments:

* if you don't want/need collision checks between the bricks you should maybe use a bounding box for the bricks or maybe just a row
* you should rename your "direction" to "velocity" because it's not just a direction

This topic is closed to new replies.

Advertisement