Heh, I've been meaning to attempt to write some similar code for future games. You've saved me some work.
I noticed a little problem though. Don't know if this happens to you, but the object will pitch in the wrong direction. Easily fixed though.
Just need to change this:
pitch#=asin((front#-back#)/l#)
To this:
pitch#=asin((back#-front#)/l#)
I also put it into a quick function to use with any object and matrix.
function orient_object(OBJECT, MATRIX)
rem before main loop
rem create constants
` object length
l#=object size z(OBJECT)
`object width
w#=object size x(OBJECT)
rem in main loop+++++++++++++++++++++++++++++++
rem define variables and position object
rem theta# is heading
ox#=object position x(OBJECT)
oy#=object position y(OBJECT)
oz#=object position z(OBJECT)
oy#=get ground height(MATRIX,tx#,tz#)
position object OBJECT,ox#,oy#+20,oz#
theta#=object angle y(OBJECT)
rem tilting
rem work out points : left, right, front, back, centre
sin_heading#=sin(theta#)
cos_heading#=cos(theta#)
fx#=ox#+((l#/2)*sin_heading#)
fz#=oz#+((l#/2)*cos_heading#)
bx#=ox#-((l#/2)*sin_heading#)
bz#=oz#-((l#/2)*cos_heading#)
lx#=ox#-((w#/2)*cos_heading#)
lz#=oz#+((w#/2)*sin_heading#)
rx#=ox#+((w#/2)*cos_heading#)
rz#=oz#-((w#/2)*sin_heading#)
front#=get ground height(MATRIX,fx#,fz#)
back#=get ground height(MATRIX,bx#,bz#)
left#=get ground height(MATRIX,lx#,lz#)
right#=get ground height(MATRIX,rx#,rz#)
roll#=wrapvalue(asin((right#-left#)/w#))
pitch#=wrapvalue(asin((back#-front#)/l#))
rem make sure roll# is between 1 and 360
xrotate object OBJECT,pitch#
zrotate object OBJECT,roll#
rem end of loop+++++++++++++++++++++++++++++++++++++
endfunction