 # Another collision question

This is over my head, can someone explain it to me better? http://mathworld.wolfram.com/Reflection.html

I’m making a 2d breakout fighting game, so I need the ball to be able to reflect when it hits a wall, paddle, or enemy (or a enemy hits it).

all their formula’s are like: x_1^’-x_0=v-2(v·n^^)n^^.

And I can’t fallow that. (What does ’ mean or x_0? or ^^?)

A paddle war game should be considerably simple.

Currently how are you doing your collision? Rectangles or does the game support diagnal lines?

If no diagnal lines, then all you need to do is negate the direction of the ball on one of the axis, like if the game flow is up and down, then just negate on the y axis. And if it hits a side wall negate the x axis.

Also take into consideration either: the momentum of the paddle when the ball is hit, or the area on which the ball hit that paddle. This will make a sharper angle, depending on how you want the game to feel.

If you are using diagnal line collisions, then it will work a little differently.

Well, it’s going to be you vs a boss. Kinda like this: http://www.youtube.com/watch?v=jo5tW7fk1sc

There are no diagonal lines, but if the boss swats the ball I would need to know the angle he hit the ball at, and make it go flying in the reverse angle. That’s why I’m trying to make the collision detection so complicated.

that game is really cool. I thought you ment a paddle war, but this seems more like a pinball machine.

In that case you would need to make the ball entity store an angle (direction it is moving) and a momentum value to store its speed.

then apply a gravity value on each game cycle.

are you familar with line equations (eg. y = mx + c)?

No, no. Mine’s a paddle game.

Nope, don’t know what that formula means at all.

This is what I got for my collision right now, but the ball just stops when it hits a wall.

``````
for(int i = 0; i < wall.length; i++){
if(new Rectangle((int) position.x, (int) position.y, size, size).intersects(new Rectangle(wall[i].x, wall[i].y, wall[i].width, wall[i].height))){
Vector2f rvelo = velo.copy(); //get velo (direction)
rvelo.set(rvelo.x*-1, rvelo.y*-1); //reverse it for back tracking

Vector2f prevpos = position.copy();
System.out.println("Angle: " + Math.toDegrees(Math.atan2(-(position.x -prevpos.x), (position.y - prevpos.y))));

velo.x = (float) (Math.sin(Math.atan2(-(position.x -prevpos.x), (position.y - prevpos.y))) * 5);
velo.y = (float) (Math.cos(Math.atan2(-(position.x -prevpos.x), (position.y - prevpos.y))) * 5);

//position.x += (wall[i].x + wall[i].width) - position.x;
//velo.x = -velo.x;
accel = velo.scale(speed);

}
}

``````

The ^ symbol over the n in the variable names is a common symbol for a normalized vector. If the variable doesn’t have a ^, then in the equations in that link, it represents a point. When you subtract two points, you get a vector representing the difference. The ’ symbol is just part of the variable name, it usually symbolizes “new version”. So x1’ means the new x1, or in this case the reflected x1.

Another way to write the reflection equation (x1’ = -x1 + 2x0 + 2n[(x1-x0)•n]) in a more programmatic way is:

``````
Vector2f dir = ...
Vector2f n = dir.normalize();

