Calculate co-ordinates along raycast


Is it possible to calculate the co-ordinates of a point say 75% of the distance between the origin of the ray (the camera) and the intersect point?

chatgpt:“for threejs calculate vector at 75% between camera and raycasted object”

// Create a raycaster
const raycaster = new THREE.Raycaster();

// Set the raycaster’s origin to the camera’s position
raycaster.setFromCamera(new THREE.Vector2(), camera);

// Check if the raycaster intersects any objects in the scene
const intersects = raycaster.intersectObjects(scene.children);

// If there are any intersecting objects
if (intersects.length > 0) {
// Get the first intersecting object
const intersectedObject = intersects[0];

// Get the position of the intersected object
const intersectedObjectPosition = intersectedObject.object.position;

// Calculate the vector between the camera and the intersected object
const cameraToObjectVector = intersectedObjectPosition.sub(camera.position);

// Calculate the vector at 75% between the camera and the intersected object
const halfVector = cameraToObjectVector.multiplyScalar(0.75);
const vectorAt75Percent = camera.position.add(halfVector);

// Do something with the vector at 75% (e.g. move another object to that position)

This line will change position of the camera. camera.position.clone().add(halfVector) will work.

Moreover, the task involves the point of intersection, not position of the intersected object. Thus, intersectedObject.point, instead of intersectedObject.object.position.

I would solve the task with let v3at75percent = new THREE.Vector3().lerpVectors(camera.position, intersectedObject.point, 0.75);

But who am I to argue with AI :thinking:

1 Like