Search Unity

Mass, Drag and Torque

Discussion in 'Scripting' started by SticksStones, Apr 15, 2012.

  1. SticksStones

    SticksStones

    Joined:
    Jan 3, 2012
    Posts:
    17
    I'm having trouble understanding what values and equations hide behind how Mass, Drag and Torque function in Unity, and how they relate to one another.

    If I have a GameObject in zero-g and want to apply a single blast of torque to spin it 180, with a AngularDrag of 1 and a Mass of 1. How could I figure how much torque to apply? (- trial and error got me a value of 48.5, but why that value?)


    I don't really understand what AngularDrag = 1 means. Does the scale go from 0 to 100?

    On mass, the Unity scripting reference say's "You should strive to keep mass close to 0.1 and never more than 10. Large masses make physics simulation unstable." - are those values purely abstract?

    Thanks for any help
     
  2. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    I have no idea, but I think that's probably pretty complicated to calculate. Is there some particular reason why it must be turned by physics?
    I couldn't tell you how, but I'm sure it'd be a lot easier if you could just rotate the Transform.
     
  3. SticksStones

    SticksStones

    Joined:
    Jan 3, 2012
    Posts:
    17
    Tobias, from looking around on the forums your answer seems to be the same that everybody gets when they've asked a question like mine....it seems really weird to me that nobody seems to actually understand the math behind the physics.

    I'm just trying to make some AI spaceships fly around, chasing and shooting at each other. I could use transform rotate, but what happens when they collide? I don't want to simply destroy them, so I would then have to write my own collision detection and response. I've done that before, but it's not much fun. My math isn't too great.
    I don't have any level geometry, just GameObjects. Physics is the obvious approach.

    PHP:
    void Start () {
    rigidbody.mass 1;
    rigidbody.angularDrag 1;
    rigidbody.AddRelativeTorque (Vector3.up 48.5f);
    }
    I'm wrong above, 48.5 is approximately 360 degrees.
    (48.5 / 2) is 180 degrees, (48.5 / 4) is 90, which is interesting.

    48.5 with a mass of 2 is 180 degrees - so double the mass, half the displacement, also interesting.

    Not quite sure how drag works though, but I guess if the drag is the same on everything, I probably have something workable even if I don't really understand it.
     
  4. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    huh? err. no. You don't have to do anything other than provide a rigidBody to your gameObject, and all collisions are taken care of for you.

    Or am I still not comprehending the problem?
     
  5. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    I think what SticksStones is asking is: What is the algorithm that drives the physics engine in unity?

    If you knew the math behind the physics engine, you could programatically apply the required torque / velocity required to move an object a specific distance or rotation.

    I'm sure I'm not alone when I say that it would be nice to see some formula representing what goes on in the physics engine, unfortunately I don't believe it would be in UT's interests to release that information (we could all go and make our own physics engines based on that, and Unity would lose out on a USP).

    Still... It would be nice ;).
     
  6. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    I think nearly all the physics formulas themselves have probably been in the 'public domain' since the time of Newton. Probably wikipedia has all that you need.
    I think it's the implementation of the formulas they need to keep secret.

    Whether the engine exposes all the values you need, I have no idea.
     
  7. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    In 90% of the cases you won't need to calculate such stuff. Actually the whole point of having a physic engine is not to calculate it yourself, but let the engine do it and when you do something like that, it's mostly a sign that something wrong with your implementation/attempt
     
  8. maawaa

    maawaa

    Joined:
    Apr 1, 2012
    Posts:
    16
    drag only effects the falling speed within gravity. Think of it as Unity operating in complete vacuum at 0 drag, so two items with a mass of 1 and 100 will fall at the same speed. Set the mass 1 item to a drag of 1 and it falls 10 times slower, thus simulating air drag (full atmosphere). thats how i look at it anyway.

    Im interested in how angular drag works too as i have had some problems with it myself.

    I set an object at mass of 1, apply some torque and it spins, change the mass to 2 and its rock solid and doesnt spin, but if i move it forwards and try spinning, then it spins as the rigidbody has less static pressure on it (or something?)

    Its close to my desired outcome where i want an item to spin slowly while stationary but be able to rotate faster under locomotion to convey the feeling of a hefty mass (in this case a Tank), im having problems fine tuning it though and just feels like guess work at the moment.
     
  9. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    No, even if you set gravity to 0 you have a drag. Drag is independent of gravity. Drag is like... a constant for "air resistance". It slows down your object down.

    This has two effects:
    a) when using an impulse or velocity, your object will eventually stop moving (no matter if gravity is enabled or not). Theoretically even in outer space you have drag, because the outer space is not empty but has some atoms/particles which will eventually slow down an object, given enough time
    b) if you use constant force/acceleration, the drag will limit the max speed as at some point the drag/"air resistance" becomes so huge that an object can't be accelerated anymore

    Normally, calculating the drag is complex, as it depends on the surface of the object and other factors. But doing this in a game, which needs do do all calculations in real time is overkill, so the drag gets very simplified to a constant for performance reasons.

    There are 4 kinds of "forces" you can apply with "Rigidbody.AddForce" (it has an overload with 2 parameters.

    http://unity3d.com/support/documentation/ScriptReference/ForceMode.html

    Force Add a continuous force to the rigidbody, using its mass.
    Acceleration Add a continuous acceleration to the rigidbody, ignoring its mass.
    Impulse Add an instant force impulse to the rigidbody, using its mass.
    VelocityChange Add an instant velocity change to the rigidbody, ignoring its mass.

    As you see, if you want a certain velocity or acceleration of an object, use
    Code (csharp):
    1.  
    2. rigidbody.AddForce(Vector3.forward * 5, ForceMode.Acceleration);
    3.  
    for example. This accelerates the object, independent of it's mass. An object with a mass of 1 will be accelerated exactly same as an object with mass 10 (P.S. Don't use mass higher than 10, this will cause in accuracies in the physic calculations. The mass is relative anyway).

    But the difference is, the object with mass 10 will have a much higher force on impact than the 1 mass one will.

    That's because force depends on mass. If you have a ball of 1 kg and throw it, it may land 2m away. If you have a ball of 5 kilos it won't get as far with the same force.

    Just use different force modes. Instead of adding force, accelerate it or change its velocity.

    Physic is overrated in most things and especially PhsyX is not very well know to be a accurate physics engine. It's enough for certain things (i.e. an exploding char), but if you want to simulate real world stuff (i.e. controlling an air plane or car via physics) it will fail. That's not what physic was meant for. Most games do that via scripting, as its much more efficient performance wise
     
  10. maawaa

    maawaa

    Joined:
    Apr 1, 2012
    Posts:
    16
    That was pretty much what i said, drag = air resistance :p, except the "within gravity" part was incorrect, thank you for the correction.

    im already using force.accelerate but it still doesnt spin if i increase the mass, thats why i am getting all :confused:. I must have done something wrong somewhere, at least i know what i attempted is correct, just the implementation is wrong.

    thats what im trying to achieve! thank you for your comments, i can go try it again in a simpler techtest format knowing im at least going down the right lines.

    cheers!
     
  11. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    Neither the ForceMode nor the mass will affect spin by themselves. When using AddForce() you apply the force at the center of the object, thus no inherent spin is added (that is added through friction with other objects).

    If you want the force itself to add spin, use AddForceAtPosition()
     
  12. maawaa

    maawaa

    Joined:
    Apr 1, 2012
    Posts:
    16
    I think i have misunderstood some of the documentation.

    http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddTorque.html

    I have been attempting to use torque to spin my rigid body around its axis.

    Code (csharp):
    1.  
    2. Vector3 targetTorque = transform.TransformDirection(Vector3.up) * Input.GetAxis("Horizontal") * turningSpeed;
    3. rigidbody.AddTorque(targetTorque, ForceMode.Acceleration);
    4.  
     
  13. SticksStones

    SticksStones

    Joined:
    Jan 3, 2012
    Posts:
    17
    Exactly! Couldn't put it better myself!

    If I knew the algorithm, I can predict what will happen in the future, provided there isn't a collision. In this instance, I need to program my AI so that it will know when it's rotating too fast and will overshoot it's target direction. I then need it to know how long to fire it's retro engines in the opposition direction to stop the rotation so that it's pointing at it's target.

    I'm still surprised nobody has popped up with the formula, but I think I should be able to figure it out.

    Thanks timsk and also Tseng for some useful info.
     
  14. SticksStones

    SticksStones

    Joined:
    Jan 3, 2012
    Posts:
    17
    This is a working algorithm for a cube in 2D space...

    AngularVelocity = (((Power / 50) * 6) / (scale.x * size.x * scale.z * size.z)) / Mass

    ...but it doesn't work for a rectangle.


    PHP:
    public float power 50f;

    void Start () {        
        
    rigidbody.mass 1;
        
    rigidbody.angularDrag 0;
        
    rigidbody.AddRelativeTorque (Vector3.up power);
    }

    void Update () {
        Print(
    "Angular Velocity "+(rigidbody.angularVelocity.magnitude));
    }
    I'm so close, but running out of ideas. Anybody?
     
  15. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    I just want to add that another reason to know the algorithms inside the physics engine is so you could write AI that anticipates where a physical rigid body will be over time. It's easy with drag at 0 and no collisions, but it would be nice to know what's going on under the hood when physics get complex.
     
  16. Occulus

    Occulus

    Joined:
    Apr 27, 2013
    Posts:
    14
    Actually, if I understand correctly... it looks like Unity 3D uses the PhysX library for their physics engine. See under Documentation/legal.txt. The SDK is free for download. It takes advantage of your on-board GPU to run the calculations... if it can.

    For your physics question. I'm not sure what units they use... but if one has to apply 50,000 (mystery-units) to get a mass of 100 to jump in a gravity well of 10. Well, this seems to correspond directly to SI units.

    Drag corresponds to a force which is proportional to your velocity and opposite your direction of motion. This means that as long as you are moving, you are slowing down.

    There are, in fact, multiple different types of drag.

    The general equation for drag can be thought of as:

    F_drag = A + B*v + C*v^2 + D*v^3 + (Higher order junk)

    More than likely, they are not using this long, weighty and painful series expansion to represent drag. They are probably using a simple model like:

    F_drag = B*v

    Where B is the constant that you change, think of it as a weight for the drag effect. This expression isn't nearly as painful to work with. Less painful, faster to compute, it's a win-win. All of this is for linear velocity, what about angular velocity?


    Well, it turns out that angular velocity is pretty much a direct analogy with linear velocity. An angular drag would be a torque that an object feels which is directly proportional to the angular velocity, but opposite in direction.

    Angular drag will take an object that is spinning, and it will make it slower and slower as time goes on until eventually it stops.

    Torque_drag = B*angular_velocity


    Why don't we ever talk about this in beginning level physics? Mostly because to describe it completely would require differential equations. Most students don't like to apply algebra, much less differential equations... so, we don't talk about it in early physics classes.

    I will add that computers make these calculations trivial. :) :) :)
     
    Last edited: Apr 30, 2013
  17. Occulus

    Occulus

    Joined:
    Apr 27, 2013
    Posts:
    14
    I feel obligated to add...


    What causes drag?

    Moving through a fluid... any fluid! In physics and in engineering, an object moving through water/air/molasses/quicksand is treated like the object is moving through a fluid.



    What causes angular drag?

    Spinning in a fluid, like air/water/molasses/quicksand. It's a real thing!
     
  18. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    OMG the only place I found these informations, THANK YOU.
     
  19. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,929
    You need to take the inertia tensor into account. In 3D, this is usually a 3x3 matrix (Unity expresses it as a diagonal matrix and a basis rotation quaternion) that describes the mass distribution of the body. A square and a rectangle have completely different moments of inertia, so that's why your formula will only work for squares with the center of mass at their geometrical center.

    Roughly speaking, it is to angular velocity what mass is to linear velocity:
    https://en.wikipedia.org/wiki/Moment_of_inertia