3

I am currently trying to code a 2D physics engine in gamemaker studio, however I have run into a problem.

I have found the following useful website to help me calculate the new x and y components of my speed vector after collision: http://williamecraver.wixsite.com/elastic-equations

EDIT: I'll try and be more specific about my problem: I want to calculate collision in 2D. In order to do this I first rotate my x-y axis so that x runs from the centre of one object through the center of the other object (both are circles). (the image in the link nicely illustrates the situation)

In the link I provide, calculation of resulting vx and vy vectors for each object after the collision is explained. It uses the following equations to rotate the vx and vy components(I'm sorry I don't know how to insert proper equations)

vxr = v * cos(theta - phi)

vyr = v * sin(theta - phi)

with vxr being the rotated vx vector (same for vy), theta being my original angle between v and the x axis, phi being my rotation angle

Then these "rotated" vx and vy components are inserted into the equations for conservation of momentum and kinetic energy to solve a 1D collision (which is possible because of the rotation of the axis). Afterwards he rotates the axis back using the following equations:

vfx = vfxr * cos(phi) + vyr * cos(phi + pi/2)

vfy = vfyr * sin(phi) + vyr * sin(phi + pi/2)

with vfxr/vfyr being the resulting rotated x/y component after collision, vfx vfy being the resulting x/y components transformed back into the regular x-y plane.

My problem is that my y-axis is pointing down (instead of the conventional up). Therefore I believe the original equations for vyr should be:

vyr = v * -sin(theta - phi)

My first question is if my assumption for the vyr component is correct and if I missed other things that should change in the calculation because of the inverted y-axis. What about the equation to rotate vx and vy back to the regular x-y plane?

Secondly, if my assumptions are correct, I am unable to perform the calculations when inserting these equations into the 1D collision equations. Especially regarding the conversion back to regular x-y plane, I don't understand where these equations come from and how they change with an inverse y-axis.

lygho
  • 31
  • Seems to me that you just need to replace all the angles by their negations and use the identities $\cos(-x)=\cos x$ and $\sin(-x)=-\sin x$. – amd Aug 28 '17 at 21:41
  • These are typical variations in 2D coordinate systems, but it is difficult to grasp what problem you are having because of them. In general, despite the display variation, a resultant vector will be obtained by vector addition. I'd guess you are having problems with the angles, but without seeing more of these problems, I cannot recommend a "cure". – hardmath Aug 28 '17 at 23:58
  • I have edited my orginal question to be more specific. If anything is unclear let me know and I'll try to clarify. The link provided illustrates the situation I'm trying to calculate nicely. My problem with the calculation is explained in the last two paragraphs of the question – lygho Aug 29 '17 at 18:44
  • Thanks for the clarification. I'm going to try and make some edits to give "proper" math notation, but I need to study the website you linked to, to better understand what the notation means. – hardmath Aug 29 '17 at 19:28

1 Answers1

3

Hence the problem with doing this problem using angles. I suggest you use vectors and basic vector manipulations to get the same result. In my convention, a vector quantity is boldfaced and its magnitude italicised. For example $$ \begin{align} \mathbf{v} & = \pmatrix{v_x \\ v_y} & v = \| \mathbf{v} \| = \sqrt{v_x^2+v_y^2} \end{align} $$

  1. Establish the direction of the contact ${\bf n}$. For balls use the center to center vector. If the two centers have coordinates $\mathbf{r}_1$ and $\mathbf{r}_2$, then $$\mathbf{n} = \frac{ \mathbf{r}_2 - \mathbf{r}_1 }{ \| \mathbf{r}_2 - \mathbf{r}_1 \| } $$
  2. Get the reduced mass $m$ of the system. If the two masses are $m_1$ and $m_2$ then $$m = \left( \frac{1}{m_1} + \frac{1}{m_2} \right)^{-1} = \frac{m_1 m_2}{m_1 + m_2} $$
  3. Get the impact speed. This is the relative speed along the direction of the contact $$v_{imp} = \mathbf{n} \cdot (\mathbf{v}_1 - \mathbf{v}_2) $$ where $\cdot$ is the vector dot product.
  4. Decide what the coefficient of restitution $\epsilon$ shall be. For perfectly elastic collisions use, $\epsilon=1$.
  5. Calculate the total momentum exchanged (a.k.a. impulse). This is a scalar quantity $$ J = (1+\epsilon) m \, v_{imp} $$
  6. Calculate the final velocities after impact from the impulse

$$\begin{align} \mathbf{v}_1^f & = \mathbf{v}_1 - \frac{J}{m_1} \mathbf{n} \\ \mathbf{v}_2^f & = \mathbf{v}_2 + \frac{J}{m_2} \mathbf{n} \end{align} $$

That is it. You have yourself a proper 2D collision model regardless of the convention of which way the x and y axes go. As long as you are consistent with the components of the positions and velocities, the rest works out on its own. :-)

John Alexiou
  • 13,816