Author Topic: Rotation Help  (Read 1017 times)

I need help with a small cable mod that I'm making for ship rigging etc. The wand model seems like a good auxiliary model, so I am using it. I am also using a simple output event right now because it's easy to test with, but eventually there will be sender/receiver bricks or some other system. Anyway, I need help with rotating the correctly sized and positioned cable StaticShape to connect the %this.position and %targetPos.

Code: [Select]
datablock StaticShapeData(CableData) {
shapeFile = "base/data/shapes/wand.dts";
};

function fxDtsBrick::createCable(%this, %string, %client) {
%targetBrick = ("_" @ %string).getID();

if(isObject(%targetBrick) && %targetBrick.getClassName() $= "fxDTSBrick") {
%targetPos = %targetBrick.position;

if((%cableLen = VectorDist(%this.position, %targetPos)) > 1) {
%cableShape = new StaticShape() { datablock = CableData; };

if(isObject(%cableShape)) {
for(%i = 0; %i < 3; %i++)
%mid = %mid SPC ((getWord(%this.position, %i) + getWord(%targetPos, %i)) / 2);

%currentNormal = VectorNormalize(%cableShape.position);          // BLATANTLY loving WRONG
%targetNormal  = VectorNormalize(%targetPos);                         // BLATANTLY loving WRONG

%angle = mAcos(VectorDot(%currentNormal, %targetNormal));    // BLATANTLY loving WRONG
%axis  = VectorCross(%currentNormal, %targetNormal);              // BLATANTLY loving WRONG

%cableShape.setScale("1 1 " @ (%cableLen * 0.55));
%cableShape.setTransform(%mid SPC %axis SPC %angle);

MissionCleanup.add(%cableShape);
}
}
}
}

registerOutputEvent(fxDTSBrick, "createCable", "string 20 250", 1);

My initial idea (represented by the picture below) favored the axial rotation system (as opposed to Euler rotation angles) and was to create a vector B from the src and receiver and then create a perpendicular vector A of which to rotate around. I quickly realized that there could be an infinite number of perpendicular vectors which renders this solution moot.


I haven't done this kind of math in quite a while and am rusty... any help? Am I being stupid?

Well firstly, someone already made and released something similar
Secondly, I haven't read your code yet, but why do you have lines in there that are "blatantly loving wrong"

Edit: this

Edit: this
That uses emitters. This does not. He's aware that it has been done with emitters.

Well firstly, someone already made and released something similar
Edit: this
You can't walk on those, this is much less laggy on stuff computers and you will be able to save these into your build.

datablock StaticShapeData(CableData) {
Arrrrrrrg
Stop that ew format

datablock StaticShapeData(CableData) {
Arrrrrrrg
Stop that ew format
no

That uses emitters. This does not. He's aware that it has been done with emitters.
You can't walk on those, this is much less laggy on stuff computers and you will be able to save these into your build.
I'm aware of this. You said you were having problems with math, I linked to it so you can compare the math. Did you ignore my second question, as well?

How could there be an infinite number of perpendicular vectors?
To get vector A, I think you'd take the cross product of B and the vertical vector. I think you're getting those two vectors the wrong way, though, looking over %currentNormal and %targetNormal. You'd get B by subtracting the start position from the end position (final - initial), then normalizing it to get a unit vector. The one you're crossing it with would be a vertical unit vector, "0 0 1." I think the amount you'd need to rotate would be based on the dot product of those same vectors. Not sure exactly how you get from that product to the 4th axis rotation value, because I never actually read up on that system. mAcos may be it, looking over your code.
« Last Edit: October 27, 2012, 06:39:05 PM by -Jetz- »

I linked to it so you can compare the math. Did you ignore my second question, as well?
The math is nothing the same.

I didn't ignore it, I thought it was obvious and redundant. He wanted us to know which lines he needed help with, so he wrote that those were the lines with problems (since the others worked).

The math is nothing the same.
I don't see how looking at the math for the other mod would hurt in any conceivable way.
« Last Edit: October 29, 2012, 09:28:43 AM by Treynolds416 »

I don't see how looking at the math for the other mod would hurt in any conceivable way. Secondly,
The way things are done from this mod and the linked one are completely separate. Anyway, I've figured it out. Basically, you find the pitch by taking the inverse sin of the target height/cable length. You then find the yaw by taking the inverse tangent of the x and y components. After that, you build a matrix and rotate.
« Last Edit: October 28, 2012, 10:43:26 PM by dotdotcircle »

How could there be an infinite number of perpendicular vectors?

Perpendicular - a new component at a 90° angle to all given components.

In 2D space with one component, there are two perpendicular vectors for every one vector. In 3D space with only one defined vector, there are a infinite number of perpendicular vectors (for a vector of either "0 0 1" or "0 0 -1", any vector with no up/down component is perpendicular.)

Perpendicular - a new component at a 90° angle to all given components.

In 2D space with one component, there are two perpendicular vectors for every one vector. In 3D space with only one defined vector, there are a infinite number of perpendicular vectors (for a vector of either "0 0 1" or "0 0 -1", any vector with no up/down component is perpendicular.)
But to get a perpendicular vector in 3D space, he used a cross product, which requires 2 input vectors.

But to get a perpendicular vector in 3D space, he used a cross product, which requires 2 input vectors.

Yes, which still leaves you with TWO potential perpendicular vectors which are equally viable.

Yes, which still leaves you with TWO potential perpendicular vectors which are equally viable.
It isn't random which one it gives you, though. If it applies the rotation in the wrong direction, just swap the two parameters and it should be right for all future cases.