Rotate Object Parallel to Underlying Surface?

Blitz3D Forums/Blitz3D Programming/Rotate Object Parallel to Underlying Surface?

Ok, I've got it started, but I can't get it finished right.

Here's the code so far.

; LINEPICK DOWNWARD

baseent=LinePick (EntityX#(Player\entityhandle,True),EntityY#(Player\entityhandle,True),EntityZ#(Player\entityhandle,True),0,-2,0)

nx#=PickedNX#()
ny#=PickedNY#()
nz#=PickedNZ#()


temppiv=CreatePivot()
AlignToVector temppiv,nx#,ny#,nz#,1
AlignToVector temppiv,nx#,ny#,nz#,3
TurnEntity temppiv,90,0,0

Player\TargetPitch#=EntityPitch#(temppiv)
FreeEntity temppiv


That works fine if the player yaw remains zero. But I need to be able to account for whatever the yaw can be because it could be anything.

I've uploaded a diagram to show what I mean.





The player is on a downward slope, the normal is the big arrow. If the yaw is zero ( we're looking side on ) then I need to rotate 90 degrees to get the right pitch. But if the yaw is 180 degrees ( pointing back up the slope ) then i need to rotate -90 degrees to get the right pitch. In both cases the roll will be zero, but if the yaw is 90 or 270 then the pitch will be zero and the roll will be 90 or -90 instead. And all the angles inbetween obviously.

What am I missing to make it work for all yaw values?