bouncing squares, embarassing question
Hello,
I'm a newbie programmer in Java, and in had a quite silly problem when making a game, maybe you gentleman can type one word or two, just to point me the right direction, please.
The game consists in a series of squares with letters inside, which are suposed to bounce when touching each other, or at the walls, or at a paddle. Yet, the player controls this paddle, that moves in all directions, trying to conduct the bouncing square/letters to some boxes, to form words (yes, it's a child game, mostly to brazillian kids learn some simple english words with some fun)
I've made quite a good job until now, but sometimes the squares overlap each other, instead of bouncing. They keep running together in a nervous fashion. The same with the paddle(which is a horizontal bar). I just can't stop the squares to overlap the paddle, changing direction rapidly, but without bounce.
I'm using the traditional motion vectors swap, to bounce the squares; testing limts of walls, to bounce at walls, and testing rectangles intersects, for collision detection. I'm pretty sure the problem is with the vectors, since they increments the positions in leaps (3 to 8 pixels). I think it "jumps" to the center of the other square or paddle, and when changing direction, can't scape from the rectangle boundaries, and changes direction again in the next intersection test, in a loop.
Maybe somebody had this kind of problem, or have a more experienced view and kindly throws some light on me. please.
Thanks in advance, forgive my spell erros.
Mello
[1597 byte] By [
RochaMeloa] at [2007-10-2 12:03:13]

Ok, thanks for reading this.
My bouncing boxes move in the following way. Their positions are represented by a Point. Their speed and direction are
represented by a motion vector, represented by another Point. So, they move by means of adding the values x and y of
the vector Point with the values of x and y of the position Point.
// pos stands for position's point
// motionVector Points have a range of 1 to 8, a typical example is
// (2, 6), which means that the box will go down 2 pixels, and right 6 pixels.
public void move() {
pos.translate(motionVector.x, motionVector.y);
}
This works well, and when wall collisions are detected, I just reverse the signal of motion vector's x (bouncing in the right or left wall) or y (up and down walls)
Also, I have this code for checking collisions:
public void chkCollision(){
// generating rectangles for collision test
// bB and bB2 stands for bouncingBox and bouncingBox2
Rectangle r1 = new Rectangle(bB.getPos().x, bB.getPos().y, 60, 60);
Rectangle r2 = new Rectangle(bB2.getPos().x, bB2.getPos().y, 60, 60);
Rectangle playerRect = new Rectangle(player.getPos().x, player.getPos().y,
player.getWidth(), player.getHeight());
// checking for collisions between the two boxes, and swaping motionVectors
// if they intersects, (poorly but good enough to the game) simulating pool or billiard balls.
if (r1.intersects(r2)) {
Point tmpMotionVector = bB.getMotionVector();
bB.setMotionVector(bB2.getMotionVector());
bB2.setMotionVector(tmpMotionVector);
}
// checking for collisions between the Boxes and player pad.
// the boxes should bounce, reversing their directions in the Y-axis
if (playerRect.intersects(r1)) {
bB.getMotionVector().y *= -1;
}
if (playerRectCimaBaixo.intersects(r2)) {
bB2.getMotionVector().y *= -1;
}
}
It works most of time, but when the boxes aproach in a sharp angle, or when the player is moving the pad in the moment of collision, they overlap each other, moving together one upon the other, like trying to escape. I just thinked it's a common problem when dealing with bouncing balls that are suposed to bounce on each other, so I asked you the help. I'd like the boxes could never go upon the other, and the pad and ball could not even move if it's pressing a ball against the wall, for example. I've read a lot of examples and tutorials abou bouncing balls, but none of them has balls colliding each other, just balls passing through the others or balls moving in one-pixel increments, which reduces this problem a little.
Thanks again.
Melo
Well, after viewing your code, I believe I may have found a problem in this section in you code:
if (r1.intersects(r2)) {
Point tmpMotionVector = bB.getMotionVector();
bB.setMotionVector(bB2.getMotionVector());
bB2.setMotionVector(tmpMotionVector);
}
When bB and bB2 overlap each other, they switch directions. If their speed in the y direction is less than how far they overlap in the y direction, or if their speed in the x direction is less than how far they overlap in the x direction, they will still overlap each other the next loop iteration, so they will swap vectors again. They will continue to switch directions forever, making them appear as though they're trying to escape one another (which they are). In this way, it is impossible for them to escape each other. One way to solve this problem would be to set them apart before you swap vectors.