Obtaining a pick ray (or origin+vector)

Hi,
can someone give me a hint on how to compute a pickray based on the mouse pointer position on the Canvas3D.

I require to get the origin (camera/view origin) and a vector for the direction of the pointing.

Thanks a lot

PS: I know Xith has picking capabilities but I need a different style of picking.

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=xith3d;action=display;num=1068252077;start=30

thanks for the info … I try to get it to work :slight_smile:

finally got it to work:

here is the code (not optimized):

public boolean computePickOriginAndRay(int _mouseX, int _mouseY, Point3f origin, Vector3f ray) {
float width
= (float)(canvas.getWidth());
float height
= (float)(canvas.getHeight());

    float aspect_ = width_/height_;
    
    float nx_ = (((float)_mouseX / width_) -.5f)*2;
    float ny_ = (((float)_mouseY / height_) -.5f)*2;
    
    float distance_ = -5000f; // can be removed later
    float fov_ = view.getFieldOfView();
    
    float dist_ = distance_*(float)Math.tan(fov_); // dist = Z * tan ( FOV / 2 )
    
    float screenX_ = nx_*dist_;
    float screenY_ = ny_*dist_;
    
    _ray.set(-screenX_*aspect_, screenY_, distance_);
    
    Transform3D view_ = new Transform3D(view.getTransform());
    
    view_.transform(_ray);
    
    System.out.println("[db] RAY: "+_ray.toString());
    
    
    _origin.set(currentPosition.x,currentPosition.y,currentPosition.z);
    
    return true;
}

That’s cool, but where does:

currentPosition come from?

currentPosition is the current position of your view. In order to check a ray for intersections you will probably need the origin from where the ray is projected.

I used it in this rayIntersection function:

the triangle is defined by three points a,b,c
and subsequently by the vectors ab,ac

the return is the factor by which you have to scale _dir to reach
the intersection point: hit = _origin + (return)*_dir;

public float rayIntersection(Point3f _origin, Vector3f _dir) {
Vector3f pvec = Util3d.makeNormal(ac,_dir);
float det = Util3d.dotProduct(ab,pvec);

    if (det > -Util3d.EPSILONf && det < Util3d.EPSILONf) return 0;
    
    float inv_det = 1.0f / det;
    Vector3f tvec = Util3d.makeVector(a,_origin);
    float u = Util3d.dotProduct(tvec,pvec) * inv_det;
    
    if (u < 0.0f || u > 1.0f) return 0;
    
    Vector3f qvec = Util3d.makeNormal(ab,tvec);
    
    float v = Util3d.dotProduct(qvec,_dir)*inv_det;
    
    if ( v < 0.0 || (u+v) > 1.0 ) return 0;
    
    return Util3d.dotProduct(ac,qvec)*inv_det;
}

PS: don’t ask me how or why this works. I got it from some publication about efficient intersection computation and ported it
to java. It has not let me down so far.

I recognize the triangle intersect code, that’s from the “Fast, Minimum Storage Ray/Triangle Intersection” paper by Tomas Moller and Ben Trumbore. It’s been the theory basis for a lot of intersection tutorials, like at FlipCode.

the paper I got it from was from:

// From the rayIntersectsTriangle Algorithm of Tomas M?ller & Prosolvia Clarus AB

just to get the credit right :wink: