OpenGL Rotate an object around a line

I am programming in OpenGL and C ++. I know 2 points on 1 line (diagonal line) and I want to rotate the object around this diagonal line. How can i do this? I know how to use glrotatef to rotate around the x, y, or z axis, but I'm not sure about that.

+4
source share
3 answers

The x, y, and z glRotate before glRotate can specify any arbitrary axis, not just the x, y, and z axes. To find the axis passing through your line, simply subtract the end points of the line to get the axis vector: if there are two points (x1, y1, z1) and (x2, y2, z2) , you need the axis (x2-x1, y2-y1, z2-z1) .

Edit: As @chris_l pointed out, this only works if the line passes through the origin. If not, first apply the translation (-x1, -y1, -z1) so that the line passes through the origin, then applies the specified rotation and translates it back to (x1, y1, z1) .

+5
source

Hey, how about doing some quaternions / vector maths? =) I did this using a small β€œpatch” in my Vector class:

 double NumBounds(double value) { if (fabs(value) < (1 / 1000000.0f)) return 0; else return value; } class Vector { private: double x, y, z; public: Vector(const Vector &v) { x = NumBounds(vx); y = NumBounds(vy); z = NumBounds(vz); } Vector(double _x, double _y, double _z) { x = NumBounds(_x); y = NumBounds(_y); z = NumBounds(_z); } Vector Normalize() { if (Length() != 0) return Vector(x / Length(), y / Length(), z / Length()); else return *this; } double operator[](unsigned int index) const { if (index == 0) return NumBounds(x); else if (index == 1) return NumBounds(y); else if (index == 2) return NumBounds(z); else return 0; } void operator=(const Vector &v) { x = NumBounds(vx); y = NumBounds(vy); z = NumBounds(vz); } Vector operator+(const Vector &v) { return Vector(x + vx, y + vy, z + vz); } Vector operator-(const Vector &v) { return Vector(x - vx, y - vy, z - vz); } double operator*(const Vector &v) { return NumBounds((x * vx) + (y * vy) + (z * vz)); } Vector operator*(double s) { return Vector(x * s, y * s, z * s); } Vector DotProduct(const Vector &v) { double k1 = (y * vz) - (z * vy); double k2 = (z * vx) - (x * vz); double k3 = (x * vy) - (y * vx); return Vector(NumBounds(k1), NumBounds(k2), NumBounds(k3)); } Vector Rotate(Vector &axis, double Angle) { Vector v = *this; return ((v - axis * (axis * v)) * cos(angle)) + (axis.DotProduct(v) * sin(angle)) + (axis * (axis * v)); } }; 

Using this class, you can easily rotate any vector around any other:

 Vector a(1.0f, 0.0f, 0.0f), b(0.0f, 1.0f, 0.0f), c(0.0f, 0.0f, 0.0f); c = a.Rotate(b, M_PI / 2.0f); // rotate vector a around vector b for 90 degrees (PI / 2 radians): should be Vector(0, 0, 1); 
+1
source

glrotate rotates around an axis. One way is to perform transformations that align the axis of rotation with one of the coordinate axis, perform the rotation, and then invert the first step. If you need speed, you can combine operations in a special transformation matrix and apply them in one step. Here is the description here .

0
source

All Articles