Ejemplo n.º 1
0
void Cross::activateGrab() {
    if(isGrabActive) return;
    isGrabActive = true;

    float minDistance = 1000;
    RenderObject * minBody = NULL;

    // TODO children hack
    const vec3 crossPos = this->getPosition();
    std::vector<RenderObject *> allSceneObjects;
    for(unsigned int i = 0; i < sceneObjects->size(); i++){
        RenderObject * body = (*sceneObjects)[i];
        allSceneObjects.push_back(body);
        const std::vector<RenderObject *>& children = body->getChildren();
        for(unsigned int j = 0; j < children.size(); j++){
            allSceneObjects.push_back(children[j]);
        }
    }

    for(unsigned int i = 0; i < allSceneObjects.size(); i++){
        RenderObject * body = (allSceneObjects)[i];
        if(!body->isGrabable()) continue;

        const vec3& bodyPos = body->getPosition();

        float dx = crossPos.x - bodyPos.x;
        dx *= dx;
        float dy = crossPos.y - bodyPos.y;
        dy *= dy;
        float dz = crossPos.z - bodyPos.z;
        dz *= dz;
        float distance = sqrt(dx + dy + dz);
        if(distance < grabRadius){
            if(distance < minDistance){
                minDistance = distance;
                minBody = body;
            }
        }
    }
    if(minBody != NULL){
        grabedMap.clear();
        grabedMap[minBody] = BodyInfo(minDistance, *minBody->getColor());
        minBody->setColor(grabedColor);
        minBody->setSelected(true);
    }


}
Ejemplo n.º 2
0
std::vector<RenderObject*> Cross::getClosestObjectVector(const RayCast& ray,
                                                         int width, int height,
                                                         int distTol) const{
    std::vector<RenderObject*> closestObjects;
    float error_tol = 0.10;
    float x = ray.currentX;
    float y = ray.currentY;

    float vX = 2.0 / (float) width;
    int rayX = (x + 1.0f) / vX;

    float vY = 2.0 / (float) height;
    int rayY = (y + 1.0f) / vY;

    for(unsigned int i = 1; i < sceneObjects->size(); i++){


        RenderObject * body = (*sceneObjects)[i];

        // ---------------------
        // TODO children
        const std::vector<RenderObject *>& curr_children = body->getChildren();
        for(unsigned int i = 0 ; i < curr_children.size(); i++){
            RenderObject * child = curr_children[i];

            const vec3& bodyProjectedPosition = child->getProjectedPosition();

            float bodyX = bodyProjectedPosition.x;
            float bodyY = bodyProjectedPosition.y;
            if(child->NDC_W < error_tol) continue;

            int bodypX = (bodyX + 1.0f) / vX;
            int bodypY  = (bodyY + 1.0f) / vY;

            int dx = rayX - bodypX;
            int dy = rayY - bodypY;
            int dist = sqrt(dx*dx + dy*dy);

            if(dist < distTol){
                closestObjects.push_back(child);
                return closestObjects;
            }
        }
        // ---------------------

        if(!body->isGrabable()) continue;

        const vec3& bodyProjectedPosition = body->getProjectedPosition();

        float bodyX = bodyProjectedPosition.x;
        float bodyY = bodyProjectedPosition.y;
        if(body->NDC_W < error_tol) continue;

        int bodypX = (bodyX + 1.0f) / vX;
        int bodypY  = (bodyY + 1.0f) / vY;

        int dx = rayX - bodypX;
        int dy = rayY - bodypY;
        int dist = sqrt(dx*dx + dy*dy);

        if(dist < distTol){
            closestObjects.push_back(body);
        }
    }
    return closestObjects;
}