How to achieve the effect like Path Constrain using MAXScript?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Display Modes
  03 March 2009
Wink 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!
  03 March 2009
That is just a random shot now, but I think you might be able to do that with a Rotation Script Controller, where you specify the Orientation Vector by the difference of the Position Vector of the current frame and the Position Vector of an earlier frame.
  03 March 2009
Check out Shape Common Properties in the help.

You can use lengthTangent to get the directional vector for the tangent at a certain percentage along a spline's length. Use this to then generate a rotation value.

You can also use lengthInterp to get the position value of a certain percentage along a spline's length.

Hope that helps.
  03 March 2009
Red face Thank you

Thank you for your reply.

But I don't want a spline. I just wanna move the car manually not using a spline.
  03 March 2009
so basically you'd want the equivalent of...
1. going into trajectories mode
2. converting the trajectory to a spline
3. setting that spline as a path constraint with 'follow' on
4. bake the rotation out to actual keyframes

(I, too, thought he just meant he didn't want to use a path follow constraint - while still following a path, leading to this script controller >_<

nearestParam = nearestPathParam spl 1 obj.pos steps:200
obj.pos = pathInterp spl 1 nearestParam
obj.dir = pathTangent spl 1 nearestParam

  03 March 2009
I think I know what you are going for, and I have done it before on my reel (you can check it out here, near the end of the reel:

What you want is to get he motion of the object, and based on it's previous position get the directional vector. From this directional vector you can get the proper rotation of the car.

f = currentframe
LastF = currentframe - 1
dirVec = normalize ((at time f $.pos) - (at time LastF $.pos)

This will basically get you the direction, and then you just need to normalize the entire matrix through consecutive cross products.

Hope it helps.
  03 March 2009
Ah okay, I didn't realize. I guess I paid too much attention to the subject line.

In that case, Phillip and Dimitry have provided some very good approaches. I would go down that path. (pun intended?)
  03 March 2009

Thank you for your answers. Could you show me more details about your method? ĎCause I still donít get how to turn your method into an executable MAXScript.
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.

  03 March 2009
No, not a script...a 'Script Controller'

Go to the Curve Editor or the Motion Pannel and assign a 'Rotation Script' to the Rotation of the object. In there you write the code.
  03 March 2009
Red face

Originally Posted by Piflik: No, not a script...a 'Script Controller'

Go to the Curve Editor or the Motion Pannel and assign a 'Rotation Script' to the Rotation of the object. In there you write the code.

Yeah,thank you!
But what does the Script should be?
Could you show any detail?
  03 March 2009
Sorry for taking so long. I think I might post a small tutorial on my blog in the future just for the hell of it. But basically I make it in a way where I have a Custom attribute on the object that is rotating. The code for the attribute is this:
CustAttr = attributes RotVal 
   parameters RotValP 
      Rot type:#float default:0 
custattributes.add $.baseobject CustAttr

When it comes to the actual script controller code, this should be it:
if currentTime == 0f do 
Obj.RotVal.Rot = 0 --Obj should be a variable added in the Script controller pointing to the object that holds the custom attribute
p1 = at time currentTime Obj.pos 
p2 = at time (currentTime-1) Obj.pos 
dist = distance p1 p2
cir = (2*pi)*Obj.radius
ang = dist/cir
Obj.RotVal.Rot += degtorad(360*ang)

Sorry for taking so long, busy days at work
  03 March 2009
Red face

Thanks for your reply during the busy work!

But I'm cofused at this place:
cir = (2*pi)*Obj.radius

What does Obj.radius mean?
I guess that it's the radius of the object's trajectory,but there's no such property for a node as far as I know.

And using your method the object always rotates for a plus number,but sometimes it may rotate for a negative degree like -70.
So how do we get that the angle of its rotation is a plus number or a negative one?
  03 March 2009
O man, hahahaha, I completely messed this one up So sorry man, I posted a completely wrong part of what I wanted to show. This is for the wheel rotation, not for the actual object rotation. Sorry about that. So here goes my second attempt
if currenttime != 0f then --I prefer to have the rotation neutral at frame 0
     YVec = normalize (Cnt.pos - (at time (currenttime-1) Cnt.pos)) --get normalize
     ZVec = [0,0,1] --this is needed for a temporary up vector calculation
     XVec = cross YVec Zvec --cross product of Y and Z gives us X
     ZVec = cross XVec YVec --cross product of X and Y gives the orthogonal Z
     tm = (matrix3 XVec YVec ZVec Obj.pos) -- this is our objects transform
     tm.rotationpart --get the rotational part
     quat 0 0 0 1

What I gave you was basically another part of having a car set-up, which would be the wheel rotation. Once again sorry shame on me! Anyways, this should do it for you

EDIT: In fact, lemme attach the max file so that you can see it in action! (Its in Max 2k8 format)
Attached Files
File Type: zip (13.6 KB, 57 views)

Last edited by Dimich : 03 March 2009 at 10:25 AM.
  03 March 2009
Thumbs up

Thank you!

I'm learning your method which looks very cool.
  03 March 2009

I got it, man!
The Vector Cross Product is very cool!
And I've also understood the meaning of Matrix3!
Thank you!
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 10:37 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.