September 8, 2016

The Circle Tutorial

Two blocks down and make a right.

Well making a right turn in space may not have as simple a meaning as is does on earth, but we can do something like it. When we turn right on earth we are doing something like turning to a vector that is perpendicular to a vector pointing straight ahead of us and also perpendicular to the vector defining the plane of the ground (which is a vector running from our feet up through our head.) This also happens to be true of turning left. There’s no small coincidence that there’s an APP, I mean API, for that. A vector Cross Product gives us a vector that is perpendicular to two other vectors. (The two original vectors don’t need to be perpendicular to each other as in my example, but they shouldn’t be parallel to each other.)

So let’s just do it. We need to calculate a “right turn” and demonstrate that we did it. Certainly we could do the calculation and print the results out in a debug statement, but isn’t it better to see something happen with a satellite? So we’ll calculate the cross product of the satellite’s orientation vector and the negative z-axis. This will give us the right turn that we were looking for.

Okay this could be a little more intuitive. Remember when I mentioned the vector running up through our head? In the FreeMode game the positive z-axis runs down. So the vector that runs up through the satellite is actually the negative z-axis. So using the right hand rule crossing a vector forward from the satellite with the vector running “up” through the satellite gives us a right turn. The exact same thing would happen if we used the positive z-axis but then you’d have to turn the bottom of your screen up in order to see the turn as one to the right. 

Once we calculate the new orientation we’ll simply rotate to that orientation. We need to make sure that we only do this calculation once so we don’t keep chasing our right shoulder (like a dog chasing its tail.) Our new loop() will need to look something like this:

void loop(){
    float MyState[12];
    float OtherState[12];
 
    api.getMyZRState(MyState);
    api.getOtherZRState(OtherState);
 
    if (time==0)
    {
        float axis[]={0,0,-1};
        mathVecCross(dir,&MyState[6],axis);
    }
 
    api.setAttitudeTarget(dir);
     
    time++;
}

Notice that the calculation is only performed once, but we need to call setAttitudeTarget every loop. Try it.

Did you see the satellite rotate to the “right” 90°?

Do you know what the original orientation was?

What happens if you try different values for the “axis” vector?