Trouble with RigidBody2D add torque.

Hi everyone, I’m creating unity tests to verify that the way I apply forces to my objects is correct in Unity 2019.1.10f1.

I cannot seem to get the RigidBody2D.addTorque right in my classes. When that identical setup works for add force.

In my unity test in play mode, I create a RigidBody2D and make sure that gravity is not applied to it and that angular drag is 0.
Therefore my RigidBody has an inertia of 1 which is the default.
Here is the setup code

[OneTimeSetUp]
public void Init()
{
    //Init runs once before running test cases.
    go = new GameObject();
    rb = go.AddComponent<Rigidbody2D>();
    // Don't forget this, it seems like gravity is not the same as the
    // Project settings.
    rb.gravityScale = 0;
    rb.angularDrag = 0;
}


[SetUp]
public void SetUp()
{
    //SetUp runs before all test cases
    rb.velocity = Vector2.zero;
    rb.angularVelocity = 0;
    rb.position = Vector2.zero;
    rb.rotation = 0;

}

I would expect that if I apply a force of 50 N.m and my FixedDelta time is 0.02s, my rotational speed would be of 1 degree per second after that force has been applied and that the tick has passed.

I then apply a torque the following way :

[UnityTest]
public IEnumerator RotationTest2()
{
Debug.Log($"Initial angular speed is : {rb.angularVelocity}.");
rb.AddTorque(50, ForceMode2D.Force);
yield return null; // do simulation frame

Debug.Log($"Angular acceleration is : {50}.");
Debug.Log($"Final angular speed is : {rb.angularVelocity}.");
Debug.Log($"Expected angular speed is : {50 * Time.fixedDeltaTime / rb.inertia}.");
Assert.Less(Mathf.Abs(rb.angularVelocity - 50 * Time.fixedDeltaTime / rb.inertia)), 0.00001f);
}

The output of my unit test is the following :
Initial angular speed is : 0.
Angular acceleration is : 50.
Final angular speed is : 57.29578.
Expected angular speed is : 1.

Does someone see the mistake that I am currently doing?

Thank you in advance guys!
Elliot.

Could someone please confirm that they also get the angular speed I have on their version?

Steps to replicate :

  • Create new project
  • In assets menu : Create>Testing>Test assembly folder
  • Paste the attached script in the created folder
  • Run it with the test runnner that can be opened in Window>general>TestRunner
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

/// <summary>
/// Testing the unity engine Physics2D
/// </summary>
namespace Tests
{
    public class RigidBodyTest
    {
        /// <summary>
        /// Game object on which we test the engine
        /// </summary>
        GameObject go;
        /// <summary>
        /// Rigid body 2D instance to test the engine with
        /// </summary>
        Rigidbody2D rb;


        [OneTimeSetUp]
        public void Init()
        {
            //Init runs once before running test cases.
            go = new GameObject();
            rb = go.AddComponent<Rigidbody2D>();
            // Don't forget this, it seems like gravity is not the same as the
            // Project settings.
            rb.gravityScale = 0;
            rb.angularDrag = 0;
        }


        [SetUp]
        public void SetUp()
        {
            //SetUp runs before all test cases
            rb.velocity = Vector2.zero;
            rb.angularVelocity = 0;
            rb.position = Vector2.zero;
            rb.rotation = 0;

        }

        [UnityTest]
        public IEnumerator RotationTest()
        {
            Debug.Log($"Initial angular speed is : {rb.angularVelocity}.");
            rb.AddTorque(50, ForceMode2D.Force);
            yield return null; // do simulation frame

            Debug.Log($"Angular acceleration is : {50}");
            Debug.Log($"Final angular speed is : {rb.angularVelocity}");
            Debug.Log($"Expected angular speed is : {50 * Time.fixedDeltaTime / rb.inertia}");
            Assert.Less(Mathf.Abs(rb.angularVelocity - 50 * Time.fixedDeltaTime / rb.inertia), 0.00001f);
        }
    }
}

Solved by this Torque and inertia in Rigidbody2D scripting manual