andy101
07-21-2005, 11:50 PM
Hello,
I am a new member on this site, but long-time viewer. I posted here hoping that somebody could help me with a problem I have with a dynamic car MEL script.
I have read Roni's post here: http://forums.cgsociety.org/showthread.php?t=194356
and decided that I wanted to do something similar. I wrote up a script that I referenced to Roni's own, but it did not work as it should have when I created it.
It is basically a script for making car wheels rotate according to the movements they make.
Here is a part of the code that I tested on a tire:
Note: Words between "<>" mean that they will be replaced with the real object/value once the object/value itself is created.
float $transX = <leftwheelname>.translateX;
float $transZ = <leftwheelname>.translateZ;
float $tls = `currentTime-q` - 1;
float $lsx = `getAttr -time $tls <leftwheelname>.translateX`;
float $lsz = `getAttr -time $tls <leftwheelname>.translateZ`;
float $relx = $transX - $lsx;
float $relZ = $transZ - $lsz;
float $hyp = sqrt(($relx*$relx) + ($relz*$relz));
float $wheelaim = asind($relx/$hyp);
//Scripting various rotation of the wheel.
//Required: An (empty) node named "CarVars"
//Turning wheels to correspond to movement of car (vertical rotation)
if (carvars.backwards == 0)
{
if ($relx <= 0 && $relz > 0 || $relx > 0 && $relz > 0)
{
<leftwheelname>.rotateY = 90 - $wheelaim;
}
else
{
<leftwheelname>.rotateY = 90 + $wheelaim;
}
}
else
{
if (carvars.backwards == 1)
{
if ($relx <= 0 $$ relz > 0 || $relx > 0 && $relz > 0)
{
<leftwheelname>.rotateY = -90 - $wheelaim;
else
{
<leftwheelname>.rotateY = -90 + $wheelaim;
}
}
}
//Turning wheels to correspond to moving the car.
if (carvars.backwards == 0)
{
<leftwheelname>.rotateX = <leftwheelname>.rotateX + $hyp*360/(2*3.14159*<wheelradius>);
}
else
{
if (carvars.backwards == 1)
{
<leftwheelname>.rotateX = <leftwheelname>.rotateX - $hyp*360(2*3.14159*<wheelradius>);
}
}
<leftwheelname>.rotateX = <leftwheelname>.rotateX;
CarVars.Backwards is a node that refers to the mode in which the car is placed: 0 means it is in normal drive mode, and 1 refers to the car being in Reverse mode.
The problem I had was that the wheels, although the rotation itself seems correct, seemingly rotate on the wrong axis, and flip-flip around instead of spinning like they should.
I would be happy and gratified beyond belief if somebody could help me, as this is my first long script in MEL/expressions, and I do not want it to turn into a nightmare! =D
Thank you greatly in advance for any replies.
-Andy
I am a new member on this site, but long-time viewer. I posted here hoping that somebody could help me with a problem I have with a dynamic car MEL script.
I have read Roni's post here: http://forums.cgsociety.org/showthread.php?t=194356
and decided that I wanted to do something similar. I wrote up a script that I referenced to Roni's own, but it did not work as it should have when I created it.
It is basically a script for making car wheels rotate according to the movements they make.
Here is a part of the code that I tested on a tire:
Note: Words between "<>" mean that they will be replaced with the real object/value once the object/value itself is created.
float $transX = <leftwheelname>.translateX;
float $transZ = <leftwheelname>.translateZ;
float $tls = `currentTime-q` - 1;
float $lsx = `getAttr -time $tls <leftwheelname>.translateX`;
float $lsz = `getAttr -time $tls <leftwheelname>.translateZ`;
float $relx = $transX - $lsx;
float $relZ = $transZ - $lsz;
float $hyp = sqrt(($relx*$relx) + ($relz*$relz));
float $wheelaim = asind($relx/$hyp);
//Scripting various rotation of the wheel.
//Required: An (empty) node named "CarVars"
//Turning wheels to correspond to movement of car (vertical rotation)
if (carvars.backwards == 0)
{
if ($relx <= 0 && $relz > 0 || $relx > 0 && $relz > 0)
{
<leftwheelname>.rotateY = 90 - $wheelaim;
}
else
{
<leftwheelname>.rotateY = 90 + $wheelaim;
}
}
else
{
if (carvars.backwards == 1)
{
if ($relx <= 0 $$ relz > 0 || $relx > 0 && $relz > 0)
{
<leftwheelname>.rotateY = -90 - $wheelaim;
else
{
<leftwheelname>.rotateY = -90 + $wheelaim;
}
}
}
//Turning wheels to correspond to moving the car.
if (carvars.backwards == 0)
{
<leftwheelname>.rotateX = <leftwheelname>.rotateX + $hyp*360/(2*3.14159*<wheelradius>);
}
else
{
if (carvars.backwards == 1)
{
<leftwheelname>.rotateX = <leftwheelname>.rotateX - $hyp*360(2*3.14159*<wheelradius>);
}
}
<leftwheelname>.rotateX = <leftwheelname>.rotateX;
CarVars.Backwards is a node that refers to the mode in which the car is placed: 0 means it is in normal drive mode, and 1 refers to the car being in Reverse mode.
The problem I had was that the wheels, although the rotation itself seems correct, seemingly rotate on the wrong axis, and flip-flip around instead of spinning like they should.
I would be happy and gratified beyond belief if somebody could help me, as this is my first long script in MEL/expressions, and I do not want it to turn into a nightmare! =D
Thank you greatly in advance for any replies.
-Andy
