MageFlight / merlin-game-engine

GNU General Public License v3.0
1 stars 0 forks source link

Make kinematicBodies able to be pushed. #2

Closed MageFlight closed 1 year ago

MageFlight commented 1 year ago

This could also be used to implement moving platforms that pull the player on it. If the other colliding object is a kinematicBody, then the velocities should be added. If mass is implemented, more sophisticated calculations would be required. Example / equations:

Elastic Collisions In One Dimension With Two Unknowns

Problem

A 4kg ball moving east at a speed of 5 m/s strikes a 2kg ball at rest. Calculate the veloities of the two balls assuming a perfectly elastic collision.

Work

Equations to use:

$$m_1v_1 + m_2v_2=m_1v_1'+m_2v_2'$$ $$(4)(5)+(2)(0)=4v_1'+2v_2'$$ $$20=4v_1'+2v_2'$$

Now, let's focus on the Simplified Equation of Conservation of Kinetic Energy. $$v_1+v_1'=v_2+v_2'$$ $$5+v_1'=0+v_2'$$ $$5+v_1'=v_2'$$ $$5=-v_1'+v_2'$$

Now, we can solve for $v_1'$ or $v_2'$ by using a system of equations.

$$4v_1'+2v_2'=20$$ $$-v_1'+v_2'=5$$

From here, it's simple algebr: $$4v_1'+2v_2'=20$$ $$4(-v_1'+v_2'=5)$$


$$4v_1'+2v_2'=20$$ $$-4v_1'+4v_2'=20$$

If we add both equations together, $v_1'$ cancels out and we are left with only $v_2'$.

$$6v_2'=40$$ $$v_2'=6\frac{2}{3}\space m/s$$

Now, we can calculate $v_1'$.

$$-v_1'+v_2'=5$$ $$v_2'=v_1'+5$$

Substitute $v_2'$

$$6\frac{2}{3}=v_1'+5$$ $$1\frac{2}{3}=v_1'$$

Therefore, the final velocities of both balls are

$$v_1'=1\frac{2}{3}$$ $$v_2'=6\frac{2}{3}$$

Checking the Answers

To check that the answers are correct, check that momentum is conserved. $$m_1v_1+m_2v_2=m_1v_1'+m_2v_2'$$ $$(4)(5)+(2)(0)=(4)(1\frac{2}{3})+(2)(6\frac{2}{3})$$ $$20=\frac{20}{3}+\frac{40}{3}$$ $$20=\frac{60}{3}$$ $$20=20$$

We should also check that kenetic energy is conserved: $$v_1+v_1'=v_2+v_2'$$ $$5+1\frac{2}{3}=0+6\frac{2}{3}$$ $$6\frac{2}{3}=6\frac{2}{3}$$

Our answers conserve both momentum and kinetic energy, so they are correct!

Finding A Single Equation

To find single equations for $v_1'$ and $v_2'$, using a systems of equations to combine the equations for conservation of momentum and conservation of kinetic energy will yield the correct answer. The full process is not listed here, because that too much work.

Source for the final equations: 2dcollisions2.pdf

Another source from Khan Academy. This one goes into much more depth than the other source. $$v_1'=\frac{v_1(m_1-m_2)+2m_2v_2}{m_1+m_2}, v_2'=\frac{v_2(m_2-m_1)+2m_1v_1}{m_1+m_2}$$

MageFlight commented 1 year ago

https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331