Hi, I am trying to make 3d strategy game. I am using mouse to click on gameboard, but problem is that i cant determine where I am clicking on board, all I can get is possition of cursor on screen. I guess theres some equation, but cant find it or make it out. Or perhaps theres some function?
Any help is appreciated.
you need to cast a ray into the scene and check the intersections…
here's some code to find triangle intersection with a ray…
final static public TrianglePickData findClosestTriangle(Ray cl_ray, Spatial cl_testAgainst) {
TrianglePickResults lcl_pick = new TrianglePickResults();
lcl_pick.setCheckDistance(true);
cl_testAgainst.findPick(cl_ray, lcl_pick);
if(lcl_pick.getNumber() == 0) {
return null;
}
float lf_distance = Float.MAX_VALUE;
int li_lowestIndex = -1;
for(int li_index = 0; li_index < lcl_pick.getNumber(); li_index++) {
TrianglePickData lcl_data = (TrianglePickData)lcl_pick.getPickData(li_index);
if(lcl_data.getDistance() < lf_distance) {
lf_distance = lcl_data.getDistance();
li_lowestIndex = li_index;
}
}
if(li_lowestIndex == -1) {
return null;
}
return (TrianglePickData)lcl_pick.getPickData(li_lowestIndex);
}
you would use it then like this...
Vector2f lcl_pt = new Vector2f(MouseInput.get().getXAbsolute(), MouseInput.get().getYAbsolute());
Vector3f lcl_worldNear = DisplaySystem.getDisplaySystem().getWorldCoordinates(lcl_pt, 0);
Vector3f lcl_worldFar = DisplaySystem.getDisplaySystem().getWorldCoordinates(lcl_pt, 1);
Ray lcl_ray = new Ray(lcl_worldNear, lcl_worldFar.subtractLocal(lcl_worldNear).normalizeLocal());
TrianglePickData lcl_p = findClosestTriangle(lcl_ray, rootNode);
if(lcl_p == null) {
System.out.println("no sel");
return;
}
if(lcl_p.getTargetMesh() instanceof TriMesh) {
TriMesh lcl_mesh = (TriMesh)lcl_p.getTargetMesh();
System.out.println("sel = " + lcl_mesh.getName());
}
Thanks a lot! thats exactly what i was looking for