Ray casting

I am trying to use ray casting with the mouse cursor to be able to select items on the screen. I am following the tutorial found here


Near the bottom it shows about using the mouse cursor. I am doing this and have an ActionListener but it does not seem to be working. I even tried printing out the name of the object but nothing shows up on the output. I am definitely clicking on it so I think it is not getting to the code. Any suggestions? Would you like to see the code?

i have the ray cast working now but it makes the object disappear. i am trying to make it so when the mouse button is held down, you can move the object to a new spot.

You’re going to have to show us your code to solve this.

Also, try putting in some logging that displays the object’s location when you move it. It might turn out that it is moving somewhere where you aren’t expecting.

for raycasting to work it has to “collide with something”, if it doesnt collide it wont work.

in your example you will move the object from his position to Math.Inf.

Solution : Create a plane with size [1000,1000]. It will always collide thus allow you to move it correctly.

Ok. So basically i was using the wrong numbers for how to move it. I was using the x and y of the cursor which is in the pixels of the screen so i was moving it to far away from where I was. Any suggestions on what numbers I should use to move the box with? Here is the code.

[java]private AnalogListener clickListener = new AnalogListener(){

public void onAnalog(String name, float value, float tpf){

if (name.equals("Click")){

CollisionResults results = new CollisionResults();

Vector2f click2d = inputManager.getCursorPosition();

Vector3f click3d = cam.getWorldCoordinates(click2d, 0);

Vector3f dir = cam.getWorldCoordinates(click2d, 1).subtractLocal(click3d);

Ray ray = new Ray(click3d, dir);

rootNode.collideWith(ray, results);

if (results.size() > 0){

Geometry movable = results.getCollision(0).getGeometry();

movable.setLocalTranslation(new Vector3f(inputManager.getCursorPosition().x, inputManager.getCursorPosition().y, movable.getLocalTranslation().z ));




I cannot find a good method i can use to get a number i can set the translation of the box to move with the mouse. Hmmm.

Thanks for your help guys.

That’s not code, that’s lines of text that can barely be read. Please use the “java” tag button to paste formatted code. Makes things MUCH easier for everyone to help. Without this, not many people will even bother.

is there any documentation for the inputlistener? i try to use actionlisterner and it doesnt seem to be implemented in the same way as shown in the tutorials.