Ray casting problem

I’m trying to use collision detection, but there are some points in the quad texture that if i click, the ray detection sees the sky as the closest object which is like 998 wu away. This is like one pixel wide, because anywhere else i click, i get the normal result. Any idea how to solve it? I don’t think i did anything overly complex so here’s the code

Vector2f click2d = g.getInputManager().getCursorPosition();

Vector3f click3d = g.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = g.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();

Ray ray = new Ray(click3d, dir);

CollisionResults results = new CollisionResults();

SceneNode.collideWith(ray, results);

CollisionResult closest = results.getClosestCollision();
Vector3f a = closest.getContactPoint();//
> this is where I get a huge distance



I know that problem, i’ve fixed it by going trough every result and check its name

for (int i = 0; i < results.size(); i++) {

String targetname = results.getCollision(i).getGeometry().getName();



Oh dear. That means that when the user clicks on the blind spots I will just pretend he didn’t do anything right?

At least I won’t put the selector object 800wu into the ground :stuck_out_tongue:

hm maybe i got your problem wrong, in my case i was getting the sky as the first collision and the object ive klicked on as the second.

So the Object you’ve clicked on is not in results? Check with

for (int i = 0; i < results.size(); i++) { System.out.println(results.getCollision(i).getGeometry().getName()); }

Look at this: When I click on the surface of the floor



AAAAAAAAAAAAAFloor ← this is the closest

When I find the right spot on the square


AAAAAAAAAAAAAsky ← this is the closest object

so i’m like…what just happened?

try this for center of camera view raytrace:


CollisionResults results = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

rootNode.collideWith(ray, results);

CollisionResult res = results.getClosestCollision();

if (res != null) {

click2Vect = res.getContactPoint();



i think you have somethin wrong with direction, it is normalized but propably wrong

for mouse position click try this:


Vector3f origin = camera.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);

Vector3f direction = camera.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);


Ray mouseRay = new Ray(origin, direction);

CollisionResults results = new CollisionResults();

if (results.size() > 0) {

Iterator<CollisionResult> i = results.iterator();

while (i.hasNext()) {

CollisionResult collision = i.next();

Node node = collision.getGeometry().getParent();

// code




if nothin work, then i dont know… maybe you have problem with node you try to raytrace


three dimensional cube for floor it is!!! thanks normen, that seems to be the problem (the floor is two dimensional and rays miss by a pixel)

if it work for terrainQuad i thinked it should work for a Quad :?

@oxplay2 said:
if it work for terrainQuad i thinked it should work for a Quad :?

not at all. terrain uses Bresenham picking
1 Like

ahh, thank you for reply. The more i know, the more advanced JMonkey is for me :stuck_out_tongue: