Exemplo n.º 1
0
/******************************************************************************
 Main function.
******************************************************************************/
int main(int argc, char* argv[])
{
    HDErrorInfo error;

    printf("Starting application\n");
    
    atexit(exitHandler);

    // Initialize the device.  This needs to be called before any other
    // actions on the device are performed.
    ghHD = hdInitDevice(HD_DEFAULT_DEVICE);
    if (HD_DEVICE_ERROR(error = hdGetError()))
    {
        hduPrintError(stderr, &error, "Failed to initialize haptic device");
        fprintf(stderr, "\nPress any key to quit.\n");
        getchar();
        exit(-1);
    }

    printf("Found device %s\n",hdGetString(HD_DEVICE_MODEL_TYPE));
    
    hdEnable(HD_FORCE_OUTPUT);
    hdEnable(HD_MAX_FORCE_CLAMPING);

    hdStartScheduler();
    if (HD_DEVICE_ERROR(error = hdGetError()))
    {
        hduPrintError(stderr, &error, "Failed to start scheduler");
        fprintf(stderr, "\nPress any key to quit.\n");
        getchar();
        exit(-1);
    }
    
    initGlut(argc, argv);

    // Get the workspace dimensions.
    HDdouble maxWorkspace[6];
    hdGetDoublev(HD_MAX_WORKSPACE_DIMENSIONS, maxWorkspace);

    // Low/left/back point of device workspace.
    hduVector3Dd LLB(maxWorkspace[0], maxWorkspace[1], maxWorkspace[2]);
    // Top/right/front point of device workspace.
    hduVector3Dd TRF(maxWorkspace[3], maxWorkspace[4], maxWorkspace[5]);
    initGraphics(LLB, TRF);

    // Application loop.
    CoulombForceField();

    printf("Done\n");
    return 0;
}
Exemplo n.º 2
0
bool collisionRectSAT(glm::vec4& URA4, glm::vec4& LRA4, glm::vec4& LLA4, glm::vec4& ULA4,
                      glm::vec4& URB4, glm::vec4& LRB4, glm::vec4& LLB4, glm::vec4& ULB4) {
    
    glm::vec3 URA(URA4);
    glm::vec3 LRA(LRA4);
    glm::vec3 LLA(LLA4);
    glm::vec3 ULA(ULA4);
    
    glm::vec3 URB(URB4);
    glm::vec3 LRB(LRB4);
    glm::vec3 LLB(LLB4);
    glm::vec3 ULB(ULB4);
    
    glm::vec3 axis[4];
    
    axis[0].x = URA.x - ULA.x;
    axis[0].y = URA.y - ULA.y;
    axis[1].x = URA.x - LRA.x;
    axis[1].y = URA.y - LRA.y;
    axis[2].x = ULB.x - LLB.x;
    axis[2].y = ULB.y - LLB.y;
    axis[3].x = ULB.x - URB.x;
    axis[3].y = ULB.y - URB.y;
    
    glm::vec3 vertA[] = {URA, LRA, LLA, ULA};
    glm::vec3 vertB[] = {URB, LRB, LLB, ULB};
    
    glm::vec3 projA[4];
    glm::vec3 projB[4];
    
    std::vector<double> positionA(4);
    std::vector<double> positionB(4);
    for (int a = 0; a < 4; a++) {
        for (int i = 0; i < 4; i++) {
            double top = vertA[i].x * axis[a].x + vertA[i].y * axis[a].y;
            double bottom = axis[a].x * axis[a].x + axis[a].y * axis[a].y;
        
            double common = top / bottom;
        
            projA[i].x = common * axis[a].x;
            projA[i].y = common * axis[a].y;
        
            positionA.at(i) = glm::dot(projA[i], axis[a]);
        }
    
        for (int i = 0; i < 4; i++) {
            double top = vertB[i].x * axis[a].x + vertB[i].y * axis[a].y;
            double bottom = axis[a].x * axis[a].x + axis[a].y * axis[a].y;
        
            double common = top / bottom;
        
            projB[i].x = common * axis[a].x;
            projB[i].y = common * axis[a].y;
        
            positionB.at(i) = glm::dot(projB[i], axis[a]);
        }
        double minA = *(std::min_element(positionA.begin(), positionA.end()));
        double minB = *(std::min_element(positionB.begin(), positionB.end()));
        double maxA = *(std::max_element(positionA.begin(), positionA.end()));
        double maxB = *(std::max_element(positionB.begin(), positionB.end()));
    
        //no collision on this axis, none at all
        if (((minB > maxA) || (maxB < minA))) {
            return false;
        }
    }
    return true;
}