Search code examples
javascriptthree.jsraytracing

Three.js - how to find closest object along a line


I am using Three.js to simulate and visualize a robot. I need to allow this robot to detect obstacles; specifically it needs to return the distance to the closest obstacle straight in front of it. How could said functionality be implemented in Three.js? I need to essentially "look" along a given ray and find the closest object that intersects it.


Solution

  • You can use the raycaster built into Three.js: https://threejs.org/docs/#api/en/core/Raycaster