• Create Account

# Collision Problem in BrickBreaker (Java/Slick2D)

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

1 reply to this topic

### #1JohnnyCrazy  Members   -  Reputation: 116

Like
0Likes
Like

Posted 31 August 2013 - 03:34 AM

Hello,

First something about my Project:

For a important school-work, we need to develop a game in Java. Since we are allowed to use Librarys, i decided to use SLICK2D/LWJGL to create my game (If you know a better one, say it ). My game is called BrickBreaker, a classic game in which you have to destroy Bricks with a paddle and a ball.

I solved nearly all my issues alone, but for this one, i can't find any answer:

To get the collision detection working, when the ball hits a Brick, i have to check which side was hit by the Ball. I achieved this with creating a Line on each side, and then i check if the line collides with the Ball and return the code for that direction (e.g "south","west").

This works at the most collisions, BUT:

Since i hava a Circle Object as ball, i also can happen, that the Ball collides with 2 lines of a Brick, e.g if he hits a corner of a Brick. Then, the ball goes in that direction, which was checked first, and often thats unrealistic and looks ugly.

Steps which i already tried:

Storing all collisions with the lines in an ArrayList, and then checking if the ArrayList contains 2 Directions. If so, turn the Ball 180°.

Works at all, but there are moments where it totaly looks crappy:

The ball would turn around 180°, but normally he should just go down. ( it collides with north and east)

I hope you can understand my Problem, if not, i will post more Infos.

Thanks,

Johnny

### #2Horscht  Members   -  Reputation: 209

Like
0Likes
Like

Posted 01 September 2013 - 06:23 AM

I had a similiar problem with my platformer game and the way I solved it was by calculating how far the two object overlap and then divide that by the velocity, to determine which side was hit first and then push the player out on that side.

void ResolveCollisions()
{
Vector2D penetration = GetPenetrationAmount(playerBoundingBox, tileBoundingBox);
float tx = 100000.0f; // Time of impact, 0 = now, really big number = never
float ty = 100000.0f;

// Calculate time of impact, the smaller happened first

if(velocity.x != 0) tx = abs(penetration.x / velocity.x); // Don't divide by zero or everything explodes :)
if(velocity.y != 0) ty = abs(penetration.y / velocity.y);

// Check in which direction we collide first and resolve collision on that axis

if(tx < ty)
{
PushOutOfCollisionX(penetration.x);
}
else
{
PushOutOfCollisionY(penetration.y);
}
}

Vector2D GetPenetrationAmount(RECT r1, RECT r2)
{
Vector2D p;

int l2r = r1.right - r2.left;
int r2l = r1.left - r2.right;
int t2b = r1.bottom - r2.top;
int b2t = r1.top - r2.bottom;

if(abs(l2r) <= abs(r2l))
{
p.x = l2r + 1;
}
else
{
p.x = r2l - 1;
}

if(abs(t2b) <= abs(b2t))
{
p.y = t2b + 1;
}
else
{
p.y = b2t - 1;
}

return p;
}

Something like that. I hope this helps you somehow, good luck

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS