Ray Picking

When I fire a Ray, it doesn’t always pick the object at which I aim. Is this because it tests rays’ collisions with the other objects’ edges and not faces?

Yeah. That’s inevitable with floats.

Unless there’s a decision to switch to doubles there isn’t much we can do I’m afraid.

Maybe if we push hard enough floats will be dropped and replaced by doubles. :smiley: that’d make my day.

It’s a nightmare from 30z - 40z away :frowning: You shoot at things and they don’t get hit.

Yes, it does tests for faces, or else it wouldn’t make sense. Maybe how you are pointing/constructing the ray you are getting a missdirection.

Depending on distance you might also get precision loss.

Yeah. When I’m close, everything works fine. But when I’m farther away (10z away), it’s less precise.

“I’m holding this piece of string. It is red and frayed at one end. Can anyone tell me how long it is?”

It would help if we could see some of your code. Picking should be at least pixel accurate (if you aren’t picking then that would also be useful information).

There could be any of a dozen things going wrong and it would be faster if we didn’t have to guess all of them until one magically fits your code. :slight_smile:

Technically there should be a way to compensate for precision loss, but honestly I’ve never looked into it. Maybe there’s something out there…

If you do find something, please share. :smiley:

@pspeed, what code, exactly? There’s not much which could be going wrong. I’m trying to scale everything down.

How do you construct your Ray? How are your objects positioned? Are they animated? Do their meshes change in any other way? Is there anything that would cause their bounds to change?

Can you boil it down into a simple test case?

From my perspective, you are doing something wrong. I pick a single pixel of an object from as far as 30 meters away without issue. We cannot tell what is wrong though because we have no information.

Maybe the rays you create are wrong somehow? I can pick objects fine down to a pixel size on screen using bullet.

I’ll try a test case then.

Strange, from 50Z out, it works well. Must be something with my ray origin and direction then. Sorry for bothering you.

@pspeed said:
From my perspective, you are doing something wrong. I pick a single pixel of an object from as far as 30 meters away without issue.

What's your scale? 30 meters = ? floats.

I know that I'm getting slight weird results at +25 000 floats from the camera. The farther, the worse. I guess though I'm a bad example as the my scale is pretty humongous.

30 meters = 30f. Just like the original poster who is having problems at greater than 10. These are not big values. It is not related to precision.

Oh wow. Ok… Yeah. I agree. 30f shouldn’t be an issue at all.


The red things is the path of the ray. However, it's as if the yellow sphere is not being hit. In the log, it should show that it was hit, but it doesn't. Any help?

[java]results = new CollisionResults();
Ray ray = new Ray((new Vector3f(Main.Mgeom.getLocalTranslation().x,
new Vector3f(Main.setup.cross.getLocalTranslation().x,

This is how I initialize the ray. In the following picture, it might not show up well, but there's a small yellow sphere near the space craft and another one in the middle of the blue cross hair. These show the origin and direction of the ray respectively. Everything should work well, but it doesn't. Any explanations?


If you do not normalize your direction then I think you get very strange results.

What do you mean by normalize? What code do I use?

Actually, I can’t figure out exactly what you are passing to the Ray’s constructor:

Ray(Vector3f origin, Vector3f direction)