Public Group

# Simple vector rotation problem

This topic is 3931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to rotate two 3d vectors while keeping them perpendicular to each other. I first define a 3d vector y in spherical coordinates = {magnitude, angle from z-axis, angle from y-axis}. I then define a second vector x as always being perpendicular to y = {mag, y[1] + pi/2, y[2] + pi}. In this way x is always 90 degrees ahead of y in the z plane, and 180 degrees ahead of y in the xy plane. Therefore, the dot product between x and y should always be 0 as they are always perpindicular. Here is my code for creating y, x, and advancing y. My problem is the dot product becomes non zero after the first rotation!
double[] y = {10, 0, Math.PI/4}; //arbitrary start position
double[] x = {10, y[1] + Math.PI/2, y[2] + Math.PI}; //perpendicular to y

When the user drags the mouse up/down the following code to tilt the y vector in the z axis [i.e. increase angle between y and z-axis] executes:
y[1] += Math.PI/90;	//increase tilt by arbitrary increment
x[1] = yviewP[1] + Math.PI/2;
x[2] = yviewP[2] + Math.PI;

Now, when I convert x and y to Cartesian coordinates (xC and yC) using the standard method:
yC[0] = y[0]*Math.sin(y[1])*Math.cos(y[2]);
yC[1] = y[0]*Math.sin(y[1])*Math.sin(y[2]);
yC[2] = y[0]*Math.cos(y[1]);
xC[0] = x[0]*Math.sin(x[1])*Math.cos(x[2]);
xC[1] = x[0]*Math.sin(x[1])*Math.sin(x[2]);
xC[2] = x[0]*Math.cos(x[1]);

and then take the dot product of yC and xC I get nonzero results, yet I do believe I have maintained a 90 deg angle between x and y when I incremented the tilt. Can anybody see any obvious flaws in my math or code? I have been stuck here for a week. The reason I am doing this is that y and x will become my screen axis and to display 3d vectors I will project the vectors on y and x and those two results will become my 2d plot.

##### Share on other sites
How much non-zero? A tiny bit, or a lot?

If it's just a bit, then it's the rounding due to inaccuracy of floating point numbers.

If it's a lot, then there's an error in your code.

##### Share on other sites
Quote:
 Original post by AntheusHow much non-zero? A tiny bit, or a lot?If it's just a bit, then it's the rounding due to inaccuracy of floating point numbers.If it's a lot, then there's an error in your code.

Yeah it's quite a lot, like it increases by ~10 every time. The code I listed is the only code that has any effect on the dot product.

If anybody is willing to compile/run this I cleaned up the entire code. It pops up a window and when the user clicks its supposed to tilt the y axis, displaying a 3d axis system:
[msg me if want code]

[Edited by - javaguy5000 on February 9, 2008 1:16:45 PM]

##### Share on other sites
Hi.

The problem is not in your code, or rather, the problems in your code are inherited from the problems in your math.

Lets look at what happens:
double y[] = { 10, 0, pi/4 };double x[] = { 10, y[1] + pi/2, y[2] + pi }

These are indeed perpendicular, and all is good. Now lets tilt y by pi/4:
y[1] += pi/4;x[1] = y[1] + pi/2;x[2] = y[2] + pi;

Now:
y = { 10, pi/4, pi/4 }x = { 10, 3pi/4, 5pi/4 }

Lets convert them to cartesian:
y = { 5, 5, 7.071 }x = { -5, -5, -7.071 }

So we see the problem. In fact x == -y (this is coincidental by the way), which is not what we wanted.

The problem arises from the use of spherical coordinates. When you tilt y, x also tilts correctly. However when you rotate x, it rotates around the z-axis, and not around the y-vector, which is what you want, I believe.

As for how to fix this, I can't tell you. From what little I know about rotations in 3d I think you need to look in to Quaternions or Rotation Matrices.

##### Share on other sites
Quote:
 Original post by realcoreHi....So we see the problem. In fact x == -y (this is coincidental by the way), which is not what we wanted.The problem arises from the use of spherical coordinates. When you tilt y, x also tilts correctly. However when you rotate x, it rotates around the z-axis, and not around the y-vector, which is what you want, I believe.As for how to fix this, I can't tell you. From what little I know about rotations in 3d I think you need to look in to Quaternions or Rotation Matrices.

Hello, thank you for your response. You are correct and I indeed had a similar epiphany last night.

My solution is this:

double[] y = {10, 0, Math.PI/4};double[] x = {10, Math.PI/2, yviewP[2] + Math.PI/2}//Upon rotateif(rotate is up/down)    y[1] +/-= pi/180;if(rotate is left/right)   y[2] +/-= pi/180x[2] = y[2] + pi/2;

This way the x vector is always in the xy-plane and merely needs to keep ahead of y by 90 degrees in that plane.

As far as I can tell this creates a full 3d renderer equivalent to a viewing plane at the origin of the axes onto which all vectors are projected (projected x=dot(x, vector), projected y=(dot(y, vector)). When the user drags the mouse up, the plane at the origin tilts. A left-right moves similarly rotates the plane around the z-axis.

I can't tell if the x vector always being in the xyplane creates any limitations but its good enough for a basic data plotter I think.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633297
• Total Posts
3011251
• ### Who's Online (See full list)

There are no registered users currently online

×