# How to rotate a path in 3D (computer sciences)

I'm working on something in Unity3D (the game engine) where I have to modify a path/road in 3d space. The path consists of a collection of positions and quaternion-orientations (the orientation determines both the direction of the road, and the banking).

I'm having trouble rotating the the road the way I want it to.

I want to be able to rotate it using euler angles, rotating the path according to the startingpoint's orientation. (so the x-angle should rotate it around the first point's local x axis, etc.)

I thought the correct way to do this would be to do:

for (int i = 0; i < points.Count; i++)
{
rotation = (Quaternion.Inverse(points[0].orientation) * Quaternion.Euler(eulerRotation) * points[0].orientation);
points[i].orientation = rotation * points[i].orientation;
points[i].position -= points[0].position;
points[i].position = rotation * points[i].position;
points[i].position += points[0].position;
}


But that doesn't seem to rotate it the way I want it to, the axes are wrong.

Any help?

Here's an image to show what I mean: roadscreenshot

It shows both the road before and after rotation (the upmoast road being its original orientation), It was supposed to rotate 30 degrees around the startingpoint's local x-axis, but clearly that's not how it rotated. The axes shown are the startingpoint's local axes, the x-axis is the red one.

• Could you please clarify what point in “collection of points” mean geometrically? And also what quaternion lib are you using. The picture would really help. It's hard to understand now, what you have, what you are achieving with the current code and what you are trying to achieve. – Vasily Mitch Mar 5 at 10:53
• Ok, I edited my question a bit to make it more clear what I mean. Sorry, I used the word point for both the position of a point and position+rotation. I'm working in Unity3D (the game engine). The points contain positions in 3d space, and the quaternions are Unity's quaternions (can't really say more about them) – Stef Mar 5 at 11:00
• Are points[i].poisiton given in global or local coordinates? What will happen if you just draw a polyline on those points? Will you get an approximation of the road? – Vasily Mitch Mar 5 at 11:09
• Both the positions and rotations are in Global space – Stef Mar 5 at 11:12
• I added an image – Stef Mar 5 at 11:16

If position and orientation of the points are given in global coordinate space, then you don't need to mix orientation into rotation as it has nothing to do with it.

rotationQuaternion = Quaternion.Euler(eulerRotation);
for (int i = 0; i < points.Count; i++)
{
// If you use orientation as a rotation quaternion
points[i].orientation = rotationQuaternion * points[i].orientation;
points[i].position -= points[0].position;
points[i].position = rotationQuaternion * points[i].position;
points[i].position += points[0].position;
}


For better performance (at least I believe so), you might want to create Transform object from rotation and position of first point and apply it to positions of other points with one operation instead of three.

• I don't understand why you use both the rotation and the inverse? I'll try it and come back to you. (the actual setup is a lot complexer than what I described unfortunately, so I have to think about how to actually apply this) – Stef Mar 5 at 11:26
• I will expand my answer. – Vasily Mitch Mar 5 at 11:31
• Actually it depends on how you use orientation as a quaternion. If you use it as a rotation quaternion from some fixed basis (say you perform points[i].orientation * Vector3(1,0,0) to find the direction of the road), then you need only one multiplication without inverse quaternion. I fixed the answer for the latter case. – Vasily Mitch Mar 5 at 11:39
• I don't know what "fixed basis" means in this context, but yes, multiplying the orientation with Vector3(0,0,1) will give the roads direction in that point (as I use the z-axis as the forward axis). You're answer now is actually what I had initially, if you're sure about this I must have made some other mistake somewhere else, let me check. Thanks for your help. – Stef Mar 5 at 12:41
• Ok, so I basically reverted the project to back when I did what you said, and everything works fine. I feel like this project is trollling me, it's giving me serious headaches. Thanks for your help, much appreciated. – Stef Mar 5 at 13:20