Search Unity

Mathematical Way to Calculate Angular Acceleration (Without rigidbody)?

Discussion in 'General Discussion' started by AhSai, Jun 25, 2019.

  1. AhSai

    AhSai

    Joined:
    Jun 25, 2017
    Posts:
    129
    I am trying to write a script that manually calculates the torques of an object, then calculate its angular acceleration and angular velocity, then apply the appropriate rotation to it without using Rigidbody (for various reasons). The physics part seems to be correct, however, the angular acceleration and angular velocity seems cannot be done by simply doing this:
    Code (CSharp):
    1. angularVelocity += angularAcceleration * Time.deltaTime;
    2. transform.Rotate(angularVelocity * Mathf.Rad2Deg * Time.deltaTime, Space.World);
    The rotation is slightly off compared to using a rigidbody.
    I have attached a sample scene showing two objects with similar swinging motion. The one on the left is calculated using math, and the one on the right uses Rigidbody. You can see that the angle of the swing on the left will be off over time compared to the one on the right. Does anyone know the correct way of adding up angular velocity?
     

    Attached Files:

  2. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,025
    I haven't looked at your example scene, but when you're working with changing acceleration, it will matter a lot whether you are doing it in Update or FixedUpdate. FixedUpdate sometimes operates more than once per frame, so if your acceleration changes, it will apply multiple different accelerations (each for a fraction of the frame interval) during the frame interval, rather than the one acceleration for the entire frame interval in Update.
     
  3. AhSai

    AhSai

    Joined:
    Jun 25, 2017
    Posts:
    129
    I have tried both Update and FixedUpdate, it doesn't seem like that is that causing the problem. I believe the problem is that you simply cannot just add up the rotation as an Euler.
     
  4. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,025
    OK I couldn't resist, so I took a look at the scene.

    1. When you need to debug physics, always calculate the correct value mathematically (even if you need to reduce the problem to a small testable fraction of what you're doing).

    2. This pendulum calculator calculates the period (swinging to and fro) of your setup to be just over 2.8 seconds, with a mass of 1 and a length of 1.414 meters. If you freeze your scene at 2.8 seconds, you'll see that your non-physics pendulum is actually correct.

    3. So since the physics pendulum is faster, I figured it must be something to do with lack of inertia, since this is the only negative force in your non physics setup.

    4. Your inertia calculation takes into account the distance of the mass from the swing center, whereas the physics one treats the mass as a point mass existing at the swing center (rigidbody position). I'd say this is the issue.
     
  5. AhSai

    AhSai

    Joined:
    Jun 25, 2017
    Posts:
    129
    For the math part, i think the non physics pendulum is actually correct. However, the problem occurs when I add the angular acceleration to the angular velocity. It seems like the angle will be slightly off when I increment them with * deltaTime like linear velocity. If you run the sample scene for about 30 seconds, you will start to see that the non physics one is no longer swinging diagonally, whereas the physics one is still swinging perfectly in its diagonal. So I would say that the math part in the non physics one is correct, but the angle part is not. And the angle part of the physics one is correct, but the speed is wrong (probably due to point 4 as you stated above, which is one of the reasons why I don't use rigidbody for this). So my question is how can I correct the non physics one so that it produces the correct angle?
     
  6. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,025
    Ah ok that could have been a bit clearer in the first post.

    Yes it's not going to work because the rotations are applied one after the other on I believe Z, X and then Y axes, so preceding rotations will affect later ones in all sorts of weird ways. I suggest you rotate around local axis or get used to using quaternions, which don't actually need to be fully understood to be used practically.
     
    AhSai likes this.
  7. AhSai

    AhSai

    Joined:
    Jun 25, 2017
    Posts:
    129
    You are right, after reading the documentation of Transform.Rotate again, it does mention that rotating using Euler does rotate each axis one after another which will produce a different result. Passing in an axis and angle in the parameter seems to solve the problem. Thank you very much.
     
    Billy4184 likes this.