Hello, i've implemented a ray picking function in my game (the function is not my and work like it should by returning a normalized direction vector) but i cant make it work correctly with the collision.

My probleme is that every things is fine as long as the X direction is positive and the Z position negativ. Otherwise i go strange output and the think just not working.

I've manage to see that when X i negativ the difference between what my code gave and what it should be is

"function output x" + xDirection*10 but just correcting this difference with a small if gave even weirder results.

If anyone can look at my code and tell me if he see something, it would be great. I'll share the function (every input use are accurate and the problem don't come from there, i've test them each one at a time). I'll also share you a quick example of test (input-> output) and if you want to see it, an "in game" video of the result.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| private void setRayCoord(){
float xStart = -(float)posCam.getX(); float yStart = -(float)posCam.getY(); float zStart = -(float)posCam.getZ(); double rayX = ray.x; double rayZ = ray.z;
boolean xNeg = ray.x<0; boolean zPos = ray.z>0;
Vector3f origine = new Vector3f(xStart, yStart, zStart);
System.out.println("avant : "+ray); System.out.println("CamÃ©ra : "+xStart +", "+zStart); System.out.println("=====");
ray.x *= 5; ray.z *= 5;
ray.x += origine.x; ray.z += origine.z;
System.out.println(ray); System.out.println("---------------------------------");
picker.setPos(ray.x, yStart, ray.z);
} |

Tests:

1 2 3 4 5
| Ray.x : 0.89 camera.x : 9.33 outPut.x : 13.79 Ray.z : -0.29 camera.z : 8.55 outPut.z : 7.08
Ray.x : -0.89 camera.x : 9.33 outPut.x : 4.86 Ray.z : +0.29 camera.z : 8.55 outPut.z : 10.03 |

The video: