Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Check ofRay intersection with a ofMeshFace

$
0
0

@Kaito_Kid wrote:

Pretty self explanatory. I've tried many examples of code here, but I couldn't get one to work.

I have an ofRay (from ofxRay), and a list of of3dPrimitive. I am trying to figure out if the ray intersects a primitive, and if so, to know which primitive the ray intersected with "first" (as in, which one is the closest to the screen).

ofVec3f screenToWorld = camera.screenToWorld(ofVec3f(x, y, 0.0));

std::list<primitive>::iterator iterator;

ofRay ray(camera.getPosition(), screenToWorld - camera.getPosition(), true);

for (iterator = primitives.begin(); iterator != primitives.end(); ++iterator)
{
    if (!shiftHeld)
    {
       iterator->setSelected(false);
    }

    float* distance = 0;

    bool found = iterator->FindIntersection(ray, distance);
}

And my findIntersection looks like this:

bool primitive::calcTriangleIntersection(ofRay ray, float *result) const {

    ofMesh mesh = prim->getMesh();
    std::vector<ofMeshFace> indices = mesh.getUniqueFaces();

    for (std::vector<ofMeshFace>::iterator i = indices.begin(); i != indices.end(); ++i)
    {
        ofMeshFace face = *i;

        // Detection here

        if (detected intersection)
        {
            *result = distance;
            return true;
        }
    }
    return false;
}

I've tried so many things, but none of them works. I'm also drawing my rays to the screen, so I know for a fact that they are indeed created correctly and go in the right direction for an infinite distance

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles