Anyone knows how to calculate the coordinate of the intersection between the pick ray and the picked geometry?
^^ bump! ;D ^^
I’m really interested to see how people accomplish this. I have found lots of information on how to determine if a ray intersects a shape, but not where.
Think i have a similar problem.
I need the virtual universe coordinates at a specific screen coordinate.
Is there already such a method in Xith?
Here is my solution:
public Vector3f getIntersection(Point mousePos, Point3f camPos, Point3f lookat)
{
double r=(mousePos.x - screenWidth/2.0f)/(screenWidth/2.0f)*0.935f;
double s=((screenHeight/2.0f - mousePos.y)/(screenHeight/2.0f)) * (screenHeight/screenWidth)*0.935f;
//Vector d from Camera to Lookat
Vector3f d=new Vector3f();
d.x=camPos.x-lookat.x;
d.y=Math.abs(camPos.y-lookat.y);
d.z=camPos.z-lookat.z;
//Vector v1 parallel to Ground and perpendicularly to Vector d
Vector3f v1=new Vector3f();
v1.x=d.z;
v1.y=0;
v1.z=-d.x;
v1.normalize();
// Crossproduct of d and v1
Vector3f v2= new Vector3f();
v2.cross(d,v1);
v2.normalize();
//Plane to project the pointer
Vector3f v=new Vector3f();
v.x=(float)(lookat.x+r*v1.x+s*v2.x);
v.y=(float)(lookat.y+s*v2.y);
v.z=(float)(lookat.z+r*v1.z+s*v2.z);
/*Intersection
*Line: l=camPos+w(v-camPos)
*Plane: p=sx*(1;0;0)+sz*(0;0;1)
*l=p ....
*/
float w=(float)(-camPos.y/(v.y-camPos.y));
float sx=(float)(camPos.x+w*(v.x-camPos.x));
float sz=(float)(camPos.z+w*(v.z-camPos.z));
//Plane
Vector3f plane=new Vector3f();
plane.x=sx;
plane.y=0;
plane.z=sz;
return plane;
}
It does not work for shapes yet. Only a testplane. But it should not be hard to develop it further.
Think boundingboxes would be a good solution instead of checking the shapegeomery itself.
If wanted i can post a full example.
Bye
Jacke
Take a look at http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=xith3d;action=display;num=1091274518,
and try to make the picking better (I need it too ;))