Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

ApplyAngularImpulse in Unity?

Discussion in '2D' started by AntonVazhinsky, Jun 19, 2020.

  1. AntonVazhinsky

    AntonVazhinsky

    Joined:
    Nov 24, 2013
    Posts:
    67
  2. Neomedus

    Neomedus

    Joined:
    May 14, 2020
    Posts:
    47
    They adapted the box2D engine for unity, that doesn't mean it's going to be the same thing. Very little code is directly shared between standalone box2D and unity's adaptation of it. In addition box2d is written in c++ and unity uses c#. If you want to find a function that does the same thing just search through the unity documentation.
     
    Last edited: Jun 20, 2020
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,544
    Here are the docs for Rigidbody2D which encapsules a b2Body.

    The method you're looking for is AddTorque. It'll be a force by default but set the ForceMode2D arg to Impulse if that's what you want.

    In-case you're interested, we simply wrap the body in as light a wrapper as possible and we expose nearly everything. The above for instance is this:
    Code (CSharp):
    1. void Rigidbody2D::AddTorque(float torque, ForceMode mode)
    2. {
    3.     ABORT_INVALID_ARG_FLOAT(torque, torque, AddTorque, Rigidbody2D);
    4.  
    5.     // Ignore if no body is available or not dynamic body.
    6.     if (m_Body == NULL || m_BodyType != kDynamicBody)
    7.         return;
    8.  
    9.     if (mode == kForceMode)
    10.         m_Body->ApplyTorque(torque, true);
    11.     else
    12.         m_Body->ApplyAngularImpulse(torque, true);
    13. }
    14.  
     
  4. AntonVazhinsky

    AntonVazhinsky

    Joined:
    Nov 24, 2013
    Posts:
    67
    Thanks, but this is not ApplyAngularImpulse. Here is the working code:
    Code (CSharp):
    1. void ApplyAngularImpulse(float impulse)
    2.     {
    3.         float InverseInertia = 1.0f / YouGameObject.GetComponent<Rigidbody2D>().inertia;
    4.         YouGameObject.GetComponent<Rigidbody2D>().angularVelocity += InverseInertia * impulse;
    5.     }
    It would be great to see this in unity as a standard feature.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,544
    I'm curious if you read my post fully or just saw that you need to use "AddTorque" and didn't read further? If you read it you'd see that you can select Impulse force-mode in which case we call ApplyAngularImpulse (as I show in the previous post) which does what your code does.

    I am not sure why you think that this somehow doesn't work or is wrong.

    It's been there since day 1. ;)

    Code (CSharp):
    1. myBody.AddTorque(myImpulse, ForceMode2D.Impulse);
     
    Last edited: Jul 4, 2020
  6. AntonVazhinsky

    AntonVazhinsky

    Joined:
    Nov 24, 2013
    Posts:
    67
    Very strange. But AddTorque rotates the object stronger than my code. I am porting the game with corona sdk and the strength value in my code is fully consistent with the rotation of the object in corona sdk. Thank.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,544
    We don't modify the value you pass as can be seen from the code I posted so it goes straight to Box2D unaltered. Any differences would be elsewhere.

    This is the code in our repository for that method from Box2D so it's stock Box2D:
    Code (CSharp):
    1. inline void b2Body::ApplyAngularImpulse(float32 impulse, bool wake)
    2. {
    3.     if (m_type != b2_dynamicBody)
    4.     {
    5.         return;
    6.     }
    7.  
    8.     if (wake && (m_flags & e_awakeFlag) == 0)
    9.     {
    10.         SetAwake(true);
    11.     }
    12.  
    13.     // Don't accumulate velocity if the body is sleeping
    14.     if (m_flags & e_awakeFlag)
    15.     {
    16.         m_angularVelocity += m_invI * impulse;
    17.     }
    18. }
    I wish I knew why you got different results from your code to the above.
     
    Last edited: Jul 9, 2020