Moving the Spider over rough terrain -- Surface Alignment
Function GuideToSurface()
As the guide object moves forward, a 3 stage alignment procedure takes place;
1. A tape object is aligned to the GuideBody object. The tape creates a ray that points down through the Z_Axis of the Sider into the Ground object. A maxScript intersectRay function is used to find the position of the intersection of the Tape Ray and the Ground Object. The GuideBody object moves to that spot.
2. Now the tape object is aligned to the front of GuideBody object and once again intersectRay finds the intersection of the Tape Ray and the Ground Object. Now for a little math, with the forward intersect spot known we can determine the relative offset angle needed to rotate GuideBody object to point towards the forward intersect spot.
3. One last time the tape object is aligned to the side of GuideBody object and once again intersectRay finds the Tape Ray and the Ground Object. Now for a little math, with the side intersect spot known we can determine the relative offset angle needed to rotate GuideBody object to point towards the side intersect spot. With these 2 rotations; X_Axis for the Forward Spot and Y_Axis for Side Spot, we can achieve near perfect alignment to the Ground Object surface normal. Each new alignment is relative to the last rotation so this method will create smooth rotation tracks without Euler flipping.
 
Page 7