0
$\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 points that each have a position (Vector3) and orientation (quaternion) (the orientation determines both the direction of the road, and the banking).

There is some banking in the road each time there's a turn (kind of like in a bobsleigh-road). But the banking of the path seems to slowly drift, as the path starts with no banking, but ends with some banking. (while it should end without banking as well.)

I thought I could fix this by:

  • calculating the banking-angle in the endpoint
  • dividing this angle by the number of points in the path
  • creating a quaternion that rotates this amount around the global forward-direction
  • applying this rotation to each point, starting with the first, and rotating the rest of the path with it.

like this:

var sideDirection = Points.Last().orientation * Vector3.right;
float bankingAngle = -Mathf.Asin(sideDirection.y) * Mathf.Rad2Deg;
Quaternion bankingRotation = Quaternion.Euler(0, 0, bankingAngle * (1.0f / (Points.Count - 1)));
for (int i = 1; i < Points.Count; i++)
{
    Quaternion currentBankingRotation = Quaternion.Euler(0, 0, bankingAngle * (i / (float)(Points.Count - 1)));
    Points[i].orientation = currentBankingRotation * RotatedTrackingPoints[index][i].orientation;
    for (int j = i + 1; j < Points.Count; j++)
    {
        Points[j].position -= Points[i].position;
        Points[j].position = bankingRotation * Points[j].position;
        Points[j].position += Points[i].position;
    }
}

But it doesn't fix the banking all the time, sometimes it's correct, but sometimes it rotates in the wrong direction. It appears the banking angle I calculate sometimes has the wrong sign, but I can't fgure out why.

Can anyone tell me what I'm doing wrong and how I should fix the banking drift?

$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.