Point rotation

Started by
1 comment, last by Koetje 17 years, 9 months ago
I have a vector class with in it 'points' (the other point is assumed to be the origin). Now I would like to make 2 or three functions in this vector class like: public Vector3 Yaw(int degrees) public Vector3 Pitch(int degrees) public Vector3 PitchYaw(int pitchDegrees, int yawDegrees) That Yaw and Pitch the Vector3 and return a new Vector3 with the result. Now there are multiple ways to do this as far as I found, using quaternions, rotation matrixes etc. I used a quaternion camera class once but I had major troubles really understanding what it did and I dont really like that, also I feel that it would be overkill. Whats the prettiest way to do this kind of thing? (If it turns out to be quaternions, I guess I'll have to fiddle a bit more with it again :P) Speed isnt an issue, but I would really like the implementation to be clean and understandable.
Advertisement
#include <cmath>struct Vector3 {  double x,y,z;  Vector3(double x, double y, double z):x(x),y(y),z(z){  }  Vector3 Yaw(double a){    double cos_a=std::cos(a);    double sin_a=std::sin(a);    return Vector3(x*cos_a-z*sin_a,y,x*sin_a+z*cos_a);  }  Vector3 Pitch(double a){    double cos_a=std::cos(a);    double sin_a=std::sin(a);    return Vector3(x*cos_a-y*sin_a,x*sin_a+y*cos_a,z);  }  Vector3 PitchYaw(double pitch_angle, double yaw_angle){    return Pitch(pitch_angle).Yaw(yaw_angle);  }};std::ostream &operator<<(std::ostream &o, Vector3 const &v){  return o << '(' << v.x << ',' << v.y << ',' << v.z << ')';}


Friends don't let friends use degrees. The functions I provided use radians. Also, don't make your angles integers. If you want to convert your degrees to radians, multiply by Pi/180.

Also, I am not entirely sure what conventions you use with axes, so you may have to change the code a little to work the way you want. Also, it's not immediately obvious to me whether you want to apply pitch first and yaw later or the other way around, but that's also easy to modify.

Hey, thanks for your reply!

Your code looks alot like one of the things I was working with earlier but couldnt get it to work, but this works pretty good :)

About the radians, I actually already used those but in my quick example I wrote degrees, oops...

This topic is closed to new replies.

Advertisement