Search Unity

Trouble with RigidBody2D add torque.

Discussion in 'Physics' started by ownezx, Aug 10, 2019.

  1. ownezx

    ownezx

    Joined:
    Mar 29, 2018
    Posts:
    10
    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
    Code (CSharp):
    1. [OneTimeSetUp]
    2. public void Init()
    3. {
    4.     //Init runs once before running test cases.
    5.     go = new GameObject();
    6.     rb = go.AddComponent<Rigidbody2D>();
    7.     // Don't forget this, it seems like gravity is not the same as the
    8.     // Project settings.
    9.     rb.gravityScale = 0;
    10.     rb.angularDrag = 0;
    11. }
    12.  
    13.  
    14. [SetUp]
    15. public void SetUp()
    16. {
    17.     //SetUp runs before all test cases
    18.     rb.velocity = Vector2.zero;
    19.     rb.angularVelocity = 0;
    20.     rb.position = Vector2.zero;
    21.     rb.rotation = 0;
    22.  
    23. }

    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 :
    Code (CSharp):
    1. [UnityTest]
    2. public IEnumerator RotationTest2()
    3. {
    4. Debug.Log($"Initial angular speed is : {rb.angularVelocity}.");
    5. rb.AddTorque(50, ForceMode2D.Force);
    6. yield return null; // do simulation frame
    7.  
    8. Debug.Log($"Angular acceleration is : {50}.");
    9. Debug.Log($"Final angular speed is : {rb.angularVelocity}.");
    10. Debug.Log($"Expected angular speed is : {50 * Time.fixedDeltaTime / rb.inertia}.");
    11. Assert.Less(Mathf.Abs(rb.angularVelocity - 50 * Time.fixedDeltaTime / rb.inertia)), 0.00001f);
    12. }
    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.
     
    Last edited: Aug 16, 2019
  2. ownezx

    ownezx

    Joined:
    Mar 29, 2018
    Posts:
    10
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using NUnit.Framework;
    4. using UnityEngine;
    5. using UnityEngine.TestTools;
    6.  
    7. /// <summary>
    8. /// Testing the unity engine Physics2D
    9. /// </summary>
    10. namespace Tests
    11. {
    12.     public class RigidBodyTest
    13.     {
    14.         /// <summary>
    15.         /// Game object on which we test the engine
    16.         /// </summary>
    17.         GameObject go;
    18.         /// <summary>
    19.         /// Rigid body 2D instance to test the engine with
    20.         /// </summary>
    21.         Rigidbody2D rb;
    22.  
    23.  
    24.         [OneTimeSetUp]
    25.         public void Init()
    26.         {
    27.             //Init runs once before running test cases.
    28.             go = new GameObject();
    29.             rb = go.AddComponent<Rigidbody2D>();
    30.             // Don't forget this, it seems like gravity is not the same as the
    31.             // Project settings.
    32.             rb.gravityScale = 0;
    33.             rb.angularDrag = 0;
    34.         }
    35.  
    36.  
    37.         [SetUp]
    38.         public void SetUp()
    39.         {
    40.             //SetUp runs before all test cases
    41.             rb.velocity = Vector2.zero;
    42.             rb.angularVelocity = 0;
    43.             rb.position = Vector2.zero;
    44.             rb.rotation = 0;
    45.  
    46.         }
    47.  
    48.         [UnityTest]
    49.         public IEnumerator RotationTest()
    50.         {
    51.             Debug.Log($"Initial angular speed is : {rb.angularVelocity}.");
    52.             rb.AddTorque(50, ForceMode2D.Force);
    53.             yield return null; // do simulation frame
    54.  
    55.             Debug.Log($"Angular acceleration is : {50}");
    56.             Debug.Log($"Final angular speed is : {rb.angularVelocity}");
    57.             Debug.Log($"Expected angular speed is : {50 * Time.fixedDeltaTime / rb.inertia}");
    58.             Assert.Less(Mathf.Abs(rb.angularVelocity - 50 * Time.fixedDeltaTime / rb.inertia), 0.00001f);
    59.         }
    60.     }
    61. }
    62.  
     
  3. ownezx

    ownezx

    Joined:
    Mar 29, 2018
    Posts:
    10