We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I need to figure out how to manipulate few equations that I have to rotate a single line in a 3D space using Processing.
I am getting the xyz coordinate values from a real gyroscope that I can rotate freely.
For the purposes of my application, the first point of the line is fixed to the origin, thus, my line function is now:
line(0, 0, 0, x, y, z);
I need to rotate my line in a manner that reaches any possible position.
For the distance, I am using the known formula of the distance:
d = sqrt(x*x + y*y + z*z);
Before continue, let me clarify something very important regarding how I am reading the x, y and z coordinate values from the gyroscope.
Each coordinate contains circular values between 0 to 360 degrees. In other words, if I rotate 360 degrees the gyro laying on my table, the z coordinate will change from 0 to 360. Similarly for the other two axis.
So far I can rotate the line perfectly but one axis at a time. I.e.
For rotating around the axis z, I am using:
x = d * cos(z)
y = d * sin(z)
For rotating around the axis y, I am using:
z = d * cos(y)
x = d * sin(y)
For rotating around the axis x, I am using:
y = d * cos(x)
z = d * sin(x)
My problem starts when I try to put them together:
I have tried the following:
y1 = d * sin(z);
x2 = d * cos(z);
x1 = d * sin(y);
z2 = d * cos(y);
z1 = d * sin(x);
y2 = d * cos(x);
x = (x2+x1);
y = (y1+y2);
z = (z1+z2);
When I rotate the line using the above equations, the line pivots almost correctly but at some positions its length starts to change or even becomes zero.
Again, what I just need is to find a way to generate x, y and z that permits the line to rotate freely (with the distance fixed of course).
As you may have noticed, my problem is more related to a Math approach than programming. That is why I have pot posted any code yet.
Thank you very much in advance for any comment.
Answers
I think it may be:
x = x + cos(a)*fixedLength
same for others...
Note that Processing has a built-in
dist()
function...Also, moved this topic to the How to... section as it is more appropriate there.
Thank you _vk and calsign for your comments.
After running some tests I have found out why the length of my line changes (greater or lesser). The reason is that in my formulas the second point of the line (x,y,z) does not depend upon the distance but on the value of the sine and cosine functions, therefore, when the gyro at different planes generates complementary angles that make the sine value equals but opposite to the cosine value, then, when I totalize them I get any coordinate value (greater or lesser).
For example, at certain position of the gyro I get: y=224 and z=314 then
Consequently
x = x1 + x2 = -0.69d + 0.69d = 0.
Therefore, the addition of the partial axis from different planes is no longer appropriate. That is why I don't have problems when combining one plane at a time.
So I need to redefine my formulas in order to achieve my goal.
I think your best bet would be to use Matrices. You could then multiply the coordinates of the "free" end of the line by a rotation matrix which could have the angle from the gyro in it. This will allow you to simply multiply the x,y,and z rotations of the point and the length of the line will remain fixed. Google "3d rotation about origin". It is really not as difficult as it looks at first. Hope this helps.
here is a code that takes mouseX and mouseY (so only two angles (simulated) and not three as in your case)
but the results are weird, he is rotating but....
maybe somebody can clarify this.
When I have the mouseY in the middle (=height/2) and rotate by going right with my mouseX, all looks fine (rotates around Y-axis)
but:
when I go with the mouseY down, it's still ok
but when I change the mouseX then, he's not rotating around the Y-axis anymore but around the current angle invoked by mouseY
how can I solve this?
I rather want that also in the 2nd case he always rotates around the y-axis (fixed y-axis when moving mouseX left and right)
In reference to my initial question about pivoting a line in 3D, I just resolved it. I only needed two angles: theta (polar angle) and phi (azimuth) which corresponded to my y and z values from the gyro angles. Then, I use the following formulas (spherical coordinate system to Cartesian coordinates) to calculate the second point:
I took the formulas from here: en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates
Regards!