I'm creating a C++ ifc importer.
I have a direction vector and I want to extrude a section from it. The section itself is a list of 2d points.
To calculate the extrusion direction I have to multiply a non-transformed direction with a transformation matrix.
The matrix has a transformation in x, y, and z (like Euler angles).
I must calculate the rotation angle around the extrude direction.
I have a matrix class that returns the Euler angles from a
matrix:
matrix.ExtractEulerXYZ(x,y,z)
The problem is that I can have a direction vector that has a rotation in x, y or z, how can I select the correct angle x, y, or z from the extrude direction?
A 2d point is at location (x, y) in 2d space and (x, y, 0) in 3d space.
Euler angles in 3d space define 3 rotations from the xyz axes to rotate the xyz axes to the specified point. That is, if you mark a point on the x axis that is the correct distance on the x axis to the point, you rotate the axes by a along the XY plane, b along the ZY plane and c along the ZX plane.
See the animation in the "Relationship with physical motions" section of http://en.wikipedia.org/wiki/Euler_angles -- particularly, follow the progress of the x axis that initially projects toward the bottom left corner.
If you just have a 2d point, the ZY and ZX rotations do not do anything -- you are just rotating around the XY axis. Therefore, you can use simple trigonometry (SOH CAH TOA) to find the angle of the line from the origin to the point; there is no need to use a matrix.
Related
so i'm trying to rotate the camera around a cube object , using the keyboard arrows to change the y angle and the x angle , i want a result like this : this video
Well now I want to move the camera in a circular motion around the shape, so I used
the sphere parametric equation to determine the camera's x, y, and z coordinates on the parameter of the sphere using the x angle and the y angle , this is the equation : sphere parametric equation
For this moment, the equation is as following:
x = radius*cos(y_angle)*cos(x_angle)
y = radius*cos(y_angle)*sin(x_angle)
z = radius*sin(y_angle)
so the code will be :
gluLookAt(x, y, z, 0, 0, 0, 0, 1, 0); // x,y and z that we calculated above
Now it is supposed to work without problems, but the camera dose rotate in a strange way that is not as it should, I think this has something to do with the up vector in the
glulookat() function because i'm using a constant up vector like
(0,1,0)
Anyway, I can't calculate the up vector correctly, so I want help in making a circular motion around the cube like the one that appeared in the video using the glulookat , i hope i made it clear ، Because as you have noticed, I can't explain well
I need a method to find a set of homogenous transformation matrices that describes the position and orientation in a sphere.
The idea is that I have an object in the center of this sphere which has a radius of dz. Since I know the 3d coordinate of the object I know all the 3d coordinates of the sphere. Is it possible to determine the RPY of any point on the sphere such that the point always points toward the object in the center?
illustration:
At the origo of this sphere we have an object. The radius of the sphere is dz.
The red dot is a point on the sphere, and the vector from this point toward the object/origo.
The position should be relatively easy to extract, as a sphere can be described by a function, but how do I determine the vector, or rotation matrix that points such that it points toward origo.
You could, using the center of the sphere as the origin, compute the unit vector of the line formed by the origin to the point on the edge of the sphere, and then multiply that unit vector by -1 to obtain the vector pointing toward the center of the sphere from the point on the edge of the sphere.
Example:
vec pointToCenter(Point edge, Point origin) {
vec norm = edge - origin;
vec unitVec = norm / vecLength(norm);
return unitVec * -1;
}
Once you have the vector you can convert it to euler angles for the RPY, an example is here
Of the top of my head I would suggest using quaterneons to define the rotation of any point at the origin, relative to the point you want on the surface of the sphere:
Pick the desired point on the sphere's surface, say the north pole for example
Translate that point to the origin (assuming the radius of the sphere is known), using 3D Pythagorus: x_comp^2 + y_comp^2 + z_comp^2 = hypotenuse^2
Create a rotation that points an axis at the original surface point. This will just be a scaled multiple of the x, y and z components making up the hypotenuse. I would just make it into unit components. Capture the resulting axis and rotation in a quaterneon (q, x, y, z), where x, y, z are the components of your axis and q is the rotation about that axis. Hard code q to one. You want to use quaterneons because it will make your resulting rotation matricies easier to work with
Translate the point back to the sphere's surface and negate the values of the components of your axis, to get (q, -x, -y, -z).
This will give you your point on the surface of the sphere, with an axis pointing back to the origin. With the north pole as an example, you would have a quaternion of (1, 0, -1, 0) at point (0, radius_length, 0) on the sphere's surface. See quatrotation.c in my below github repository for the resulting rotation matrix.
I don't have time to write code for this but I wrote a little tutorial with compilable code examples in a github repository a while back, which should get you started:
https://github.com/brownwa/opengl
Do the mat_rotation tutorial first, then do the quatereons one. It's doable in a weekend, a day if you're focused.
I have two points on circle. I know degree from center and coordinates of one point. I want find coordinate of the other point. I think need multiply by rotation matrix to find point. How can i do in c++? Is there any function for it?
you can calculate it directly using
x cos(angle) - y sin (angle )
x sin(angle) + y cos (angle )
the cos and sin functions are available in math.h
note that the rotation will be in anti clockwise direction
and the rotation will be about the origin. 'angle' should be in radians.
if the center of the circle is not located at origin then you'll have to first shift the origin to the center of the circle , apply rotation and shift the origin back again to get the other point
I'm currently working on a game which renders a textured sphere (representing Earth) and cubes representing player models (which will be implemented later).
When a user clicks a point on the sphere, the cube is translated from the origin (0,0,0) (which is also the center of the sphere) to the point on the surface of the sphere.
The problem is that I want the cube to rotate so as to sit with it's base flat on the sphere's surface (as opposed to just translating the cube).
What the best way is to calculate the rotation matrices about each axis in order to achieve this effect?
This is the same calculation as you'd perform to make a "lookat" matrix.
In this form, you would use the normalised point on the sphere as one axis (often used as the 'Z' axis), and then make the other two as perpendicular vectors to that. Typically to do that you choose some arbitrary 'up' axis, which needs to not be parallel to your first axis, and then use two cross-products. First you cross 'Z' and 'Up' to make an 'X' axis, and then you cross the 'X' and 'Z' axes to make a 'Y' axis.
The X, Y, and Z axes (normalised) form a rotation matrix which will orient the cube to the surface normal of the sphere. Then just translate it to the surface point.
The basic idea in GL is this:
float x_axis[3];
float y_axis[3];
float z_axis[3]; // This is the point on sphere, normalised
x_axis = cross(z_axis, up);
normalise(x_axis);
y_axis = cross(z_axis, x_axis);
DrawSphere();
float mat[16] = {
x_axis[0],x_axis[1],x_axis[2],0,
y_axis[0],y_axis[1],y_axis[2],0,
z_axis[0],z_axis[1],z_axis[2],0,
(sphereRad + cubeSize) * z_axis[0], (sphereRad + cubeSize) * z_axis[1], (sphereRad + cubeSize) * z_axis[2], 1 };
glMultMatrixf(mat);
DrawCube();
Where z_axis[] is the normalised point on the sphere, x_axis[] is the normalised cross-product of that vector with the arbitrary 'up' vector, and y_axis[] is the normalised cross-product of the other two axes. sphereRad and cubeSize are the sizes of the sphere and cube - I'm assuming both shapes are centred on their local coordinate origin.
I have a problem with the orientation of objects in the OpenGL program, I can't calculate rotation of x, y, z based on 3D vector and angle or 4D vector.
Im working with c++.
I have:
Vector3f myVector;
float angle;
float rotx;
float roty;
float rotz;
//i need smomething like
doSomething(a,angle,&rotx,&roty,&rotz);
glRotatef(rotx,1.0,0,0);
glRotatef(roty,0,1.0,0);
glRotatef(rotz,0,0,1.0);
// draw object
"myVector" is a vector in 3d space. I want to rotate object in direction of vector. "angle" is rotation of object around the vector. "rotx,roty,rotz" are local variables. How to calculate rotx,roty,royz to do this?
http://en.wikipedia.org/wiki/File:Euler_AxisAngle.png
In picture on link my object is oriented in direction of 'x', i want to orinet it in direction of 'e' and 'O' is my "angle".
How to calculate rotx,roty,royz to do this?
There's no unique solution to your problem. For each target direction there are 6 different ways to express it in Euler angles.
The only way to represent rotations unambigously are rotation matrices, or their close relatives, Quaternions.
The parameters to glRotate are very close to a quaternion. In fact the axis parameters are the normalized i,j,k elements of a quaternion and the real quaternion part is the rotation angle in radians.