PDA

View Full Version : Not Working like it should...


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

andy101
07-22-2005, 06:05 PM
i have discovered the cause of this, and i really need help fixing it...

the problem is not at all with the script, but with the rotation itself. if i use just one axis of rotation, the script works fine. But when i introduce the other, things get weird. The x-axis rotation turns not only the tire itself, but also its y-orientation, making the y-axis spin along with the tire.

When i add the y-axis rotation, it spins along the LOCAL y-axis instead of the global one, which causes all the aforementioned problems.

Basically, im asking this: is it possible to have the tire spin on a local x-axis and a global y-axis?

Again, thanks in advance for any posts, im glad I finally narrowed down the problem.
-Andy.

claforte
07-23-2005, 01:45 AM
You could try changing the rotation order on the transform.

Christian Laforte
Maya API Consultant
http://www.feelingsoftware.com

andy101
07-23-2005, 10:42 PM
could you elaborate?

Do you mean rotate Y and then rotate X?

The thing is, this script executes every frame, and hence the rotate Y axis will have moved. Even if i change rotation order on the first frame, the following frames will have the same problem.

CGTalk Moderation
07-23-2005, 10:42 PM
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.