1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Vasily Mitch Mar 5 at 10:53
  • $\begingroup$ 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) $\endgroup$ – Stef Mar 5 at 11:00
  • $\begingroup$ 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? $\endgroup$ – Vasily Mitch Mar 5 at 11:09
  • $\begingroup$ Both the positions and rotations are in Global space $\endgroup$ – Stef Mar 5 at 11:12
  • $\begingroup$ I added an image $\endgroup$ – Stef Mar 5 at 11:16
0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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) $\endgroup$ – Stef Mar 5 at 11:26
  • $\begingroup$ I will expand my answer. $\endgroup$ – Vasily Mitch Mar 5 at 11:31
  • $\begingroup$ 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. $\endgroup$ – Vasily Mitch Mar 5 at 11:39
  • $\begingroup$ 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. $\endgroup$ – Stef Mar 5 at 12:41
  • $\begingroup$ 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. $\endgroup$ – Stef Mar 5 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.