Rotating an ellipse with quaternions.

Hey all,

I have a situation where I’m plotting an elliptical path in 3D space, the function that does this is given the following parameters.

The problem I am having is the rotations necessary to get the desired orientation of the resulting path.

EDIT: Solution found, I finally managed to take a long enough break from it to see things a bit clearer on my return. ive updated the code below for any who might be interested.

    public static void DrawEllipse(Vector3 p1, Vector3 p2, Vector3 up, float minor)
    {
        if (up.magnitude == 0) return;
        float major = Vector3.Distance(p1, p2)*0.5f;
        Vector3 center = p1 + ((p2 - p1)) * 0.5f;
        Quaternion quat = Quaternion.identity;

        float dx = p2.x - p1.x;
        float dz = p2.z - p1.z;
        quat = Quaternion.AngleAxis(-Mathf.Atan2(dz, dx)*Mathf.Rad2Deg, Vector3.up);
        quat *= Quaternion.LookRotation(up);

        Vector3[] points = new Vector3[50];
        for (int i = 0; i < points.Length; i++)
        {
            // build the coordinates in arbitrary space.
            points[i].x = Mathf.Cos(((float)i / points.Length) * (Mathf.PI * 2)) * major;
            points[i].z = Mathf.Sin(((float)i / points.Length) * (Mathf.PI * 2)) * minor;

            // modify the point for correct placement and orientation.
            points[i] = quat * points[i];
            points[i] += center;
        }
        DrawPath(points, Color.cyan, true);
        LogArray(points);
        DrawCross(p1, 1, Color.red);
        DrawCross(center, 1, Color.white);
        DrawCross(p2, 1, Color.green);
        Debug.DrawLine(center, center + up);
        DrawAxis(Vector3.zero, Vector3.up, Vector3.right, Vector3.forward, 1);
    }
1 Like

This mite be easier with itween path .

Sure it might have been easier with iTween path. But then where’s the fun or challenge in that :P. Always nice to try your own hand at things sometimes, and doing it that way means usually better understanding of the code, and thus more control to tweak and change things as needed down the road.