clampPoint method

Vector3 clampPoint(
  1. Vector3 point,
  2. Vector3 result
)

Ensures the given point is inside this OBB and stores the result in the given vector.

Reference: Closest Point on OBB to Point in Real-Time Collision Detection by Christer Ericson (chapter 5.1.4)

Implementation

Vector3 clampPoint(Vector3 point, Vector3 result ) {
	final halfSizes = this.halfSizes;

	_v1.subVectors( point, center );
	rotation.extractBasis( _xAxis, _yAxis, _zAxis );

	// start at the center position of the OBB
	result.copy( center );

	// project the target onto the OBB axes and walk towards that point
	final x = MathUtils.clamp( _v1.dot( _xAxis ), - halfSizes.x, halfSizes.x );
	result.add( _xAxis.multiplyScalar( x ) );

	final y = MathUtils.clamp( _v1.dot( _yAxis ), - halfSizes.y, halfSizes.y );
	result.add( _yAxis.multiplyScalar( y ) );

	final z = MathUtils.clamp( _v1.dot( _zAxis ), - halfSizes.z, halfSizes.z );
	result.add( _zAxis.multiplyScalar( z ) );

	return result;
}