Essentially that ray that I shoot down through the dozer to the ground to determine height, I am also going to determine the 'normal' (perpendicular) to the ground poly and rotate the dozer accordingly. Theoretically I should sample ALL the ground under the dozer to determine the ultimate rotation, but this will work for now.
oldrot = dozer.rotation.y;
/store the old 'y' rotation because we are going to use it later...only X and Z will change to hug the ground
intersects = ray.intersectObject(plane);
if ( intersects.length > 0 ) {
dozer.position.y = (intersects[ 0 ].point.y);
// get the face normal in object space
var vec = intersects[0].face.normal.clone();
// points up
var up = new THREE.Vector3( 0, 1, 0 );
// we want the dozer to face in the direction of the face normal
// cross will not work if vec == +up or -up, so there is a special case
if ( vec.y == 1 || vec.y == -1 ) {
var axis = new THREE.Vector3( 1, 0, 0 );
}
else {
var axis = new THREE.Vector3().cross( up, vec );
}
// determine the amount to rotate
var radians = Math.acos( vec.dot( up ) );
// create a rotation matrix that implements that rotation
var mat = new THREE.Matrix4().setRotationAxis( axis, radians );
// apply the rotation to the dozer
getRotationFromMatrix( dozer.rotation,mat, dozer.scale );
//put back to original y rotation.
dozer.rotation.y=oldrot;
}
No comments:
Post a Comment