Trying to have an object 'track' down another

Become a member of the CGSociety

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

Thread Tools Search this Thread Display Modes
  02 February 2004
Trying to have an object 'track' down another

Premise: Have one object (CTRL_Car) which will 'drive' the second object Car.

Problem: Script doesnt seem to fully execute/run on each transform of CTRL_Car. Any Ideas?

Currently I'm trying to:
1) Set Inital variables for each objects local positions
2) On a move of the CTRL update the second set of variables and calculate the distance ($distance) between them.
3) Rotate the Car towards the CTRL_Car (done currently always with an AIM contraint, later will modify its weight to make more realistic)
4)Move the car $distance towards the CTRL
5)Reset the orignal x/z positions for the next time

Current Code:

//Del values of Car and its Controller
float $carctrl[] = `xform -q -os Car`;
float $CTRL_pos[] = `xform -q -os CTRL_Car`;
$carctrl[0] = 0;
$CTRL_pos[0] = 0;
$carctrl[2] = 0;
$CTRL_pos[2] = 0;

float $distance = 0;

//Distance Between Car and Car Controller
float $test = 0;

//Update Car Controller's position with latest translation.
$CTRL_pos[2] = CTRL_Car.translateX;
$CTRL_pos[2] = CTRL_Car.translateZ;

//Calculate Distance
float $deltaX = $CTRL_pos[0] - $carctrl[0];
float $deltaZ = $CTRL_pos[2] - $carctrl[2];
float $tempX = `pow $deltaX 2`;
float $tempZ = `pow $deltaZ 2`;
$distance = sqrt($tempX + $tempZ);
move -r 0 0 $distance Car;

//Reset Distance moved
if ($distance != 0)
$carctrl[0] = $CTRL_pos[0];
$carctrl[2] = $CTRL_pos[2];
  02 February 2004
Because Maya doesn't see the xform -q or move commands as equivalent to a connection, it doesn't know when it has to execute the expression.

This is a big problem with using MEL commands in the middle of expressions. You can set the expression's "always evaluate" toggle on, which may help (though there are certain circumstances under which even this won't solve the problem completely.)

An alternative is to query the control object's translateX and translateZ by assigning them to a local variable (rather than using the xform command), and assign new position values directly to tx and tz rather than using the move command. This will make your updating work out better.

Here's my version of your expression:

float $distance = 0.1;

vector $ctrl_direction = <<locator1.translateX - tx,
                           locator1.translateZ - tz>>;

vector $norm_ctrl_direction = $ctrl_direction / mag($ctrl_direction);

vector $move_vec = $norm_ctrl_direction * $distance;

tx += $move_vec.x;
tz += $move_vec.z;

"But," you say, "the expression has to have the node name of the control object available in it!" Yes, it does. The solution to this is to build a small MEL script that you use for setting up the car/control relationship. This MEL script will look at the name of the control object you've selected and use the expression command to make the above expression.

Another alternative which I *MUCH* prefer if you're going to have a lot of these things, because it will be much, much faster and much simpler to set up, is to use particle dynamics. You can set up a particle object whose particles follow per-particle goals around just as you've done with your expression, then use geometry instancing to put a car at each particle. Not only will this be faster, but it will also save memory if you have multiple of the same car in the scene.

-- Mark
Mark R. Wilkins
author of MEL Scripting for Maya Animators
  01 January 2006
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
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 03:17 PM.

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