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

1.4k Views Asked by At

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 ?

2

There are 2 best solutions below

1
On BEST ANSWER

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.

4
On

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