Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Pushing objects with AddForce (relative mass issue)

Discussion in 'Physics' started by Djedjeska, May 27, 2020.

  1. Djedjeska

    Djedjeska

    Joined:
    Dec 24, 2016
    Posts:
    6
    Hello there,

    I'm having a bad time figuring how to make this work.
    I'm creating a "Ballance" like game, where you control a ball which "state" can be either paper, wood, stone.

    In the original game, this state would affect :
    1) its velocity (stone ball at full speed is faster than paper ball)
    2) its manoeuvrability (paper ball is extremely manoeuvrable, meaning that its momentum is light and its easy to make it stop when at full speed, whereas the stone one would take much more time to stop)
    3) and this is where I fail : its influence on other objects during collisions

    What I did is that I set up different mass on the rigidbodies of my balls, which does a good work at making them more or less manoeuvrable. For the velocity, I set a start speed, and a maximum speed that can be reached.

    Now about pushing objects, what I expect is that the lighter ball (paper) can't push a box of higher mass, whereas the stone can. But what happens is precisely the contrary ! I guess it's because of the velocity of the paper ball increasing faster (acceleration ?), but it seems strange to me since if we take the relative mass of the two objects, that result shouldn't happen.

    Does somebody have an idea ? What do I do wrong ?

    Here is my code :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Ball : MonoBehaviour
    6. {
    7.  
    8.     public float speed, max_velocity = 8f, brake_force;
    9.     [SerializeField]
    10.     private Rigidbody rb;
    11.     private Renderer rd;
    12.     [SerializeField]
    13.     private Camera cam;
    14.     public Transform cameraJig;
    15.     private float rot = 0;
    16.     [SerializeField]
    17.     private float sensivity = 100;
    18.     [SerializeField]
    19.     private string type = "wood";
    20.     public Text veltext;
    21.  
    22.     void Start()
    23.     {
    24.         Cursor.lockState = CursorLockMode.Locked;
    25.         rb = GetComponent<Rigidbody>();
    26.         rd = GetComponent<Renderer>();
    27.         Transform("wood");
    28.     }
    29.  
    30.     private void Update()
    31.     {
    32.         if (Input.GetKey(KeyCode.I)) {
    33.             Transform("wood");
    34.         }
    35.         else if (Input.GetKey(KeyCode.O)) {
    36.             Transform("stone");
    37.         }
    38.         else if (Input.GetKey(KeyCode.P))
    39.         {
    40.             Transform("paper");
    41.         }
    42.     }
    43.  
    44.     void Transform(string new_type)
    45.     {
    46.         type = new_type;
    47.         rd.material = Resources.Load<Material>("Materials/" + type);
    48.         if (type == "wood")
    49.         {
    50.             rb.mass = 0.5f;
    51.             speed = 4;
    52.             max_velocity = 6;
    53.             brake_force = 5;
    54.         }
    55.         else if (type == "stone")
    56.         {
    57.             rb.mass = 1;
    58.             speed = 5;
    59.             max_velocity = 8;
    60.             brake_force = 1.1f;
    61.         }
    62.         else if (type == "paper")
    63.         {
    64.             rb.mass = 0.1f;
    65.             speed = 3;
    66.             max_velocity = 5;
    67.             brake_force = 10;
    68.         }
    69.     }
    70.  
    71.     void FixedUpdate()
    72.     {
    73.  
    74.         // Rotate the camera object around the x-axis
    75.         //rot = Input.GetAxisRaw("Mouse X");
    76.         //cam.transform.RotateAround(cameraJig.position, new Vector3(0, rot, 0), sensivity * Time.fixedDeltaTime);
    77.  
    78.         // Move the ball
    79.         float moveHorizontal = Input.GetAxis("Horizontal");
    80.         float moveVertical = Input.GetAxis("Vertical");
    81.  
    82.         //Decelerate
    83.         if (moveHorizontal == 0 && moveVertical == 0)
    84.         {
    85.             //Stop
    86.             if (Mathf.Abs(rb.velocity.x) < 1 && Mathf.Abs(rb.velocity.z) < 1)
    87.             {
    88.                 rb.velocity = new Vector3(0, rb.velocity.y, 0);
    89.             }
    90.             else
    91.             {
    92.                 //Auto brake
    93.                 if (rb.velocity.x > 1)
    94.                     moveHorizontal = -1f;
    95.                 else if (rb.velocity.x < -1)
    96.                     moveHorizontal = 1f;
    97.                 if (rb.velocity.z > 1)
    98.                     moveVertical = -1f;
    99.                 else if (rb.velocity.z < -1)
    100.                     moveVertical = 1f;
    101.  
    102.                 Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    103.                 movement = movement * Time.fixedDeltaTime * 50;
    104.                 rb.AddForce(movement);
    105.             }
    106.         }
    107.         //Accelerate
    108.         else if (rb.velocity.magnitude < max_velocity)
    109.         {
    110.             //Manual brake
    111.             if (Mathf.Sign(moveHorizontal) != Mathf.Sign(rb.velocity.x))
    112.             {
    113.                 moveHorizontal *= brake_force;
    114.             }
    115.             if (Mathf.Sign(moveVertical) != Mathf.Sign(rb.velocity.z))
    116.             {
    117.                 moveVertical *= brake_force;
    118.             }
    119.             Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    120.             //movement = movement.normalized * speed;
    121.             movement = movement * speed * Time.fixedDeltaTime * 50;
    122.             rb.AddForce(movement);
    123.         }
    124.  
    125.  
    126.         veltext.text = "Type : " + type +
    127.             "\nMass : " + rb.mass +
    128.             "\nSpeed : " + Mathf.Floor(rb.velocity.magnitude) + "/" + max_velocity +
    129.             "\nVelocity : " + rb.velocity +
    130.             "\nHorizontal : " + moveHorizontal +
    131.             "\nVertical : " + moveVertical;
    132.          
    133.     }
    134. }
    135.  
    Thank you for your answers
     
  2. Djedjeska

    Djedjeska

    Joined:
    Dec 24, 2016
    Posts:
    6
  3. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Pushing by applying a force will follow the equation Force = mass x acceleration, which you can re-arrange to give you acceleration = Force / mass . So you can see this will be dependent upon the variable "speed" (perhaps not a good choice of variable name as it is being used as a force) and the combined masses of the two objects.

    If a collision occurs, assuming your collisions are inelastic (the objects stick together), you can assume conservation of momentum, which leads to:
    object1_mass x object1_velocity + object2_mass x object2_velocity = (object1_mass + object2_mass) x velocity

    Even if the collision is just elastic you can check the results with conservation of momentum:

    object1_mass x object1_init_velocity + object2_mass x object2_init_velocity =
    object1_mass x object1_final_velocity + object2_mass x object2_final_velocity

    I suspect your problem is with the relative masses of the objects and the ratio of mass to your variable "speed".

    Edit: changed the bit about collisions for correct wording and a bit more information, though I suspect your problem is covered by the first bit anyway.
     
    Last edited: Jun 2, 2020
  4. Djedjeska

    Djedjeska

    Joined:
    Dec 24, 2016
    Posts:
    6
    Thank you very much for your answer AITheSlacker. I figured it would be an issue of physics which I admit not being really good at.
    I'll try to describe my problem the best I can :

    When the different balls are stuck with the box (there is no momentum), what I expect is :
    - the paper ball cant push the box
    - the wood ball can push it but slowly
    - the stone ball can push it easily

    I don't know if this behaviour are "realistic" according to the physic equations you gave me, but this is the behaviour used in the original game. If I had to explain it I would say that because of the mass of the stone ball, the box isnt such "heavy" an obstacle so it can gain momentum, but for the paper ball, because of the relative masses, it would not be possible to gain momentum.

    But now what happens in my game is the contrary... Does it mean that the "speed" and "force" are to be differentiated for each ball ? But how could I achieve that, since I'm using unity physics system and it seems very tricky to me...
     
    Last edited: Jun 4, 2020
  5. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    From the F = ma you can see that force is directly proportional to acceleration with mass being the scalar. Speed is just a function of acceleration and time.

    I would suggest that you set a maximum speed for each ball (you can easily do this by not applying any more force if the ball has reached its maximum speed).

    Now you need to choose some forces and masses, how about starting with something simple like this:

    Code (CSharp):
    1. /*
    2. Ball    F       m
    3. paper   1       1
    4. wood    10      10
    5. stone   100     100
    6.  
    7. Box mass: 10kg
    8.  
    9. */
    With this starting point, they will all have the same acceleration (1 m/s/s). Now by choosing the mass of the box appropriately, e.g. 10 kg, the stone ball will not change much, the wood ball will accelerate at half the normal rate and the paper ball will only accelerate at 10% the normal rate.

    Note: They all still move. As you can see from the equation F = ma any level of force will result in some acceleration. To prevent this you need to introduce another force that opposes movement. That will be friction.

    Look at this: https://docs.unity3d.com/Manual/class-PhysicMaterial.html
    I believe that to get it to work in a robust way you need to Edit - ProjectSettings - Physics - FrictionType - Two Directional. But perhaps this is better now, so just have a play and see.

    The friction force is simple to work out: mass of object x gravity (9.81) x (static)friction coefficient.

    Example: a 10kg object, with a coefficient of friction of 0.5 will require a force of 10 x 9.81 x 0.5 = 49.05 N to get moving.

    So now you can see how to play with their masses, forces and friction to get what you want.

    Last bit: You say that the push speed limit needs to vary with ball type. Remember, at the moment any net force acting on the system will accelerate as long as the force is applied. The only cap at the moment is the ball maximum speed. To fix this you will need to add drag force. A drag force is a velocity dependent force that opposes motion e.g. aerodynamic drag.

    This can be done two ways:

    1) Increase the drag value on the box rigid body by experiment until you get something that works. I do not know how PhysX works with drag, so you may have to guess a bit here.

    2) On the box, add a script with a FixedUpdate method and write your own drag code.

    Once drag force = the applied force - friction force there will be no further acceleration, i.e. speed is capped.

    Hopefully, that is enough for you to get a solution with. Good luck!
     
  6. Djedjeska

    Djedjeska

    Joined:
    Dec 24, 2016
    Posts:
    6
    Thank you so much for taking the time to answer me. I will read it very carefully and should be able to make it work.