# How do I move an object along an ellipse path on diagonal?

Hi,

I need to move a game object along an ellipse path on any diagonal: upleft, upright, downleft or downright.

Now I’m getting only with directional movements: up, down, left or right.

For example:

``````            float positionX, positionY, centerX, centerY, semimajorAxes, semiminorAxes, alpha;

void Start() {
positionX = 0f;
positionY = 0f;
centerX = 0f;
centerY = 0f;
alpha = 0f;

//going up
semimajorAxes = 1f;
semiminorAxes = 5f;

//going left
//semimajorAxes = 5f;
//semiminorAxes = 1f;
}

void Update() {
alpha += 1f;

positionX = centerX + (semimajorAxes * Mathf.Cos(Mathf.Deg2Rad * alpha));
positionY = centerY + (semiminorAxes * Mathf.Sin(Mathf.Deg2Rad * alpha));

transform.position = new Vector2(positionX, positionY);
}
``````

Do I need to change the formula? Any suggestion?

Thx!

You have essentially two ways to actually rotate your ellipse:

• rotate your position around the center either through a rotation matrix or manually using sin and cos (which essentially would be the same thing)
• Compose the final point by using pre-rotated unit coordinate axes.

Usually simpler is the second solution, especially if you use a tranform to define the coordinate space. Though even when you specify the axis yourself for the 2d case it’s not that hard. First of all it gets way simpler when you actually use Vector2s for all your given information.

``````public Vector2 center;
public float alpha;
public Transform axis;

void Update()
{
alpha += 20f * Time.deltaTime;
transform.position = center + axis.right * p.x + axis.up * p.y;
}
``````

If you want to define the axes manually by a Vector2 you can do this:

``````public Vector2 axis;

// [ ... ]

// replace the last line in Update with this:
axis = axis.normalized;
Vector2 up = new Vector2(-axis.y, axis.x);
transform.position = center + axis * p.x + up * p.y;
``````

Of course instead of calculating the normal (up vector) of our given axis you could define both axis yourself. However if the two axis are not perpendicular of each other the result would be a skewed ellipse.