+ Reply to Thread
Page 1 of 7 1 2 3 ... LastLast
Results 1 to 10 of 63

Thread: applyTorque Tutorial

  1. #1
    Wire Sofaking

    Fizyk will become famous soon enough Fizyk will become famous soon enough Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    729
    Blog Entries
    1

    Default applyTorque Tutorial

    I see people have problems with understanding, how applyTorque works and how to use it, so I decided to make this tutorial with short explanation of these things.

    What is torque?

    Torque is a physical quantity that tells us how a force will rotate an object. When a force is applied to an object and its origin is not the mass center of the object, usually it will rotate this object. Torque means about which axis the object will rotate, in which direction, and how powerful the rotating force is, see this picture:



    To tell in which direction given torque will rotate the object, you can use the right-hand rule. If you orient your right hand in such a way, that your thumb points in the direction of the torque vector, then the rest of your fingers show the direction of movement.



    It is really convenient to think of torque like of a force, but for rotational movement. Just like force and acceleration are connected in linear movement by the formula F = m*a, torque and angular acceleration are connected by the formula:

    tau = I*epsilon

    where tau is the torque, epsilon is the angular acceleration (a quantity that tells you how fast the angular velocity changes), and I is the object's inertia. Inertia tells you how the object "resists" to torque - the bigger the inertia, the greater torque is needed to achieve the same angular acceleration, just like you need more force to accelerate a heavier object in the same way as a lighter object.

    There is one subtlety though. The mass is always one number, inertia in the general case is a 3x3 matrix. It can always be expressed by 3 numbers though, which are called the principal moments of inertia, which tell you the inertia for rotating about 3 principal axes - which in GMod are just the object's X, Y and Z local axes. The E:inertia() function returns the principal moments of inertia in a vector (it's in some weird units though, so be careful when trying to use it for some more precise formulas - see Tolyzor's GMod physics guide).

    What can it be useful for?

    There are a lot of possible applications, I'll present two of them. A simple one - how to make a wheel, and a more complex one - partial angular stabilization without applyAngForce.

    Simple example - a wheel



    Ok, we have a wheel prop and we want it to rotate like a wheel. The image shows the desired axis of rotation (light grey), the desired direction of rotation (dark grey) and torque needed to get these results (red). Let's say the direction in which torque vector points is the wheel's up direction.

    Torque works in local coordinates, so we would need something like:
    Code:
    Torque = some_number  #amount of torque - for example 100
    Wheel:applyTorque(Wheel:toLocalAxis(Torque*Wheel:up()))
    
    But this can be simplified a lot. Up direction of a prop is it's local Z axis, so Wheel:toLocalAxis(Torque*Wheel:up()) is the same, as vec(0,0,Torque):
    Code:
    Torque = some_number  #amount of torque - for example 100
    Wheel:applyTorque(vec(0,0,Torque))
    
    That's all! The wheel will spin as we wanted it to. But what if we want it to spin in the opposite direction? Well, we just apply the torque in the opposite direction:
    Code:
    Wheel:applyTorque(-vec(0,0,Torque))   #or vec(0,0,-Torque)
    
    Ok, now something more advanced.

    Advanced example - partial stabilization

    Well, let's say you have a prop (E:entity) and you want it to face certain direction. Let's say you have target direction TarDir and you want prop's forward vector to point in that direction. That's why I called it partial stabilization too - it only fixes one direction, so the prop will still be able to rotate about one axis.

    First, since applyTorque works in local coordinate system, you need to convert desired direction from global to local. Since it is a directional vector, not a position, we will use toLocalAxis:
    Code:
    TarDirL = E:toLocalAxis(TarDir)
    
    Ok, now you have two vectors: TarDirL and vec(1,0,0) (it's prop's forward vector in local coordinates). Now you want to apply such torque, that will rotate the prop so that its forward direction will point in the same direction as TarDirL. See picture:



    Fortunately, there is a function, that takes two vectors and gives a vector perpendicular to both of them. It's the cross product. We want the torque to be proportional to the angle between the two vectors. To get this angle, we will use dot product:

    Code:
    Angle = acos(TarDirL:dot(vec(1,0,0))/TarDirL:length())
    
    In our case, this can be simplified to:

    Code:
    Angle = acos(TarDirL:x()/TarDirL:length())
    
    But it's not always true. The general formula is Angle = acos(V1:dot(V2)/(V1:length()*V2:length())).

    So, the torque we need, will be:
    Code:
    Angle = acos(TarDirL:x()/TarDirL:length())
    Torque = vec(1,0,0):cross(TarDirL):normalized()*Angle*20*E:inertia()
    
    To get rotation in desired direction we need to get cross product of (current direction X target direction) - do it the other way round, and it will rotate to face the exactly opposite direction. We also normalize it, so that its length is 1, and then multiply by the angle - this way we get a vector perpendicular to both current and target directions, with its length equal to the angle between these vectors.

    I also multiplied the torque by inertia, because it is rotational equivalent of mass, and by 20 to make it a bit stronger anyway. If it goes too slow, try increasing this number, if it spazzes - decrease it.

    Ok, but if we calculate force for position stabilization, we use roughly Force = TargetPos - Pos - $Pos. The equation I gave is rotational equivalent to Force = TargetPos - Pos. What about the delta term?

    Well, we can use angular velocity as the delta term. The function that gives it is E:angVelVector(). So, our torque will look like this:
    Code:
    Torque = (vec(1,0,0):cross(TarDirL):normalized()*Angle*20 - E:angVelVector()*2)*E:inertia()
    
    Again, 2 is an arbitrary coefficient that can be adjusted to get the best effects.

    Now, the final line of code:
    Code:
    E:applyTorque(Torque)
    
    Ta-dah! That's how you can make a prop face target direction.

    Note: Beware, that this code has conditions only for one vector, that is, the forward vector. There are no conditions for up and right vectors, so the prop can still rotate about its forward axis (it's only partial stabilization)! It will hold still because of the angVelVector() term, but its up and right vectors can face any directions when it stops. If you want them to point in some particular direction, just add another piece of code that will take care of one of those vectors.

    For rotational calculations, there is also a very useful tool in E2 - quaternions. Check my sig for a tutorial about them. The tutorial also involves an example combining quaternions and applyTorque.

    I hope I made the torque thing a bit clearer
    Attached Images
    Last edited by Fizyk; 07-15-2010 at 12:48 PM. Reason: Improved the tutorial a bit

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  2. #2
    I think I think too much

    -HP- has a spectacular aura about -HP- has a spectacular aura about -HP- has a spectacular aura about -HP-'s Avatar
    Join Date
    Feb 2009
    Location
    Behind you with a very sharp knife.
    Posts
    2,239

    Default Re: applyTorque Tutorial

    Thanks Now I know how to use it, but not why the hell it works like this (wtf is cross)

    if you sign up using my link you get extra space!

    also this

  3. #3
    Expressionism 2.0

    Syranide has disabled reputation Syranide's Avatar
    Join Date
    Mar 2007
    Location
    Sweden
    Posts
    4,504

    Default Re: applyTorque Tutorial

    In my opinion, a reference to wheels wouldn't hurt. It personally helps when I visualize torque as it is for wheels on cars, that they sit on an axis which spins in either direction. Contrary to the free/relative rotation offered by applyAngForce.

    Which is another thing, applyTorque can probably be used to rather easily achieve better wheels in wiremod, with traction control to prevent them slipping at low speeds while still allowing them to put out high forces.

  4. #4
    Wirererer Generic Default is on a distinguished road Generic Default's Avatar
    Join Date
    Jul 2008
    Location
    Jalalabad, Afghanistan
    Posts
    315

    Default Re: applyTorque Tutorial

    It looks kind of confusing. What advantages does it have over applyAngForce?

  5. #5
    Wire Sofaking mjmr89 is on a distinguished road mjmr89's Avatar
    Join Date
    Mar 2008
    Posts
    559

    Default Re: applyTorque Tutorial

    Thanks! This helped a lot!

    Generic: It is immune to Gimbal lock.

  6. #6
    Wire Sofaking nescalona is on a distinguished road nescalona's Avatar
    Join Date
    Apr 2007
    Location
    Shoreline, Washington
    Posts
    1,335

    Default Re: applyTorque Tutorial

    Heh, I was working on something just like this to explain applyTorque to everyone since it seemed few were using it. You beat me :P

  7. #7
    Banned Nicolai1 will become famous soon enough Nicolai1's Avatar
    Join Date
    Nov 2008
    Location
    Denmark.
    Posts
    1,303

    Default Re: applyTorque Tutorial

    We soon have tutorials for everything D:
    People are almost never finding out something on their own D:
    Especially a lot of people in-game ask "How duz i surch hur" or "hao duz i doz dat".
    They always need tutorials

  8. #8
    Drone Madman
    Lyinginbedmon has a spectacular aura about Lyinginbedmon has a spectacular aura about Lyinginbedmon's Avatar
    Join Date
    Mar 2009
    Location
    England
    Posts
    2,651

    Default Re: applyTorque Tutorial

    Quote Originally Posted by Nicolai1 View Post
    Especially a lot of people in-game ask "How duz i surch hur" or "hao duz i doz dat".(
    No-one ever says this. We have evidence.

  9. #9
    Banned Nicolai1 will become famous soon enough Nicolai1's Avatar
    Join Date
    Nov 2008
    Location
    Denmark.
    Posts
    1,303

    Default Re: applyTorque Tutorial

    I just said it D:

  10. #10
    Wirezard

    Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte's Avatar
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    3,151

    Default Re: applyTorque Tutorial

    This is great!

    Good job!
    "If anybody says he can think about quantum physics without getting giddy, that only shows he has not understood the first thing about them."
    -- Niels Bohr


    Wire FPGA

+ Reply to Thread
Page 1 of 7 1 2 3 ... LastLast

Similar Threads

  1. Help with applyTorque
    By AndyDLP in forum Technical Support
    Replies: 13
    Last Post: 11-15-2009, 11:55 PM
  2. Keep upright with applyTorque() function
    By OmicroN in forum Finished contraptions
    Replies: 2
    Last Post: 07-30-2009, 08:24 AM
  3. E2 Tutorial
    By goluch in forum Wiremod Tutorials
    Replies: 8
    Last Post: 05-28-2009, 01:20 PM
  4. Pac's E2 Tutorial
    By Nicolai1 in forum Wiremod Tutorials
    Replies: 27
    Last Post: 04-12-2009, 07:02 AM
  5. Putting a video tutorial in the gmod tutorial list
    By Def the world in forum Technical Support
    Replies: 1
    Last Post: 08-21-2007, 09:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots