# Copyright 6 Vista 7 new! Languages 8 Glovepie scripts 9

 Page 22/30 Date conversion 08.07.2018 Size 0.65 Mb.

## Rotation

Note that rotation requires at least 3 LEDs to be visible and accurate before it can be calculated. Otherwise the rotation will stay much the same as it was before.

### Directions of Parts of the Hand

The recommended way to get the glove’s orientation in GlovePIE is to use the values that tell you which way the parts of the glove are facing.

For example:
debug = P5.FingersForward
Or if you want to be more precise:
debug = P5.FingersForward and P5.PalmDown
You need to specify two values to uniquely identify an orientation.
IMPORTANT: These do NOT refer to the finger bends. The fingers are assumed to be straight and the thumb is assumed to be sticking out the side of your hand at right angles, for the purposes of these rotation values. The finger bends make no difference to these values.
You can check the direction of the following parts:
BackOfHand, Fingers, Palm, Thumb, or Wrist
And you can check for one of the following directions:
Forwards, Backwards, Left, Right, Up, Down
The part will always be pointing in one, and only one, of those 6 directions. It can be pointing in that direction +/- 45 degrees in order to count as that direction.
Note that a Left handed glove the thumb is on the other side. So for example:
debug = P5.FingersForward and P5.ThumbRight

### Diagonal Directions

You can also check for the diagonal directions:

DownBackLeft, DownBack, DownBackRight,

DownLeft, DownRight

DownForwardsLeft, DownForwards, DownForwardsRight

BackLeft, BackRight

ForwardsLeft, ForwardsRight

UpBackLeft, UpBack, UpBackRight

UpLeft, UpRight

UpForwardsLeft, UpForwards, UpForwardsRight.

But these directions overlap with each other, and the non-diagonal directions above. For example the Fingers might register as both DownBackLeft and DownBack at the same time.
It needs to be within +/- 45 degrees of that angle for it to be true.

### Vague Directions

If you don’t want to be that precise, for example, you only want to know whether the palm is facing up or down, you can use the vague directions.

For example:
debug = P5.PalmUpVague
Will be true whenever the Palm is closer to being up than it is to being down. Either PalmUpVague or PalmDownVague will always be true. Even if the palm is facing left or forwards.
In other words, the angle only has to be +/- 90 degrees.
Vague also works for diagonals

### Strict Directions

If you want to be even more precise than normal, you can use the strict directions. For example if you want to know if the palm is facing directly up, but not facing diagonally up and to the left, then you can use the Strict version.

eg.
Debug = P5.PalmUpStrict.
This also works with diagonals (which is the whole point).
The angle must be within 22.5 degrees.

### Angle from Directions

You can also tell how far the part of your hand is from pointing in that direction. For example, how close are your fingers to pointing forwards?

You can do comparisons like this:

Enter = P5.FingersAngleFromForward <= 30 degrees

That will hold down the Enter key whenever the fingers are within 30 degrees of pointing forwards.

### Direction Vectors and Matrix

The other way of finding which direction the parts of the hand are pointing is to use the following direction vectors:

P5.FingersDir

P5.ThumbDir

P5.PalmDir

P5.WristDir

P5.BackOfHandDir
These are vectors, which means they have an x, y, and z component like this:
[1, 0, 0]
The total length will equal 1.
The first component is the x component. It says how much of it is pointing in a right/left direction. In this example it is 1 which means it is pointing all the way to the right. The second component is the y component, which says how much of it is pointing in an up/down direction. In this case none, because it is pointing exactly to the right. The third component is the z component, how much is pointing forwards/backwards, in this case none.
There is also a rotation matrix, which is just three rows of direction vectors like the ones above.
The third row of the rotation matrix is the fingers direction. The second row is the back of hand direction. And the first row of the rotation matrix is the right hand side of your hand direction (which isn’t mentioned above).
You can read the components of the rotation matrix individually, by specifying the row first and then the column:
debug = p5.mat31

In the above example it will use the fingers direction (row three), and get the x component of it. In other words, how much are the fingers pointing to the right?

debug = p5.mat22
The above example would get the back-of-hand direction (row two) and get the y component of it. In other words, how much is the back of the hand pointing up?
You can also get the entire matrix as a 3x3 matrix, by using P5.RotMat.
You can also access the unfiltered rotation matrix with P5.UnfilteredRotMat for the whole matrix, or P5.UnfilteredRotMat11 to P5.UnfilteredRotMat33.

### Using Trigonometry to Convert Them to Angles (Isn’t as hard as you might think)

If you understand a very tiny bit of trigonometry then you can convert those numbers into an angle. For example, if we want to know how much the fingers are pointing up, that would be p5.mat32. But that is a fraction, between 0 and 1, or 0 and -1. To convert a fraction to an angle, you use the inverse of the sin, cos, and tan functions. The inverse is called ArcSin, ArcCos, and ArcTan in GlovePIE (But in C they are shortened to asin, acos and atan).

ArcSin converts 0 to 0 degrees, and converts 1 to 90 degrees.

ArcCos does the opposite, and converts 0 to 90 degrees and converts 1 to 0 degrees.

ArcTan converts 0 to 0 degrees and converts 1 to 45 degrees.
Note that you have to use these functions, rather than just multiplying by 90, because it is not linear. Sin(45 degrees) is actually about 0.7 not 0.5.
Let’s say we want an angle which says how much the fingers are angled upwards. We want 1 (completely up) to be 90 degrees, so we choose ArcSin.
debug = ArcSin(p5.mat32) // a half-circle kind of pitch
You could also write it like this:
debug = ArcSin(p5.FingersDir[2]) // the same half-circle kind of pitch
That will tell you how much the fingers are angled upwards. If they are angled downwards the value will be -90 degrees. In other words, this is a version of the “pitch” angle.
There is also an ArcTan2 function, which works like ArcTan but it can give you the angles of a full circle if you give it the fractions for the two directions. It can tell the difference between 45 degrees and 225 degrees.
ArcTan2 works like this: the first parameter is the fraction which corresponds to 90 degrees and the second parameter is the fraction that corresponds to 0 degrees. For example, lets say we want to find the horizontal angle of the fingers, with 0 degrees meaning forwards, 90 degrees meaning right, 180 degrees meaning backwards and -90 degrees meaning left. So we will use the forwards component (z or column 3) of the fingers for the second parameter (which represents 0 degrees) and we will use the right component (x or column 1) of the fingers for the first parameter (which represents 90 degrees). So it looks like this:
debug = ArcTan2(p5.mat31, p5.mat33) // a full circle, from forwards, kind of yaw
This gives us a version of the “yaw” angle. Note that it is different from what we did above, because we want a complete circle for the yaw, from -180 to 180. But for the pitch we only wanted -90 to +90. Of course, we just decided that arbitrarily. We could have got different versions of the angles. For example, with these two angles, if you pitch your hand back 110 degrees then it will be considered to be a yaw of 180 degrees and a pitch of 80 degrees, because we made pitch only go up to 90, and yaw go all the way around. We could just as easily choose the opposite way around:
Lets say we want the pitch to have forwards as 0, up as 90, and backwards as 180. That means we need to use ArcTan2 (for the whole circle) and we want the first parameter (90 degrees) to be the y part of the fingers row, and we want the second parameter (0 degrees) to be the z part of the fingers row.
debug = ArcTan2(p5.mat32, p5.mat33) // a full-circle, from forwards, kind of pitch
That kind of pitch actually makes less sense to most people, since if you tilt your hand up a bit (say 10 degrees), then rotate it to the right 80 degree, the pitch will stay the same as you expect, but if you then rotate it 20 degrees more to the right, the pitch will change to 170 degrees. Of course that was the same problem we would have with the yaw if we rotated up more than 90 degrees, the yaw would have jumped.
There is no perfect set of angles that makes the most sense. It depends which angles suit you and your application most. Just think what information you really want.

### Euler Angles

Euler angles suck. They exist solely to make a set of three steps to get to a particular orientation. Individually they mean nothing though.

If you rotate an object several times, the order is important. A different order gives you a different orientation. Not all programs use the same order, or directions.
The Euler angles GlovePIE provides are in the order pitch, yaw, roll. First it rotates the hand upwards by the first angle. Then it takes the result of that and twists that thing sideways to the right by the second angle. Then whatever is left you tip over to the right by the third angle, so that whatever was at the top after the first two steps becomes at the right after the third step.
So the Pitch, Yaw and Roll angles provide one way to get to the current orientation of the glove in three steps.
Anyway, these angles can be read form P5.Pitch, P5.Yaw, and P5.Roll, but their usage is not recommended.

### Angular (rotation) velocity (speed)

If you want to measure how fast the hand is rotating, you can use PitchVelocity, YawVelocity and RollVelocity. Unlike Pitch, Yaw and Roll, they are not steps, and they do make sense individually. Their usage is recommended.

They are measured in degrees per second.
PitchVelocity is how fast the front is becoming the top.

YawVelocity is how fast the front is becoming the right.

RollVelocity is how fast the top is becoming the right.
You can also get the acceleration in degrees-per-second per second. They are PitchAcceleration, YawAcceleration and RollAcceleration.
All these values may require smoothing to make them worth using.