Jump to content
  • Advertisement
Sign in to follow this  
Koetje

Point rotation

This topic is 4516 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!