+ Reply to Thread
Page 1 of 6
1 2 3 ... LastLast
Results 1 to 10 of 51

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
    589

    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:



    When using applyTorque, you don't need to worry about the rotating forces. The function will automatically apply appropriate forces, so that the resulting torque will be equal to the one you want.

    What can it be useful for?

    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.

    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, simple toLocal won't do, you need to use:
    Code:
    TarDirL = E:toLocal(TarDir + E:pos())
    
    (Why it's like this is a thing for another tutorial.)

    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. Yep, you guessed it - it's cross product. What's more, cross product of two vectors is 0 when they are parallel, so it is a very good measurement of distance between two directional vectors.

    So, the torque we need, will be:
    Code:
    Torque = vec(1,0,0):cross(TarDirL)*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.

    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)*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)
    
    Tadam! 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 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.

    I hope I made the torque thing a bit clearer
    Attached Images

  2. #2
    no brain, no headache

    -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
    1,229

    Default Re: applyTorque Tutorial

    Thanks Now I know how to use it, but not why the hell it works like this (wtf is cross)
    sry 4 my bad spelign, but enligh is my 2 langage



  3. #3
    Expressionism 2.0

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

    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
    296

    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
    502

    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
    922

    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
    I am smart! I make it go!

    Steam name: taio
    Quote Originally Posted by SpectreCat View Post
    Why are we using an E2 to make Light = Button

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

    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 will become famous soon enough Lyinginbedmon will become famous soon enough Lyinginbedmon's Avatar
    Join Date
    Mar 2009
    Location
    England
    Posts
    2,040

    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,002

    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's Avatar
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    2,102

    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


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

Similar Threads

  1. Help with applyTorque
    By AndyDLP in forum Help & Support
    Replies: 13
    Last Post: 11-15-2009, 11:55 PM
  2. Keep upright with applyTorque() function
    By OmicroN in forum Custom & Advanced Gates
    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 Help & 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