View Full Version : Rotate object around arbitrary axis?
09 September 2008, 08:54 PM
Hi all again,
I've got another question which I'm sure should be easy, but I'm struggling to find an answer here or in the Maya documentation. Ok, I've got two vector coordinates which I want to use to form an axis, and then I want to rotate an object around that axis. I've supplied an image to make things clearer.
The reason why I think it should be easy is because the angleBetween command returns an axis in the form of a directional vector plus a rotation in radians. But what use is the arbitrary axis data if there isn't a command that can use it upon an object (specifically the 'rotate' command)?
Thanks in advance!
09 September 2008, 10:27 AM
how about if you use the angleBetween command to calculate the euler rotation of your axis in relation to the objects own rotation axis and use xform to set the objects rotation axis to your desired axis?
you could then rotate it along it.
09 September 2008, 10:37 AM
calculate the euler rotation of your axis.
That's the bit I'm really struggling with, in my original post I stated that I had two coordinates, but the angleBetween command uses directional vectors; your suggestion would work perfectly and it's what I'm working towards, however firstly I need to convert my two coordinates into a directional vector - and I really don't have a clue how to!
My vector math education in school was, err... non-existent.
09 September 2008, 02:21 PM
first of all, its probably simpeler to create a locator and rotate that toward your desired direction/axis and parrent whatever object you want to it and (local)rotate the locator.
for a more exact mel sollution
i have to be honest and tell you that my knowledge of linear algebra is very basic....allmost non exixtent so i will make some mistakes but maybe you get some idea's out of it.
the angleBetween command takes an -euler flagg to output 3 rotations for x y and z in degrees and it does take, like you said input from 2 directional vectors.
the problem is we don't have any vectors, we only have two vertices, we can calculate a vector between those two vertices by subtracting them from eachother then we have an directional vector for our axis.
now we only need another vector to define the base axix where we want to calculate the angleBetween from. i ASSUME you are probably wanting to know the rotation in relation to your ground planeand i also assume that the object has zero rotation to begin with, else you'll need another reference plane, so take the previously calculated vector and set its upaxis value to zero
(EX: if our previous vector was [1.2.3] we could set the upaxis (y) to 0 so the resulting vector would be [1.0.3]
i am not very wel versed in mel so regard the following as pseudocode
//somehow input or capture or select the name of the object for who you want
//its rotational axis set
string $objectName //input name here
//get input for the two coordinates, i assume you use vertices but locators are possible too
//calculate your vectors
vector $rotationAxis= `$vertex1 - $vertex2`;
vertor $baseAxis = <<$rotationAxis, 0 $rotationAxis >>;
//calculate the euler rotations to rotate $baseAxis into $rotationAxis
$rotations = `angleBetween -euler -v1 $baseAxis -v2 $rotationaxis`;
//set the rotation axis
xform -a -os -ra $rotations $rotations $rotations $objectName;
//you could also use
//select -r ($objectname) + (".rotationAxis");
//rotate -a -os $rotations $rotations $rotations;
this code will not run due to lazyness on my part, but you'll get the general idea
09 September 2008, 12:35 PM
Thank you very much, worked just as described!
I feel I should learn vector math and more advanced trigonometry, but it really bores me...
09 September 2008, 12:35 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.