Vector2f reflected = x0.scale(2).sub(x1).add(n.scale(2 * dot(n, x1.sub(x0)));

``````

In the above example, x0 and x1 are the points in the diagram from the link, but they’re stored as the same Vector2f type. Vector2f is a type I made up for this example, the methods used are scale(float), sub(Vector2f), add(Vector2f), normalize() and dot(Vector2f, Vector2f). These are all vector math operations that are pretty easy to implement. In this code, I’m assuming that a new Vector2f is created by each operation so that the proper order of operations is preserved.

HTH

Does dot product mean multiplying 2 vectors?
And what does normalize really mean?

dot product is :
dotProduct= x1x2+y1y2+z1*z2
or
dotProduct= |V1| * |V2| * cos(angleBetweenV1AndV2)

it is especially usefull to find angle between two vectors, or to make a projection of one vector onto another (changing frame of reference)

normalizing is an operation that make the final vector 1 unit lenght by dividing all its components by the initial vector length :
vectorLength=sqrt(xx+yy+z*z);
x/=vectorLength
y/=vectorLength
z/=vectorLength

.

I’m assuming this is actually 2D and not 3D. It’s easy to just remember that: “The angle of incidence equals the angle of reflection”. So the angle (with respect to the normal of the surface) that the ball strikes the surface is the same as the angle that it will move away from the surface, on the opposite side of the normal. In break-out style games, most of the surfaces will be aligned to an axis and the “equation” is simple: Striking the top/bottom = negate y (assuming y is up/down) and likewise striking the sides negates x.

Everything looks good so far, but I can’t figure out what points x0 and x1 are, and what they mean. I think x0 is 0,0 bu I have no idea what to put for x1. This is my code (Using slick2d’s Vector2f class)

``````Vector2f n = velo.normalise();
Vector2f x0 = new Vector2f(0,0);
Vector2f x1 = new Vector2f(0,0);

Vector2f reflected = x0.scale(2).sub(x1).add(n.scale(2 * n.dot(x1.sub(x0))));
velo = reflected.copy();
``````

x0 is the point where the collision occurs.

n is the line which the ball hit (i.e., one of the 4 edges of the rectangle you’re checking against). x0 is on that line.

Looking at your code, it doesn’t seem like you’re computing the actual collision point, just that a collision actually occurred. That’s not enough to compute reflection, though you could possibly hack around it.

… also, you’re setting both x0 and x1 to (0, 0) and then doing stuff with them like scaling.

In your case, if I understand correctly, all of the wall pieces are aligned with the x and y axis. If so, all you have to do is flip the x or y component of your velocity, depending on whether the wall piece you are checking against is to the left/right/above/below the ball. For example, if it’s above or below the ball, you’d flip the y component.

You might get into trouble if multiple collisions are detected - but that’s going to be tricky regardless of what you do.

I’d also suggest taking a step back to try and really understand the math. Vectors are very useful.

Anyone know why this collision for my enemy doesn’t work right? It seems when hit from the side it goes through him instead of bouncing off.

``````
if(new Rectangle((int) position.x, (int) position.y, size, size).intersects(
new Rectangle((int) enemy.x, (int) enemy.y, enemy.width, enemy.height))){
if(position.y + size >= enemy.y && position.y + size <= enemy.y + (enemy.height/6))
velo.y = -velo.y;

else if(position.y <= enemy.y + enemy.height && position.y >=
enemy.y + enemy.height - (enemy.height/6))
velo.y = -velo.y;

else
velo.x = -velo.x;

enemy.hp--;
}

``````

Sorry to be so intrusive, but I merged your topics.

That’s fine. But this last question wasn’t using reflection, or even trying to ^^;

I merged it because you’re littering JGO. You shouldn’t start a new thread for every tiny problem you run into.

Fair enough. +1 rep.

Whaattt? Y did u give him a rep?!? He has too many alreadddyy >:( ;D

This is why I prefer to have x,y of my entities as the center point, and not the top-left point.

If you have two rectangles intersecting, and you know the center point of both of them, and the width and height outward from the center point, you can simply check if x+width of A reaches beyond x-width of B to check if right side of A goes over the left side of B.

You do the same for all the sides: (size being the distance from centre to any side)

Check if x+size of A reaches beyond x-size of B to check if right side of A goes over the left side of B.
if Ax+size > Bx-size: Aright hit Bleft.

Check if x-size of A reaches beyond x+size of B to check if left side of A goes over the right side of B.
if Ax-size > Bx+size: Aleft hit Bright.

Check if y+size of A reaches beyond y-size of B to check if bottom side of A goes over the top side of B.
if Ay+size > By-size: Abottom hit Btop.

Check if y-size of A reaches beyond y+size of B to check if top side of A goes over the bottom side of B.
if Ay-size > By+size: Atop hit Bbottom.

You should try to visualize this using two cubes or rectangular shapes in the real world, e.g. with some cards. There are only four ways for the rectangles to collide, left to right, right to left, top to bottom, bottom to top. (that is if they don’t rotate)

btw. you need to ensure that your game uses a fixed step delta.

If the objects were to move using delta 3, collision is detected and direction is reversed.
Then the objects were updated to move using delta 1, then you would again have a collision and you would reverse direction again!

If the delta is always 1, or 3, then you would not encounter this.

But there are other ways to solve this as well.