Collision with ray picking problem.

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.

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;
		
		//ray.x += (xNeg)?rayX*10:0;
		//ray.z -= (zPos)?rayZ*10:0;


		System.out.println(ray);
		System.out.println("---------------------------------");

		picker.setPos(ray.x, yStart, ray.z);

	}

Tests:

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:

wy4wHkxR6ZQ

Up on this topic pleas. I’m still stuck with this problem, i can"t figure it down.

I am sure people would really like to help you out on this, but from your post it is not clear what your problem actually is.

You need to be more specific about your problem and explain in more detail the following points:

  • what do you mean with: you cannot “make [ray picking] work correctly with the collision.” What collision? Who collides with whom?
  • it is not obvious what you want to achieve with negating your camera’s position (mirroring it about the origin of the coordinate system), if that is what “posCam” contains
  • it is also not obvious, what “ray” contains and why one wants to check for it being negative at all.
  • also, why are you scaling “ray” (and only x and z) by 5?
  • and last but not least, why do you offset your “ray” which ought to be a direction (I guess) by your camera’s position, which ought to be a position?

Also, from your video it is not obvious what the wireframe box should be and what you expect to happen.
Currently, I do not see anything wrong with your camera rotating about some point near that box. :slight_smile:
Looks nice, by the way!

Cheers, Kai

Hi

The emergency is for you, not for us. We do our best but we owe you nothing except respect.

I use “ray picking” in ArdorCraft, you can look at how it is implemented to see what is wrong in your own code:
https://github.com/gouessej/Ardor3D/blob/master/ardor3d-core/src/main/java/com/ardor3d/bounding/BoundingBox.java#L666

i’m pretty sure we could help you if we’d understand what you’re trying to do. :slight_smile:

  • projecting screen coords to a ray in 3D ?
  • ray-AABB intersect ?
  • ray-triangle intersect ?

Hello, tanks for responding so quick. First i apologized if i offend you in some way, english is not my native language and i may have exprime myself wrong. I’m really thankful for the help any of you can provide to me.

To clarify my problem, i’m trying to implement ray picking to my (of course) 3D project. For what i understund to ray picking (so that what i’m trying to do) is to compute a ray (a vector) starting to the camera and heading to the distant point le camera/player is looking.

So i first need to calculate the direction of where the camera is looking (which is made with a function i took on a active membre of this forum and wich return a normalized vector x,y,z of the direction).
Then i take the position of the camera in my 3D space, this is pretty easy of course. And according to the mathematical function i find online, i try to find a second point on that ray further away. The function i find is ray*distance+origine.

This is the part that is not working. When i use the terme “collision” i meant colliding in the 3D space at the distance i want (5 blocks / units in the code i shared). The result i have are pretty awkward and i realy not sure why it’s doing that. The short video i share show how it’s almost working.

Once again sory for my bad english i doing my best, and thanks again for all the help you could provided. I don’t want to be rude, i’m just stuck whit this for while and i’m a little desperate :p.

Edit: in my video we can the the wired cube moving with my cursor but it should always been center with it. Also i currently not working on the Y axis because it les problem and once it will work on the other axis it will be easy to apply it on Y.

I want to finish with this : i have almost no idea of what i’m doing, my brain usually stop working properly want it comes to math.

Also, for what i’ve tried so far, it’s seems to be math(or trigonometry which is almost the same)-only problem. But i’m not sure, i’m not very skilled in openGL ether.