# Constraining rotation to a single axis

## Recommended Posts

Solved.

Thanks!

Edited by knsl96

##### Share on other sites

One way to do it:

If the user input is a quaternion, convert it to axis and angle.

Convert axis and angle to a rotation vector: vec3 rv = axis * angle

Project this vector to your constraint axis unit vector: vec3 constrained_rv = constraintAxis * constraintAxis.Dot(rv);

Then convert result back to axis / angle: float angle = constrained_rv.Length(); vec3 axis = constrained_rv / angle;

Then convert to quat or matrix or whatever you need: quat q; q.FromaxisAndAngle(axis, angle);

So that's quite simple. I assume everything is given in global space, otherwise perform the operations in the local space of the joint for example.

Edited by knsl96

##### Share on other sites
22 minutes ago, knsl96 said:

float dprod = constraintAxis.Dot(constraintAxis, rv);

This line already gives the angle, so no need to calculate magnitude later. But that's just an optimization.

Pretty sure the NaN happens here if angle is zero:

24 minutes ago, knsl96 said:

MyVector axis = new MyVector(constrained_rv._x / angle, constrained_rv._y / angle, constrained_rv._z / angle);

So you would need to return identity quaternion if angle is close to zero before that.

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015751
×