View Full Version : Trying to have an object 'track' down another

02 February 2004, 03:39 AM
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, 12:52 AM
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

CGTalk Moderation
01 January 2006, 09:00 AM
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.