MovingEntity(lineOfSightTest)方法是Yuka js库的一部分,用于测试两个实体之间是否存在可见路径。该方法使用Bresenham算法,可计算两个点之间的最短路径。此方法返回一个 Boolean 值,true表示实体之间存在可见路径,否则为false。
entity.lineOfSightTest(targetEntity, obstacles)
import { MovingEntity } from 'yuka';
const entity = new MovingEntity();
entity.position.set( 1, 1, 1 );
entity.setBoundingRadius( 1 );
entity.setMaxSpeed( 5 );
const targetEntity = new MovingEntity();
targetEntity.position.set( 10, 10, 10 );
targetEntity.setBoundingRadius( 1 );
const obstacles = [ new THREE.Mesh( new THREE.BoxBufferGeometry( 2, 2, 2 ) ) ];
obstacles[ 0 ].position.set( 6, 6, 6 );
if ( entity.lineOfSightTest( targetEntity, obstacles ) ) {
console.log( 'There is a visible path to target entity!' );
} else {
console.log( 'There is no visible path to target entity!' );
}