Found incorrectly working methods .intersectBox(), .intersectPlane() for determining the intersection point of THREE.Ray

found incorrectly working methods .intersectBox(), .intersectPlane() for determining the intersection point of THREE.Ray
coordinates of origin points:
pos2{“x”: 928.7828190673121, “y”: 39.71493452167897, “z”: -940.4438369584312}



pos1{“x”: -31.45437421978416, “y”: 30.972718151985603, “z”: -0.499054374461312}


pos4{“x”: -723.9416839086465, “y”: 189.5642678652407, “z”: 763.9022176215932}


pos3{“x”: -841.2586235570504,“y”: 64.64211539068612,“z”: -902.6526891569666}


pos5{“x”: 707.1580008987936,“y”: 66.08373507906762,“z”: 839.262074939043}


function quadrilateralProjectForPlane (camera) {
let box = new THREE.Box3( new THREE.Vector3( -1000, 9.9, -1000 ), new THREE.Vector3( 1000, 10, 1000 ) ),
ray = new THREE.Ray( camera.pos.clone()),
quadrilateral = [
new THREE.Vector3( -1, -1, 1 ),
new THREE.Vector3( 1, -1, 1 ),
new THREE.Vector3( -1, 1, 1 ),
new THREE.Vector3( 1, 1, 1 )
];
quadrilateral.forEach(dir=>{
dir.unproject(camera);
engine.showLine(camera.pos.clone(), dir.clone(), ‘yellow’);//draws a yellow line
ray.direction.copy(dir).normalize();
ray.intersectBox (box, dir);
});
engine.showLine(quadrilateral[0].clone(), quadrilateral[2].clone(), ‘red’);//draws a red line
engine.showLine(quadrilateral[1].clone(), quadrilateral[3].clone(), ‘red’);
engine.showLine(quadrilateral[2].clone(), quadrilateral[3].clone(), ‘red’);
engine.showLine(quadrilateral[1].clone(), quadrilateral[0].clone(), ‘red’);
console.log(1);
return quadrilateral;
}

Could you check this line:

The value of dir is a position in 3D space. The value of ray.direction should be a direction. So, if you have a ray from point A going towards point B, the origin of the ray is A and the direction of the ray is the normalized vector of B-A. The code above uses the normalized vector of B.

1 Like

I didn’t quite understand the essence of your explanation about the ray.
But the yellow lines on the pictures are drawn along the same ray as in the provided code, following the example from the camerHelper

Are you 100% sure of this? These methods are probably among the most used methods in the library, i’d be surprised if they suddenly stopped working. There is too much sky in your images to tell whats going on with the rays. Your code is also very hard to read without indentation, syntax highlighting and this font.

I’m sorry if my comment is not clear. I will try again:

I think there is a bug in the line ray.direction.copy(dir).normalize(); because it does not calculate correctly the ray direction. As a result, wrong direction produces wrong intersection point.

I tried the intersection of a ray and a box and it works as expected. See lines 99-104 how direction is calculated. I hope this would help you fix the bug.

https://codepen.io/boytchev/full/qBQJwzW

image

5 Likes

Thanks for the explanation, until today I did not know that the direction is the difference between the end and start points.