Example #1
0
void GLElbowFlexWidget::drawMovingArm() {

    upperArm();

    elbow();

    rotateForeArm();

    foreArm();

    hand();
}
int main()	{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 700, 32), "So Robot, Much Arm | Emmett Daly | C00001839");

    int windowWidth = 800;
    int windowHeight = 700;

    const int limbLines = 6;
    const int handLines = 7;
    const int armLength = 150;
    const int armWidth = 15;

    // Load a font
    sf::Font font;
    font.loadFromFile("C:\\Windows\\Fonts\\ARIAL.TTF");

    // Create a formatted text string - The title
    // Add some instructions for the user
    sf::Text instructions[8];
    int yPosition = 10;

    for (int i = 0; i < 8; i++) {
        yPosition += 14;
        instructions[i].setFont(font);
        instructions[i].setStyle(sf::Text::Bold);
        instructions[i].setCharacterSize(14);
        instructions[i].setPosition(10, yPosition);
    }

    instructions[0].setString(" Emmett Daly | C00001839 | November 2015");
    instructions[1].setString(" Instructions:");
    instructions[2].setString(" Q  : Move Upper Arm Up");
    instructions[3].setString(" A  : Move Upper Arm Down");
    instructions[4].setString(" W  : Move Fore Arm Up");
    instructions[5].setString(" S  : Move Fore Arm Down");
    instructions[6].setString(" X  : Expand Hand");
    instructions[7].setString(" C  : Contract Hand");

    // Define the aul type of the arm parts we'll be using
    sf::VertexArray upperArm(sf::Triangles, limbLines);
    sf::VertexArray foreArm(sf::Triangles, limbLines);
    sf::VertexArray leftHand(sf::LinesStrip, handLines);
    sf::VertexArray rightHand(sf::LinesStrip, handLines);

    // Give them a couple of point definitions so we can place them
    sf::Vector2f limbPoints[limbLines];
    sf::Vector2f leftHandPoints[handLines];
    sf::Vector2f rightHandPoints[handLines];

    // Lets make limbs
    limbPoints[0] = sf::Vector2f(0, 0 - armWidth / 2);
    limbPoints[1] = sf::Vector2f(armLength, 0 - armWidth / 2);
    limbPoints[2] = sf::Vector2f(0, armWidth / 2);
    limbPoints[3] = sf::Vector2f(0, armWidth / 2);
    limbPoints[4] = sf::Vector2f(armLength, armWidth / 2);
    limbPoints[5] = sf::Vector2f(armLength, 0 - armWidth / 2);

    leftHandPoints[0] = sf::Vector2f(0, 0);
    leftHandPoints[1] = sf::Vector2f(20, 0);
    leftHandPoints[2] = sf::Vector2f(50, -40);
    leftHandPoints[3] = sf::Vector2f(80, 0);
    leftHandPoints[4] = sf::Vector2f(90, 0);
    leftHandPoints[5] = sf::Vector2f(50, -50);
    leftHandPoints[6] = leftHandPoints[0];

    rightHandPoints[0] = leftHandPoints[0];
    rightHandPoints[1] = leftHandPoints[1];
    rightHandPoints[2] = sf::Vector2f(50, 40);
    rightHandPoints[3] = leftHandPoints[3];
    rightHandPoints[4] = leftHandPoints[4];
    rightHandPoints[5] = sf::Vector2f(50, 50);
    rightHandPoints[6] = rightHandPoints[0];

    // Create a few 'joints' so we can rotate them, just a bit though, because it's not realistic otherwise :D
    float rotationUpperArm = 0.0;
    float rotationForeArm = 0.0;
    float rotationLeftHand = 0.0;
    float rotationRightHand = 0.0;

    // Let's play countdown
    while (window.isOpen())
    {
        // Sure we may as well process the events too
        sf::Event Event;
        while (window.pollEvent(Event))
        {
            // Right down to business,
            // Define some basic transformations
            sf::Transform upperArmTransform;
            sf::Transform foreArmTransform;
            sf::Transform leftHandTranslation;
            sf::Transform upperArmRotation;
            sf::Transform foreArmRotation;
            sf::Transform leftHandRotation;
            sf::Transform rightHandTranslation;
            sf::Transform rightHandRotation;

            // Assign values to these transformations
            upperArmTransform.translate(100, 500);
            upperArmRotation.rotate(rotationUpperArm);
            foreArmTransform.translate(armLength, 0);
            foreArmRotation.rotate(rotationForeArm);
            leftHandTranslation.translate(armLength, 0);
            leftHandRotation.rotate(rotationLeftHand);
            rightHandTranslation.translate(armLength, 0);
            rightHandRotation.rotate(rotationRightHand);

            // Now, throw the limbs in, using transformations
            sf::Transform upperArmPart = upperArmTransform * upperArmRotation;
            sf::Transform foreArmPart = upperArmPart * foreArmTransform * foreArmRotation;
            sf::Transform leftHandPart = foreArmPart * leftHandTranslation * leftHandRotation;
            sf::Transform rightHandPart = foreArmPart * rightHandTranslation * rightHandRotation;

            // Drawing the limb lines AND adding color, 'cause I'm clever like that
            for (int i = 0; i < limbLines; i++) {
                upperArm[i].position = upperArmPart.transformPoint(limbPoints[i]);
                foreArm[i].position = foreArmPart.transformPoint(limbPoints[i]);
                upperArm[i].color = sf::Color::White;
                foreArm[i].color = sf::Color::Cyan;
            }
            // Ditto, but for the hands
            for (int i = 0; i < handLines; i++) {
                leftHand[i].position = leftHandPart.transformPoint(leftHandPoints[i]);
                rightHand[i].position = rightHandPart.transformPoint(rightHandPoints[i]);
                leftHand[i].color = sf::Color::Green;
                rightHand[i].color = sf::Color::Green;
            }
            // Close window : exit
            if (Event.type == sf::Event::Closed)
                window.close();
            // Escape key : exit
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
                window.close();
            // Move the upperArm & other connected limbs down
            // The upperarm movement will ave the most translations applied to it, as all parts are affected by it
            // The rotation limit is set to 65 degrees for the upperArm,
            // While rotating, we increment this until it hits 65 degrees,
            // firstly applying...
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::A)) {
                if (rotationUpperArm < 65) {
                    rotationUpperArm += 1;
                    // ...a rotation,based on what rotationUpperArm's value is, as it gets incremented each time...
                    upperArmRotation.rotate(rotationUpperArm);
                    // ... then, applying the correct matrix transformation to the upperArm ...
                    upperArmPart = upperArmTransform * upperArmRotation;
                    // ... rotating the forearm as its obviously attached to the upperArm ...
                    foreArmRotation.rotate(rotationForeArm);
                    // ... then, applying the correct matrix transformation to the foreArm ....
                    foreArmPart = upperArmPart * foreArmTransform * foreArmRotation;
                    // ... and updating the limbPoints.
                    for (int i = 0; i < limbLines; i++) {
                        upperArm[i].position = upperArmPart.transformPoint(limbPoints[i]);
                        foreArm[i].position = foreArmPart.transformPoint(limbPoints[i]);
                    }
                }
            }
            // Move the upperArm & other connected limbs Up
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Q)) {
                if (rotationUpperArm > -65) {
                    rotationUpperArm -= 1;
                    upperArmRotation.rotate(rotationUpperArm);
                    upperArmPart = upperArmTransform * upperArmRotation;
                    foreArmRotation.rotate(rotationForeArm);
                    foreArmPart = upperArmPart * foreArmTransform * foreArmRotation;
                    for (int i = 0; i < limbLines; i++) {
                        foreArm[i].position = foreArmPart.transformPoint(limbPoints[i]);
                    }
                }
            }
            // Same applies as the upperArm, only the forearm is affect here, with a rotation
            // limit of 125 degrees, to offer more movement options
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::W)) {
                if (rotationForeArm > -125) {
                    rotationForeArm -= 1;
                    foreArmRotation.rotate(rotationForeArm);
                    foreArmPart = upperArmPart * foreArmTransform * foreArmRotation;
                    for (int i = 0; i < limbLines; i++) {
                        foreArm[i].position = foreArmPart.transformPoint(limbPoints[i]);
                    }
                }
            }
            // Move the foreArm & hand down
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::S)) {
                if (rotationForeArm < 125) {
                    rotationForeArm += 1;
                    foreArmRotation.rotate(rotationForeArm);
                    foreArmPart = upperArmPart * foreArmTransform * foreArmRotation;
                    for (int i = 0; i < limbLines; i++) {
                        foreArm[i].position = foreArmPart.transformPoint(limbPoints[i]);
                    }
                }
            }
            // The hand.
            // The only thing that should be moving here when X is pressed, is the hand, thus we
            // only move the hand, expanding it as far as 55 degrees
            // The same logic applies as te other limbs, excpet, we need to
            // expand both parts as opposed to only one.
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::X)) {
                if (rotationLeftHand > -55) {
                    rotationLeftHand -= 1;
                    rotationRightHand += 1;
                    leftHandRotation.rotate(rotationLeftHand);
                    rightHandRotation.rotate(rotationRightHand);
                    leftHandPart = leftHandPart *  leftHandRotation;
                    rightHandPart = rightHandPart *  rightHandRotation;
                    for (int i = 0; i < handLines; i++) {
                        leftHand[i].position = leftHandPart.transformPoint(limbPoints[i]);
                        rightHand[i].position = rightHandPart.transformPoint(limbPoints[i]);
                    }
                }
            }
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::C)) {
                if (rotationLeftHand < 0 ) {
                    rotationLeftHand += 1;
                    rotationRightHand -= 1;
                    leftHandRotation.rotate(rotationLeftHand);
                    rightHandRotation.rotate(rotationRightHand);
                    leftHandPart = leftHandPart *  leftHandRotation;
                    rightHandPart = rightHandPart *  rightHandRotation;
                    for (int i = 0; i < handLines; i++) {
                        leftHand[i].position = leftHandPart.transformPoint(limbPoints[i]);
                        rightHand[i].position = rightHandPart.transformPoint(limbPoints[i]);
                    }
                }
            }
        }
        window.clear();
        // Draw frame items
        for (int i = 0; i < 8; i++) {
            window.draw(instructions[i]);
        }
        window.draw(upperArm);
        window.draw(foreArm);
        window.draw(leftHand);
        window.draw(rightHand);

        // Finally, display rendered frame on screen
        window.display();
    } //loop back for next frame

    return EXIT_SUCCESS;
}