Intersecting point

Hi,



I'm new in jME and I'm searching how to find the coordinates of the intersection between a picking ray and a triangle from a mesh.



I've tried to use the getDistance method with the start and the end point but it seems that getDistance returns the length of the ray.



Thanks for your help.

For bounds picking PickData#getDistance() returns the distance of the ray origin to the bound. If you do triangle picking the current cvs version returns the distance to the closest hit triangle.

Just for the record, you should be able to use…



RayOrigin + (RayDirection * distance), to find the point of intersection assuming the triangle picking distance is correct.

I tried this in my own code, the distance returned is always zero, and I don't have time to figure it out since it's time to go to work.



Edit - Found PickResults.setCheckDistance(true) and that got rid of my problems with zero being the only value, though seems there is some other problems

OK, sadly it took me this long to figure it out (I don't spend nearly enough time working on my stuff lately). My formula is basically correct, the first thing is that Ray.getDirection() doesn't return a normalized vector (not sure why but it's what I expected it to return), and the thing that hung me up, is that getDistance returns the squared value of the distance, I assume since for triangle picking purposes, the exact distance isn't important and square root is slow… Anyway, below is a chunk of ugly code from my game that determines where you clicked on the terrain using my previously mentioned formula (and probably creating all kinds of extra unnecessary vector3f objects).



Vector2f screenPos = new Vector2f();

// Get the position that the mouse is pointing to

screenPos.set(absolutemouse.getHotSpotPosition().x, absolutemouse.getHotSpotPosition().y);

// Get the world location of that X,Y value

Vector3f worldCoords = display.getWorldCoordinates(screenPos, 1.0f);

// Create a ray starting from the camera, and going in the direction

// of the mouse's location

final Ray mouseRay = new Ray(cam.getLocation(), worldCoords

.subtractLocal(cam.getLocation()));

pickResults.clear();

pickResults.setCheckDistance(true);

terrainblock.calculatePick(mouseRay, pickResults);

if (pickResults.getNumber() > 0) {

for (int i = 0; i < pickResults.getNumber(); i++) {

PickData pData = pickResults.getPickData(i);

float distance = pData.getDistance();

Ray ray = pData.getRay();

Vector3f v3f = ray.getOrigin();

Vector3f dir = ray.getDirection().normalize().mult((float)Math.sqrt(distance));

Vector3f result = v3f.add(dir);

cursor3d.set(result);

}

}



cursor3d is a vector that is a vector3f object that is used for a node with a box attached to it for visual effect, but basically that's the point that your terrain was clicked on. If anyone wants, I can clean this up and post a full example.



edit And YAY for whoever fixed the odd ball triangle not being pickable bug (at least it seems fixed).

Hey DanK, you're right : it works pretty well !  XD

Thanks !



But now, guys, I've got an other question :



I create a Node "terrain" with a child Node for each texture layer. And each of these layers Nodes have a child TriMesh for each tile of my terrain. I use this kind of structure for texture splatting !



When I create one of the TriMesh, I use this code :



mesh.reconstruct(BufferUtils.createFloatBuffer(vertices),BufferUtils.createFloatBuffer(normals),null,BufferUtils.createFloatBuffer(textCoords),BufferUtils.createIntBuffer(indexes));

mesh.setModelBound(new BoundingBox());

mesh.updateModelBound();




which seems good to create a tile that I can select with the mouse.

But when I use the code DanK gave us before (which work very well with a box generated by jME), I found 0 pick result !



Any Idea ?



Thanks for your help

Hardly a noob type problem.  gratz on figuring it out

I've made some test and I got some pickresults when I click on the screen near the terrain and not on the terrain.



I think I'm lost with the coordinates of the ray.  ://

Perhaps it's because I translate my terrain.



Should I use this translation when I declare the ray ?

OK, nevermind !



That was because I'm using Swing with jME and the screen coordinates are upside down !



Sorry