Search Unity

Calculating resulting torque form forces around the rigidbody

Discussion in 'Physics' started by Gibbonuk, Oct 11, 2019.

  1. Gibbonuk

    Gibbonuk

    Joined:
    Dec 10, 2013
    Posts:
    175
    Hi, am porting a simulator project of mine to unity and cant seem to work out how to calculate torque to apply to the RB.

    So in a loop I am calculating various forces up from around the rigidbody at different positions ending with a resulting overall force.

    I want to do the same for torque, so for each force in the loop, calculate the resulting torque from that force on the RB.

    Anyone know how I go about this?

    My current method which im not sure about... This is in a loop i

    Code (CSharp):
    1.             Vector3 local_final_force = transform.InverseTransformVector(finalForce);
    2.             Vector3 force_pos_localspace = forcePos[i].transform.localPosition;
    3.             Vector3 final_torque = Vector3.Cross(force_pos_localspace, local_final_force);
    4.             //Vector3 local_final_torque = transform.InverseTransformVector(final_torque);
    5.  
    6.             current_force.torque = final_torque;
    I then add all these up late and apply them using RB.AddTorque but im not convinced its correct, its certainly not behaving like it should.

    Thanks
     
    Last edited: Oct 11, 2019
  2. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Do it all in world coordinates, except for force position where you'd use world force position - world object position. Then world torque is just the cross product of that "position relative to center of mass in world coordinates" and the world force vector. Then after you've accumulated all the torques, feed that total world torque vector into addTorque once and call it a day. I do that in my boat simulator and it's super fast with thousands of torques all added with a single AddTorque call.