Calculate bounce velocity vector of an object colliding with a moving object

vectors

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:

  1. dotProduct = V.x * iN.x + V.y * iN.y
  2. V.x = V.x + 2 * (iN.x * dotProduct)
  3. 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:

enter image description here

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 ?

Best Answer

If the object moves with velocity $\vec v_0$ the ball with velocity $\vec v$ and the normal at the impact point is $\vec n$ then we have:

$$ \vec v = \vec v_{\vec n}+\vec v_{\Pi}\\ \vec v_{\Pi} = \vec v - \vec v_{\vec n_1} \\ \vec v_r = (\vec v-\vec v_0)_{\Pi}-(\vec v-\vec v_0)_{\vec n}\\ \vec v_r = (\vec v-\vec v_0) -2((\vec v-\vec v_0).\vec v_{\vec n})\vec v_{\vec n} $$

with

$$ \vec v_{\vec n} = \left(\vec v\cdot\left(\frac{\vec n}{||\vec n||}\right)\right)\frac{\vec n}{||\vec n||} $$

where $\vec v_r$ represents the reflected ball velocity after collision

NOTE

Here $\Pi$ represents the plane passing by the impact point with normal $\vec n$

Attached three cases. Here

$$ \begin{cases} \vec v \ \ \mbox{red}\\ \vec v_0 \ \ \mbox{green}\\ \vec n \ \ \mbox{black}\\ \Pi \ \ \ \mbox{dashed cyan}\\ \vec v_r \ \ \mbox{blue} \end{cases} $$

enter image description here enter image description here enter image description here

Related Question