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.

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.

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:

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.

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(x*x+y*y+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)

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.

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)

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!

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org