Notifications
Clear all

[Closed] face object in direction of travel of other object in real time

After few days of struggling with this I need a little help.
I create rig for crab, legs are binded and follow spline path which is
wired to movement of master so when I move master legs also animate along spline shape
. Now I want to rotate this spline path depending on direction of movemant of master object
so the legs would be always faces in direction of movement of body.
I need this in real time as controller or or maxscript function.
I tried to use algorithm below and it works fine in timeline
but when I calculate in realtime, rotation get crazy and object easily lose orientation.

I also tried with different solution form forum and net but nothing seems to work in practice.
Is there way to do this?

global ticks=0
fn update_data_transform crabmaster =
(
ticks+=1
now = $Teapot001.pos

if(ticks > 1) then

(
dirVec = normalize (now – last)
–ang = asin(dirVec[2]/50)

dir = atan (dirVec.y/dirVec.x)
if dirVec.x < 0 and dirVec.y > 0 then dir = 180+dir
if dirVec.x < 0 and dirVec.y < 0 then dir = -180+dir
if(ticks > 2) then
(
ang = dir-lastdir
rot_obj = eulerangles 0 0 ang
)
else rot_obj = eulerangles 0 0 dir
rotate $Teapot001 rot_obj
)
global last = now
global lastdir = dir
)

fn DirectionOfVector vect =
(
dir = atan (vect.y/vect.x)
if vect.x < 0 and vect.y > 0 then dir = 180+dir
if vect.x < 0 and vect.y < 0 then dir = -180+dir
dir
)
maw=$
when transform $ changes do update_data_transform maw