New vector from known angle

Hi everyone, hoping I could get some Java specific help with my rusty maths knowledge.
I drew a little diagram to illustrate my problem:

So I have a ball moving along vector a, it hits the top bar and assuming I can calculate the angle of c, how would I calculate the new vector b for the ball to follow?

Thanks in advance for your help, I know this is is something really simple but I’ve been stuck for a while :frowning:

Learn a bit of vector math. Especially what normal-vectors are and what a dot-product is. Otherwise you will get stuck again. Other than that, google for “2d vector math primer” and “2d vector reflection”.

I was afraid you would say that. Want to make a game not learn maths. Thanks for the help I’ll check those out :slight_smile:

Much of game making actually is math…

I don’t have a problem with maths. I just can’t work this out and I’m getting fed up of not finding any answers.

You don’t need to calculate a new vector from the angle of reflection. You can just invert the y component of vector A to get vector B.

What the actual …?

Normal vectors and dot product? Come on.

I’d suggest exactly what Screem suggested, and I’ll add a little bit of code to it, too:

This only works when both objects are considered to behave like two axis-aligned boxes:


// Assuming some kind of vector implementation like Vec2(int x, int y) with the fields "x" and "y".
// AABB = Axis Aligned Bounding Box
public Vec2 getCollisionVelocity(Vec2 velocity, AABB ball, AABB object) {
    // side is either TOP, LEFT, RIGHT or BOTTOM
    int side = getCollisionSide(ball, object);
    switch (side) {
    case TOP:
    case BOTTOM:
        return Vec2(velocity.x, -velocity.y);
    case LEFT:
    case RIGHT:
        return Vec2(-velocity.x, velocity.y);
    }
}

The principle of reflecting along an axis is really easy. The hard thing is finding out the side at which they collided. But depending on what kind of game you are creating this might not actually be too hard.

You only need advanced vector math with normals similar when you want to bounce an object off from an arbitrary surface / edge. But then you might as well just use a Physics library like Box2D, because that stuff can get really hard when you want to add rotation upon colliding or similar ^^

Programming(games) is just problem solving. Math is great tool to solve most of the problems.

Thanks for all the replies, sorry I was getting a little frustrated :frowning: I read through some vector math refreshers and came to the same conclusion of inverting the axis. I’m attempting a Breakout clone so I think working out where the ball has hit will be trivial for everything bar the bricks in the middle

If there is no resistance, and the thing is totally elastic collision. Then the speed along the board won’t change, while the vertical speed reverse.

I bet there is some nice bounce function around…
A while ago i made one from a known bounce math formula.


public static Vector2f CalcBounce(Vector2f vect, Vector2f surfacenormal, float bounce){
        float add = Vector2f.dot(vect, normal);
        return new Vector2f(bounce * (-2f * add * normal.x + vect.x), bounce * (-2f * add * normal.y - vect.y));
}

vect = xspeed / yspeed;
surfacenormal = normal of the surface
bounce = bounce of object, with this you can play with different material types (speeding up / slowing down).

Don’t fight math. It will help. Especially vector-math is a very valuable tool for games. Inverting the axis is just a shortcut in this special case. Next time you need to calculate your balls movement (nice wording ;)) in another case, you’ll need to learn it anyway or fall back to another shortcut for another special case, and on and on and on…

If you limit your solutions to special cases, you limit your game to special cases and therefor limit your creativity to special cases…

I completely understand, and I think so too. In the long run vector mathematics is really useful, but for beginners, it’s a good idea to start of simple.
You might even learn vector mathematics this way.
For example you might learn the dot product between a and b this way:
Special cases:
[x] (1, 0) dot (1, 0) = 1
[x] => Equal vectors -> 1
[x] (1, 0) dot (-1, 0) = -1
[x] => 180° between vectors -> -1
[x] (1, 0) dot (0, 1) = 0
[x] (0, 1) dot (1, 0) = 0
[x] => 90° between vectors -> 0
[x] (1, 0) dot (2, 0) = 2
[x] (2, 0) dot (2, 0) = 4
[x] (3, 0) dot (2, 0) = 6
[x] => 0° between non-normalized-vectors -> |a| * |b|
… and so on :slight_smile:

Well, understanding the “special cases” is very useful in understand the general case…if you bother to make the connection. Both in math and CS this is a useful learning technique…the trick is not to black box.