Search Unity

Transferring Inertia

Discussion in 'Physics' started by MWizard, Jun 12, 2018.

  1. MWizard

    MWizard

    Joined:
    Jan 5, 2017
    Posts:
    78
    Howdy!

    Ok, so here's the scenario: I have two players (online interaction) each in control of their own deal. One is a host, one is client. That Multiplayer side of things is working just as expected. My problem comes into play where I want the player to be able to bump into the other player and make them move based on how fast they rammed them.

    The goal I am trying to achieve is something like real physics, but since they are both controlled by other players I am not sure if it's a network side thing, or just something I can code into the player controller as a collision.

    1) If one player flies at the other player that is stationary, if the moving player stops all input at the moment of impact, I would like that inertia/force to get transferred to the other player. Not expecting the moving player to stop immediately, but I would expect the player to take on about that much speed in the general velocity I'd expect. Like a pool ball, if you will.

    2) If both players are heading right at each other, they will likely essentially stop moving because of equal forces.

    I am fairly confident that this is possible with just... colliders + rigidbodies if they weren't being controlled by input. Or maybe I'm mistaking a networking challenge for a physics challenge? Any input is appreciated. Thanks!
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    You want to transfer the momentum. It's a typical problem in basic dynamics. Here you have a couple of lectures on this:

    https://people.physics.tamu.edu/mahapatra/teaching/ch8.pdf
    http://www.physicsclassroom.com/calcpad/momentum

    In summary, each player has a momentum expressed as mass times velocity. When they collide they exchange some momentum as an impulse. The impulse is equal for both players but opposite directions, resulting in a change in their velocities. But the sum of the momentums before and after the collision is the same.

    It's up to you to handle the collision over the network and apply each player the corresponding impulse as result of the collision. You may use Rigidbody.AddForce with ForceMode.Impulse for that.
     
  3. MWizard

    MWizard

    Joined:
    Jan 5, 2017
    Posts:
    78
    Awesome! Thanks for that. I had already started messing with Rigidbody.AddForce and was messing around with a few tests, but I couldn't figure out the API for transferring the momentum. I'll keep looking. Thanks!