[SOLVED] Ray casting question

Hello Guys,
I have this code for ray casting from mouse pointer:
Vector2f click2d = inputManager.getCursorPosition().clone();

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

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

    Ray ray = new Ray(click3d, dir);
    g.node.collideWith(ray, results);

Now, I want to modify it so instead of the mouse pointer I have a spatial in somewhere on the screen.
What would you suggest as best practice for doing it?


but you want this spatial in GuiNode or RootNode?

**instead** of the mouse pointer

this mean you want it in GuiNode, so you dont even need Raycast, you just position based on click2D.

but if you want in rootNode, then is also depends you want it on some Ground or some distance from camera at cursor point. (in first of this case you need ray, in second just dir)

so i cant tell since im not sure what you want exactly to do. i just suppose you want replace mouse pointer with spatial so its first option.

so generally my question would be “what is mouse pointer for you”?

My spatial is in the root node. What do you mean by “need some Ground”?

before anything, you need explain " I want to modify it so instead of the mouse pointer I have a spatial in somewhere on the screen."

what is mouse pointer for you? i understand its cursor on screen, its in 2D position. So you want this spatial in 2D space or 3D space?

My spatial is in 3D space

My spatial = mouse pointer ?

Yes, instead of using the mouse pointer, I want to do ray casting from some spatial in 3D space

i think i know what you mean, at least i hope.

if you want do Raycast Beginning from Spatial, you just need

Vector3f beginLoc = spatial.getWorldTranslation();
Vector3d dir = new Vector3f(1,0,0) // some direction, you need to know which direction you want. 
// this dir can be Front of spatial or something.
// for example dir can be dir = enemy.getWorldTranslation().substract(spatial.getWorldTranslation()).normalize()

Ray ray = new Ray(beginLoc , dir.normalize());
g.node.collideWith(ray, results);

OK, thank you. I think I understand. And if I just want it to check ray casting straight ahead is it OK to use this dir: spatial.getWorldRotation().getRotationColumn(2); ?

well im weak in Quaternions, but what do you expect with spatial.getWorldRotation().getRotationColumn(2);?

you can use it because its vector, but im not sure what do you expect to have.

OK, thanks. I’ll try and report

1 Like

Personally. I prefer spatial.getWorldRotation().mult(Vector3f.UNIT_Z)… because it’s clearer what is going on without relying on weird Quaternion->Matrix transform magic.

Also if you ever decide to use a different relative vector it’s really easy.

Actually I’m using it that way. I found Quaternion->Matrix transform magic in the camera source code BTW :slight_smile: in the getDirection function…

I’m not sure what do you mean by “relative vector”. I’m willing to try anything which is best practice

He means that if you want to Check for left and right you can just enter UNIT_X. Or Y for UP

Or any other random vector you like.

For example, if this was an animated character and you wanted to check their “eye line” or whatever that may not be in a standard direction.

1 Like

Trouble solved?

Yes, Thanks a lot for this awesome forum.
Now I can perform shooting through my sight to the enemies and it works as expected (as shown in the below video)

1 Like