OpenGL Rotation of an object around a line

后端 未结 3 1794
陌清茗
陌清茗 2020-12-17 04:21

I am programming in OpenGL and C++. I know 2 points on 1 line (a diagonal line) and wish to rotate an object around that diagonal line. How can I go about doing this? I know

3条回答
  •  再見小時候
    2020-12-17 04:49

    Hey, how about doing some quaternions / vector maths? =) I've done that using small "patch" on 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(v.x); y = NumBounds(v.y); z = NumBounds(v.z);
            }
    
            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(v.x); y = NumBounds(v.y); z = NumBounds(v.z);
            }
    
            Vector operator+(const Vector &v)
            {
                return Vector(x + v.x, y + v.y, z + v.z);
            }
    
            Vector operator-(const Vector &v)
            {
                return Vector(x - v.x, y - v.y, z - v.z);
            }
    
            double operator*(const Vector &v)
            {
                return NumBounds((x * v.x) + (y * v.y) + (z * v.z));
            }
    
            Vector operator*(double s)
            {
                return Vector(x * s, y * s, z * s);
            }
    
            Vector DotProduct(const Vector &v)
            {
                double k1 = (y * v.z) - (z * v.y);
                double k2 = (z * v.x) - (x * v.z);
                double k3 = (x * v.y) - (y * v.x);
    
                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 one:

    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);
    

提交回复
热议问题