Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Transferring Inertia

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

  1. MWizard

    MWizard

    Joined:
    Jan 5, 2017
    Posts:
    42
    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:
    1,251
    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:
    42
    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!