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?
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”?
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); ?
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.
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)