Hey all,
When I TrianglePick a point on a TerrainBlock, so that I can move my characters to that point, I use the code below. Strangely, the first time I click on the terrain, the program pauses for a good 8 seconds and then continues normally. Every subsequent click is very smooth, so I'm not sure what the problem is.
I've found the exact line that causes the problem, it's the myScene.findPick(Ray, TrianglePickResults). I'm not sure how to fix this, I even tried hacking around it by calling that method in the constructor to get this strange one-time lag over with, but it didn't work. Let me know! Thanks a ton!
(Btw this is jME 2.0)
public void performAction(InputActionEvent evt)
{
selected = (Actor) myChaser.getTarget();
if(MouseInput.get().isButtonDown(1))
{
Vector2f screenPos = new Vector2f(MouseInput.get().getXAbsolute(), MouseInput.get().getYAbsolute());
Vector3f startPoint = DisplaySystem.getDisplaySystem().getWorldCoordinates(screenPos, 0);
Vector3f endPoint = DisplaySystem.getDisplaySystem().getWorldCoordinates(screenPos, 1);
Ray ray = new Ray(startPoint, endPoint.subtract(startPoint));
results.clear();
myScene.findPick(ray, results); //THIS IS THE BAD LINE
Vector3f loc = new Vector3f();
Vector3f[] vertex = new Vector3f[3];
boolean foundMeshHit = false;
for(int pickIndex = 0; pickIndex < results.getNumber(); pickIndex++)
{
TriMesh mesh = (TriMesh) results.getPickData(pickIndex).getTargetMesh();
if(mesh instanceof TerrainBlock)
{
for (int j = 0; j < mesh.getTriangleCount(); j++)
{
mesh.getTriangle(j, vertex);
foundMeshHit = (ray.intersectWhere(vertex[0].addLocal(mesh.getWorldTranslation()),
vertex[1].addLocal(mesh.getWorldTranslation()),
vertex[2].addLocal(mesh.getWorldTranslation()), loc));
if(foundMeshHit)
{
selected.setDestination(loc);
break;
}
}
}
}
}
}