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.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

RigidBody speed jittering

Discussion in 'Physics' started by Fregloin2, Apr 19, 2018.

  1. Fregloin2

    Fregloin2

    Joined:
    Dec 10, 2015
    Posts:
    6
    Hello. I need to simulate carriage moving from the slope. It consists from several parts, say ,main part, some details, two bogie and four wheels. All these parts have own rigid bodies. All rigid bodies use gravity. Wheels and bogies use HingeJoints. It moves smothly under the gravity force. But when i want to calculate its acceleration, i get strange values. Some of them are positive, some of them negative.
    Here is the script which calculates current rigidbody acceleration.
    Code (CSharp):
    1. /// <summary>
    2.     /// Компонет определающий скорость GameObject к которому он подсоединён
    3.     /// </summary>
    4.     public class Speed : MonoBehaviour {
    5.  
    6.         /// <summary>
    7.         /// Твердое тело объекта
    8.         /// </summary>
    9.         private Rigidbody rigidBody;
    10.  
    11.         /// <summary>
    12.         /// Gets the speed ms.
    13.         /// </summary>
    14.         /// <value>The speed ms.</value>
    15.         public    float        SpeedMs { get { return rigidBody.velocity.z * -1f; } }
    16.  
    17.         /// <summary>
    18.         /// Gets the speed kmh.
    19.         /// </summary>
    20.         /// <value>The speed kmh.</value>
    21.         public    float        SpeedKmh { get { return rigidBody.velocity.z * -3.6f; }  }
    22.  
    23.         /// <summary>
    24.         /// Gets the acceleration.
    25.         /// </summary>
    26.         /// <value>The acceleration.</value>
    27.         public    float        Acceleration { get { return acceleration.z * -1f; } }
    28.  
    29.         private Vector3 lastVelocity;
    30.         private Vector3 acceleration;
    31.  
    32.        
    33.         void Start () {
    34.             rigidBody = GetComponent<Rigidbody>();
    35.             lastVelocity = rigidBody.velocity;
    36.         }
    37.  
    38.         /// <summary>
    39.         /// Просчёт физики
    40.         /// </summary>
    41.         void FixedUpdate() {
    42.             acceleration = (rigidBody.velocity - lastVelocity) / Time.fixedDeltaTime;
    43.             lastVelocity = rigidBody.velocity;
    44.             Debug.LogFormat("accel {0}", acceleration.z);
    45.         }
    46.  
    47.     }
    And when carriage is moving on the track (and no difference whether it is on the plain track or slope with some angle) its acceleration always jittering. I've attached screenshot with log output.
    So how to get smooth acceleration value?
    I tryed to disable all extra threads and timers in the project, unfortunally the effect remained the same.
    I tryed to make calculations in Update instead of FixedUpdate. Nothing changed.
    I have to simulate physics quite preciously. Unfortunally i get strange results.
     

    Attached Files:

  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,423
    Your acceleration calculation looks correct to me. If you're receiving those values, then surely the velocity change in the rigidbody is not smooth. Check out how are you actually modifying the velocity of your object. For example, if the joints have some spring effect, then this will be added to the acceleration.

    Also, note that your code computes the acceleration in world space. So it will show the proper acceleration as long as the object is moving along the world Z axis.
     
  3. Fregloin2

    Fregloin2

    Joined:
    Dec 10, 2015
    Posts:
    6
    Spring effects are disabled. I add some focre in the beginning, and then carrige moves by inertion from the slope. When it moved on the slope, its velocity and acceleration are prerry smooth. But when it begins moving on the flat terrain, these parameters begin jitter. Firstly i tryed to change physics material of wheels, carriage and terrain. Nothing helped though. Then i tryed to play with hinge joint parameters. The same effect. But when i added simple cylinder with collider and rigidbody, it seemed that parameters were smoother. Unfortunally jitterring was present too.
     
  4. Fregloin2

    Fregloin2

    Joined:
    Dec 10, 2015
    Posts:
    6
    I've put current speed and previous speed logging in FixedUpdate, and sometimes previous speed is greater than current, some time is less. I don't understand why does it happen. In theory, when object is deacellerating, it's speed must decrease too. But i see that the speed of rigid body is jittering on the flat surface.
     
  5. Fregloin2

    Fregloin2

    Joined:
    Dec 10, 2015
    Posts:
    6
    By the way, i changed masses of rigid bodies of the carriage to 1, nothing had changed.
     
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,423
    If you're measuring the speed in the topmost rigidbody, then it's velocity will accumulate the errors and "springy" effects of the joints and the other rigidbodies. Joints in Unity are no "solid joints", but there's always some amount of spring effect. The velocity variations you observe are likely caused by the physics resolving the system while it decelerates.

    I can imagine a situation like this: one of the wheels decreases its speed a bit more than the others, reducing the general velocity. But then the other wheels "push" the first one which "releases" the energy from the joint spring, causing a small sudden acceleration. This may happen many times along all the wheels while the vehicle is decelerating.
     
  7. Fregloin2

    Fregloin2

    Joined:
    Dec 10, 2015
    Posts:
    6
    So how to solve this problem? Well, i see, that some spring effect is present in joints. When i put the carriage several meters above the terrain and than release, it falls down, and at the collide with terrain moment the biggest and most heavy part shows some spring through the bogies. I supposed these effects, but i need hinge joints to operate wheels on axis, and bogies attached to main part to simulate real carriage which can follow rails with branches. My colleage desided try to solve this problem, but i think it is because of unity engine.
     
  8. Fregloin2

    Fregloin2

    Joined:
    Dec 10, 2015
    Posts:
    6
    Anyway, thanks for reply!
     
  9. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,423
    The cause is not Unity but the underlying physics engine, PhysX, which works that way. A possible solution to the problem would be applying some smoothing to the received values, like using the average of the last n velocity values received. This would induce some lag but the results would be smoother.