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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

RigidBody2D's AddTorque (apparently similar rigid bodies and parameter, different results)

Discussion in 'Physics' started by jathrek, Jan 1, 2016.

  1. jathrek

    jathrek

    Joined:
    Jul 16, 2015
    Posts:
    36
    [Edit] By the way, Happy New Year! ;-)

    Hello,

    I'm currently working on a small UNet multiplayer game in which the players' movements are controlled through forces applied on their rigid bodies. I'm in the process of migrating from a hardcoded vehicle per player to a system where the vehicle of a player can be replaced by another one. Basically, the vehicule was directly present in the LocalPlayer object, Now, the ship is instantiated at runtime and linked to the LocalPlayer object, causing the input to be "applied on" the ship (if present). The only difference there is that the rigid body is not at the level of the player's object (where the script is located), but in a child game object.

    Everything seems to be working, apart from the AddTorque's behaviour (the AddForce is working as excepted, though, which makes it even more puzzling).

    The properties of the rigid bodies, on which AddTorque is being called, have the same properties (mass, drags...) in the old and new mechanisms. I also checked that properties like "mass", "centerOfMass"... were the same at runtime by adding some logging in FixedUpdate, where the calls of AddForce and AddTorque are made.

    I also did some logging of the values passed as parameter to AddTorque and the value is exactly the same. The FixedUpdate method is also called at the same frequency (50 times per second).

    In the end, the highest value reached (after continuous calls to AddTorque for a short while) for the angularVelocity is 97.73939 in the first mechanism, but barely 4.583659 in the new mechanism.

    Would someone have an idea why the resulting "angularVelocity" value is so different, though every properties, values and parameters used in the process are the same? Once again, the AddForce behaves correctly (same properties, same parameters, but same result - for old and new mechanisms - in that case).

    Here's a summary of the data;
    Rigid body's mass: 0.5271614
    Angular drag: 5
    Parameter passed to the AddTorque's method: 0.4
    Resulting angular velocity (old mechanism): 97.73939
    Resulting angular velocity (new mechanism): 4.583659

    PS; I had encountered a first issue due to the fact that, originally, the "auto mass" flag was used, but it seemed that in the new mechanism it was not working correctly (the object was instanciated manually by the server and linked to the player object), causing the rigid body's mass to be 1. To solve this, I simply deactivated the "auto mass" and entered manually the computed value.

    Thank you!
     
    Last edited: Jan 1, 2016
  2. jathrek

    jathrek

    Joined:
    Jul 16, 2015
    Posts:
    36
    After some very random tests, I found the difference that was causing the issue (though I've no idea why...).

    The collider of the game object in the old mechanism was a regular collider, while the one in the new mechanism was a trigger. This seems to be what caused the issue.

    I found an old post talking about this (http://answers.unity3d.com/questions/250233/rigidbody-behaves-differently-when-i-add-a-collide.html), but the answers seem to mention the impact of the presence of a collider on the rigid body's centerOfMass, which was always the same in the logging I made...

    Maybe I'll understand... One day... ;-p
     
  3. jathrek

    jathrek

    Joined:
    Jul 16, 2015
    Posts:
    36
    After some further searches, I went a bit further and found the reason why the behaviour was different;

    The presence of a collider impacts the value of the RigidBody2D's "inertia", which is 1 by default.
    The non-trigger collider was causing it to be of a much lower value, around 0.04, which caused the AddTorque to cause a much faster rotation.

    If you manually set the value of that "inertia" property, it won't take the collider (trigger/non-trigger) into consideration any more and the behaviour will be the same in all cases)...

    Should I remove this topic all together or could my "mistakes" be usefule to somebody else?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    Triggers don't affect the center-of-mass nor are they included in the rotational inertia calculations. If you don't have any non-trigger colliders then the center-of-mass is the rigid-body poisition and a default rotational inertia of 1 is used so that the (effectively empty) Rigidbody2D will still rotate. If you add a single non-trigger collider then it'll be used to calculate the center-of-mass and rotational inertia.