This should be a standard collision bewteen a ray and a node, but for some reason it doesn’t work as expected.
I have this static factory method:
public class PieceFactory
{
// ...static stuff referenced below...
public static Node buildTilesNode()
{
final Node tilesNode = new Node();
Spatial tile;
for(int queue = 0; queue < Board.QUEUES; queue++)
for(int level = 0; level < Board.LEVELS; level++)
{
tile = new Geometry(queue + ":" + level, UNIT_BOX);
tile.setMaterial(new Material(ASSET_MANAGER, "Common/MatDefs/Misc/Unshaded.j3md"));
((Geometry) tile).getMaterial().setColor("Color", ColorRGBA.LightGray);
tile.setLocalScale(CELL_WIDTH, CELL_HEIGHT, .0f);
tile.setLocalTranslation(positionToTranslation(queue, level));
tile.setUserData("position", new Position(queue, level));
tilesNode.attachChild(tile);
}
return tilesNode;
}
}
…and the buildTilesNode()
method is used elsewhere this way:
public class Table
{
public final Node tiles = PieceFactory.buildTilesNode();
// ...
}
This is where something goes wrong:
public class Test extends AbstractAppState
{
// ...stuff...
private Table table = new Table();
private Position pickTile()
{
final Vector2f clickSpot = inputManager.getCursorPosition();
final Vector3f origin = camera.getWorldCoordinates(clickSpot, .0f);
final Vector3f direction =
camera.getWorldCoordinates(clickSpot, 1.0f).subtract(origin).normalize();
final Ray ray = new Ray(origin, direction);
final CollisionResults results = new CollisionResults();
table.tiles.collideWith(ray, results);
if(results.size() > 0)
return ((Spatial) results.getClosestCollision().getGeometry())
.getUserData("position");
return null;
}
}
The size of results
happens to always be 0.
What am I doing wrong?