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:
(Why it's like this is a thing for another tutorial.)Code:TarDirL = E:toLocal(TarDir + E:pos())
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:
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.Code:Torque = vec(1,0,0):cross(TarDirL)*20*E:inertia()
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:
Again, 2 is an arbitrary coefficient that can be adjusted to get the best effects.Code:Torque = (vec(1,0,0):cross(TarDirL)*20 - E:angVelVector()*2)*E:inertia()
Now, the final line of code:
Tadam! That's how you can make a prop face target direction.Code:E:applyTorque(Torque)
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![]()


LinkBack URL
About LinkBacks









Reply With Quote


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







Bookmarks