I have written a script to calculate the points of an ellipse and then draw them using a LineRenderer. I add some corrections to the calculated points: First, I want the GameObject to be in on of the foci of the ellipse, so the points get offset by that amount. Second, the GameObject.Transform.rotation is added to the points so I can use the GameObject rotation in the Editor to rotate the Ellipse. Third, the GameObject.Transform.position is added to all calculated points so I can move the ellipse around by moving the GameObject the script is attached to. I fiddled around with the correct order yesterday until it worked. Then I cleaned the script a bit and created a static function for calculating the ellipse points so that other scripts can use that functionality, too. After that the rotation behaved weird - rotating around strange axis, not moving in the local cardinal directions when I use the Editor arrows to move it etc. - until I cut the uppermentioned “First” and “Third” from the calculation. Now it works and I don’t have any clue, why. Please enlighten me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent (typeof(LineRenderer))]
public class Ellipse1 : MonoBehaviour
{
public Vector2 axes = new Vector2(1f, 1f);
public float width = 1f;
public int resolution = 500;
private Transform self_transform;
private Vector3[] positions;
private LineRenderer self_lineRenderer;
void Update() {
if(self_transform == null) {
self_transform = GetComponent<Transform>();
}
if(self_transform.hasChanged) {
CalcEllipse();
}
}
public void CalcEllipse() {
if(self_lineRenderer == null) {
self_lineRenderer = GetComponent<LineRenderer>();
}
print(self_transform.rotation.eulerAngles);
Vector3[] returnlist = returnEllipsePath(self_transform.position, self_transform.rotation.eulerAngles, axes, 500);
self_lineRenderer.SetPositions(returnlist);
}
public static Vector3[] returnEllipsePath(Vector3 focus_pos, Vector3 rotation, Vector2 axes, int resolution, bool overlap) {
Vector3[] returnlist = new Vector3[resolution+2];
float f;
Vector3 focus_offset;
if(axes.x > axes.y) {
f = Mathf.Sqrt(Mathf.Pow(axes.x, 2) - Mathf.Pow(axes.y, 2));
focus_offset = new Vector3(f, 0, 0);
} else if(axes.x < axes.y) {
f = Mathf.Sqrt(Mathf.Pow(axes.y, 2) - Mathf.Pow(axes.x, 2));
focus_offset = new Vector3(0, f, 0);
} else {
focus_offset = new Vector3(0,0,0);
}
float angle;
for (int i = 0; i <= resolution + 1; i++) {
angle = (float)i / (float)(resolution) * 2.0f * Mathf.PI;
// --- OLD CALCULATION:
//Quaternion.Euler(rotation) * (new Vector3(axes.x * Mathf.Cos(angle), axes.y * Mathf.Sin(angle), 0.0f) + focus_offset) + focus_pos;
// --- NEW CALCULATION:
Vector3 pointPosition = Quaternion.Euler(rotation) * new Vector3(axes.x * Mathf.Cos(angle), axes.y * Mathf.Sin(angle), 0.0f);
returnlist[i] = pointPosition;
}
return returnlist;
}
public static Vector3[] returnEllipsePath(Vector3 focus_pos, Vector3 rotation, Vector2 axes, int resolution) {
return returnEllipsePath(focus_pos, rotation, axes, resolution, true);
}
}