I'm making a 2D game where a ball collides with an obstacle. The ball has a velocity V. When it collides with the obstacle the impact normal vector is iN. I managed to make the ball bounce off the obstacle with the following calculations:
- dotProduct = V.x * iN.x + V.y * iN.y
- V.x = V.x + 2 * (iN.x * dotProduct)
- V.y = V.y + 2 * (iN.y * dotProduct)
So when doing this the ball bounces fine with it's new velocity, now I can't really figure out how to do the same when the obstacle is moving, here is an image to showcase the issue:
In the above picture OV is the velocity of the obstacle, my guess was to add OV to the new velocity but it didn't work quite well, is it a valid solution and the error comes from my program ?




This might be better suited for the physics page. That said you should move to a reference frame where the wall is not moving, do the calculation there, and move back.
To move to the reference frame where the wall is not moving, we need to subtract away the velocity of the wall for everything. Now the wall's velocity is zero and the ball has a "new" velocity $v' = (v - ov)$. You can then do the calculation as if the wall were stationary, and then move back to the original reference frame (add the velocity of the wall back).
Note that whey I say add/subtract the velocity of the wall that I mean the velocity as a vector. If your wall is only moving in the $x$ direction, then you only need to adjust the $x$-coordinate.