# How to achieve the effect like Path Constrain using MAXScript?

Hello everyone!

I wanna animate a car.

I want to make the keys of position manually, and generate the car's rotation automatically.

I mean, first turn on Auto Key, second move the car to generate position keys, then the car rotates to follow the trajectory automatically, which is just like the effect of Path Constrain.

How can I achieve this?

Thank you!

Longing for answer!

## Comments

## Thank you.I believe I can

Thank you.I believe I can work this out!

## I got it that your method is

I got it that your method is to get the derivative of the position-time curve. But what if the object only moved at Z-axis so its rotation should always be the same.And then if I use your means,it should rotate for some angle,which is not correct.

So in my opinion, we may get the derivative for the trajectory of the object,which is the direction of the object.

But how can we achive that?

## you're right, there is a

you're right, there is a final step to make.

The angle I got from the derivative of Z is to the XY-plane (so the solution-set is a line on a cone).

Combine this with the other 2 derives to get a {yz,xz,xy}.

I'll have to think about this final step.

Studio3DR.nl

## now that i see the numbers,

now that i see the numbers, i realize i was mistaken in my 1st post... it's easier.

The rotation of x (or y or z) in world coord only depends on x-position. More specific, its the derivative (tangent-line) of the positiongraph.

I drew in 5 of these lines for Z. (i assumed you have bezier-interpolation)

You should take the value of the 2 neighbouring keys and devide the difference by 2.

This will get you the tangent.

for Z in frame 50 :(49, 18.135) (51, 21.585)

(21.585-18.135)/2= 1.725.

So z-rotation = ArcTang [1.725] = 59.9

In 33,66 and 99 it is 0 (although this methode will give a very small angle at 33 and 99).For frame 82 or 83 you'll get a simulair angle.

If you set these 5 keys values with bezier-interpolation again, you'll get a very natural angle.

The same works ofcourse for x and y.

Hope this helps you on the way ;)

Studio3DR.nl

## Thank you for your

Thank you for your answer.

And here's my script:

-- create an object

obj = box width:17 length:42 height:14

setTrajectoryOn obj true

-- animate the object's position

animate on

(

at time 33 obj.pos = [50,100,0]

at time 66 obj.pos = [120,13,38]

at time 99 obj.pos = [100,-24,0]

)

-- automatically rotate the object

......(script that I don't know how to achive)

You can see I wanna rotate the object to follow its trajectory.

This is what I wanna do.

## that's not too much

that's not too much information you're giving us there, but maybe these positionpoints is all the info there is.

In that case you will have to do some math on paper.

I can imagine that at half-time between 2 positionkeys, you'd like the car to drive in the direction of the next position, and at the time of each positionkey you want an average of the 2 halftime-directions...

getting those positionskeys (both value and time) and addNewKey in the middle isnt your biggest problem.

Its calculating the resulting path... if this is realy what you want, i can try to help you, but maybe there are other ways. Im guessing your car is on a road for instance... you could obtain its rotation from the path you used to build the road (combined with other things)

please tell us a bit more on what you're trying to do.

Studio3DR.nl