# Ray collision from above, using the mouse position

Hey there

I’m having some troubles again, this time with ray collisions. Probably because of my lack of knowledge with directions, camera position, etc, but I’ll try to explain:

What I want, is to cast a ray straight down (from infinite height, basicall - y position is irrelevant) according to the mouse position on the screen - but with the depth (Z) used for the mouse position. I created an Image, maybe that helps a bit at explaining what I mean:

http://i.imgur.com/19MCo.jpg

All ‘B’ mousepointers, cut with the box, all mousepointers outside of the box, dont hit anything. The blue lines are the ray’s I want to cast.

Now, there is one additional problem to this: the Box that you see, actually can have some other boxes above - but I basically can find out what box is visible and subtract the Y, so that’s not a large Issue.

My (ugly) solution, up to this point, was this: I created a Box, surrounding all other Meshes, and then casted a Ray like in the tutorial ( https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking ) with the difference, that I then subtracted the Y (basically, making Y zero, and only having x, z).

And that’s basically what I need: having the cursor position (2D: x, y) turned into a 3D: x, z position. Like if the roof of the Box would be the actual 2D screen. I hope I could explain it correctly, does anyone have a solution to something like this? I figured that I probably need to play around with the 3D direction, but up until now it didnt bring the expected results.

Regards, Ruby

From your picture i figure you want the ray to be cast straight upward not down, right?

what you can do is to cast 2 rays one from the mouse to the box (the standard picking tuto).

This ray will pass trough all objects it get into, so you just have to find your box in the collision results.

Get the contact point from the collision result and use it as the origin for the second ray. The direction of the 2nd ray is Vector3f.UNIT_Y (or - UNIT_Y if you want it downward )

@nehon said:
From your picture i figure you want the ray to be cast straight upward not down, right?

what you can do is to cast 2 rays one from the mouse to the box (the standard picking tuto).
This ray will pass trough all objects it get into, so you just have to find your box in the collision results.
Get the contact point from the collision result and use it as the origin for the second ray. The direction of the 2nd ray is Vector3f.UNIT_Y (or - UNIT_Y if you want it downward )

Well, in the pic it's just a line, to show from where the ray was coming, basically it goes trough the box (the cross, is the coordinate I actually need - no matter how much stuff is on top of the box, or below). But yeah, your idea could work, I'll try it out right now and if it solves the problem, great! (the box I drawed by the way, is just a single layer, so it always has more layers of equal size over them - the mouse basically always just touches the border, like the 'A' on the right side). Thanks!

Edit:
I'm sorry, I'm just dumb. After trying around, and around, and nearly getting to the expected result - I realized where my real problem was: I collided with the rootNode, instead of colliding with the actual layer. So all Issues I had were a result of the rootNode being larger than the actual layer, so the y data and everything else was wrong (if put on the current layer). After changing to the correct Node, everything was much easier and works as expected.

good