• Create Account

### #ActualÁlvaro

Posted 11 November 2012 - 12:33 AM

Also I am not sure what you meant by scaling the other three parts after changing the W value. Do I need to just normalize it?

Not quite. You need to scale the unreal part of the quaternion (the coefficients of i, j and k) so that the whole quaternion is unit length. Something like this:

#include <boost/math/quaternion.hpp>
#include <cmath>

typedef boost::math::quaternion<double> Q;

Q limit_rotation(Q q, double min_real_part) {
if (q.real() <= -min_real_part || q.real() >= min_real_part)
return q;

if (q.real() < 0)
q = -q;

double desired_unreal_length = std::sqrt(1 - min_real_part * min_real_part);
double old_unreal_length = abs(q.unreal());
return Q(min_real_part) + q.unreal() * (desired_unreal_length / old_unreal_length);
}

### #2Álvaro

Posted 11 November 2012 - 12:33 AM

Also I am not sure what you meant by scaling the other three parts after changing the W value. Do I need to just normalize it?

Not quite. You need to scale the unreal part of the quaternion (the coefficients of i, j and k) so that the whole quaternion is unit length. Something like this:

#include <boost/math/quaternion.hpp>
#include <cmath>

typedef boost::math::quaternion<double> Q;

Q limit_rotation(Q q, double min_real_part) {
if (q.real() <= -min_real_part || q.real() >= min_real_part)
return q;

if (q.real() < 0)
q = -q;

double desired_unreal_length = std::sqrt(1 - min_real_part * min_real_part);
double old_unreal_length = abs(q.unreal());
return Q(min_real_part) + q.unreal() * (desired_unreal_length / old_unreal_length);
}

### #1Álvaro

Posted 11 November 2012 - 12:31 AM

Also I am not sure what you meant by scaling the other three parts after changing the W value. Do I need to just normalize it?

Not quite. You need to normalize the unreal part of the quaternion (the coefficients of i, j and k) so that the whole quaternion is unit length. Something like this:

#include <boost/math/quaternion.hpp>
#include <cmath>

typedef boost::math::quaternion<double> Q;

Q limit_rotation(Q q, double min_real_part) {
if (q.real() <= -min_real_part || q.real() >= min_real_part)
return q;

if (q.real() < 0)
q = -q;

double desired_unreal_length = std::sqrt(1 - min_real_part * min_real_part);
double old_unreal_length = abs(q.unreal());
return Q(min_real_part) + q.unreal() * (desired_unreal_length / old_unreal_length);
}

PARTNERS