# Cross product = 0 and rotation direction

Hello guys, when I use the cross product of two vectors to get the rotation axis, and the two vectors are parallel, well the cross product is equal to 0 ans the rotation fails (my object is rotated in the wrong direction.)

do you guys have a simple solution to this problem??

thx

ERm… what?

Could you maybee try to explain not what you need solved, but what you try to do with it?

crossProduct = Vector3f.UNIT_X.cross(getDirection());
myAngle = Vector3f.UNIT_X.angleBetween(getDirection())

crossProduct is equal to Vector3f(0,-0,0) when getDirection() return -Unit_X (Notice the MINUS 0)

so when i do this :

myQuaternion.fromAngleAxis(myAngle, crossProduct);

myQuaternion.mult(myObject) <— myObject is rotated in the wrong direction, because the quaternion doesn’t take into account the -0, instead it considers the crossProduct to be equal to Vector3f(0,0,0), and This is bad, because it affects the clockwize and anti clockwize direction of the rotation. (negative is clockWize i think)

What are you trying to do at a higher level than that? Are you trying to just rotate around the y axis or something?

Otherwise, it seems odd to pin rotations to just some random plane that happens to be parallel to the x axis… but I’ll admit I just woke up.

I don’t know in advance the axis of rotation, in most cases its YAXIS, I Use the XAXIS as a reference for all my rotations. and I only get a problem when my direction is equal to -XAXIS a solution that works is to check if the crossProduct is equal to (0,-0,0) and make it equal to (0,-1,0)

but its a really stupid solution, I was hoping for a real solution to the crossProduct rotation problem If you explain at a high level what you are trying to do, like:
“I’m trying to make a cow go to the grass by…”
“I’m trying to make a ship fly into a planet by…”
“I’m trying to make a person swim after the dolphin by…”
…or whatever…

…then maybe we can offer suggestions.

For example, I’m not sure why fromAngles and toAngles don’t work for you but I don’t know enough about what you are trying to do… so…

ok ok,

1-Player clicks on the screen
2- I create a vector going from the players spatial to the cursor position (ground position)
3- I draw the vector Spatial -> goundPosition

I defined in an Animation class a list of transformations of the players spatial
Animation: List<Translation> translations

a Translation is a vector like this : (0,0,0) -> (0.7f,0,0) ->(1f,0,0)
the translation is decsribed in a relative way to the distance between spatial and gound position;

when I appli my translation to the spatial, I Multiply the translation vector by the distance of translation (Spatial->groundPosition)

then I need to rotate that vector so it will be parallel to the (Spatial->groundPosition) vector.

that’s where I get the problem,

the translation is rotated in the wrong direction, and the players spatial moves in the opposite direction.

sumup: I defined a number of transformations that I need to apply along the spatial -> groundPosition vector

Well, my immediate gut reaction is wondering if Quaternion.lookAt() can help you here.

…or if you are doing this all in the x,z plane then Quaternion.fromAngles and toAngles seems like it would be better than an angle axis.

Though I generally find that applications that find themselves doing things like atan2() or angleBetween() are going in the wrong direction. (pun intended). lol nice pun pspeed ^^

but don’t you think its a limitation of the quaternion.fromAngleAxis? which doesn’t take into account the negative 0 float?

-0 is a floating point anomaly. -0 == 0 for all practical purposes and a zero length vector is not a direction vector. The issue is that you didn’t detect that the two angles were the same to begin with and therefor a cross product is actually invalid… well, it’s valid but cannot produce an orthogonal angle because there are an infinite number of orthogonal angles between a vector and itself.

…but as I said, all of this should be unnecessary if I understand the problem correctly. If not, just detect that the vectors are the same and use UNIT_Z instead or something.

1 Like

alright, thx, i’ll just detect and set the rotation axis to Y,
thx!

@navycougar said: alright, thx, i'll just detect and set the rotation axis to Y, thx!

Well… I’m going to extrapolate a bit from your description and the fact that you solution worked for you.

Here are some assumptions:
The fact that 0,-0,0 could safely be converted to 0,-1,0 in your particular use case implies that the vector would always have been either 0,1,0 or 0,-1,0… which means that the y components of all of your direction vectors are 0.

If that’s the case then the cross product is already unnecessary because you can get the angle about the Y-axis without all of this mess:
float angle = quat.toAngles(null);

Then to get a rotation back again from some angle:
Quaternion newQuat = new Quaternion().fromAngles(0, angle, 0);

…bypass the cross product mess altogether.

I’m still not really sure even any of that is necessary, though.

To get the quaternion for looking down your direction vector would be:
Vector3f dir = click.subtract(playerPos).normalizeLocal();
Quaternion quat = new Quaternion().lookAt(dir, Vector3f.UNIT_Y);

For that quaternion, vector 0,0,1 rotated by quat would be equivalent to the original direction vector and should be what you need to deal with your Translations. (Just swap your x for z.)