Solution Explorer






struct Wiimote_Manager_Code {

Welcome to my Wiimote Manager Code page. Here you can see a few snippets of code that I wrote for my Wiimote Manager.

My Wiimote Manager is a wrapper around a C++ Wiimote library called WiiYourself developed by gl.tter. You can learn more about what my manager does at its project page. My manager keeps track of all the critical data of every wiimote connected, and it also performs operations on this data to return more accurate results.

One of these operations is to actually rotate the IR data around using the values from the Wiimote accelerometers. This is a very help function because when the wiimote is rotated, the IR data no longer update in the way the player expects. For example, if the wiimote is rotated 90 degrees, and the user start to aim in a upward motion, the wiimote will see the IR dots going up toward the side of its IR window. This will result in your onscreen cursor to move to the side (but the player expected it to go up). Wiimote_Code_1 shows off how I take this rotation into consideration so this problem can be avoided.

Another operation the manager performs is to keep track of the previous and current button states so that a user can ask if a button has been pressed or released since the last frame. In order to speed things up and keep the code clean, I use a bit mask to store the states of the buttons. Wiimote_Code_2 shows off this button testing and masking.

Wiimote_Code_1: rotated IR data

bool WiimoteManager::getDotPosRawRotated(WiimoteIndex wmIndex, DotIndex dIndex, Point &dst)
    //is the wmIndex not connected?
    if(wiimotes.empty() || !wiimotes[wmIndex]->wiimote.IsConnected())
        return false;
    float roll, sine, cosine;
    Point dot;
    Point rot;
    int originX = 1024/2;
    int originY = 768/2;
    int xx, yy;

    if(canSeeDot(wmIndex, dIndex))
        this->getDotPosRaw(wmIndex, dIndex, dot);
        this->getWiimotePitchRoll(wmIndex, rot);
        roll = rot[ROLL];
        sine   = sin(DEG2RAD(-roll)); //get sine of roll in radians

        cosine = cos(DEG2RAD(-roll)); //get cosine of roll in radians

        xx = dot[X] - originX;
        yy = dot[Y] - originY;

        //rotate the dot
        dot[X] = (cosine * xx) + (-sine * yy);
        dot[Y] = (sine * xx) + (cosine * yy);
        //translate back relative to origin
        dot[X] += originX;
        dot[Y] += originY;

        //store it
        dst[X] = 1024 - dot[X];
        dst[Y] = dot[Y];
        dst[Z] = 0; //make sure its zero

        return true;
    return false;

Wiimote_Code_2: buttons testing and masking.

//helper function: update the button bits
void WiimoteManager::updateButtons(WiimoteStruct *ws)
    /* store the buttons, each button is a bit, 1 = pressed
     * wiimote has 11 buttons so it takes bits 0-10
     * nunchuk will take bits 11-12
    ws->currentButtonBits = 0; //reset the buttons
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.A() << 0);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.B() << 1);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.Up() << 2);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.Down() << 3);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.Left() << 4);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.Right() << 5);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.Minus() << 6);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.Home() << 7);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.Plus() << 8);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.One() << 9);
    ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Button.Two() << 10);
    if(ws->currentState.ExtensionType == wiimote::NUNCHUK)
        ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Nunchuk.C << 11);
        ws->currentButtonBits = ws->currentButtonBits | (ws->currentState.Nunchuk.Z << 12);

bool WiimoteManager::isJustPressed(WiimoteIndex wmIndex, unsigned int b)
    //is the wmIndex not connected?
    if(wiimotes.empty() || !wiimotes[wmIndex]->wiimote.IsConnected())
        return false;

    //get the states for the requested wiimote
    return ((b & wiimotes[wmIndex]->currentButtonBits) && 
            !(b & wiimotes[wmIndex]->previousButtonBits));

}//end Wiimote_Manager_Code