Created
October 12, 2016 18:14
-
-
Save abeldantas/9a301f622b995e471693615515cd6a73 to your computer and use it in GitHub Desktop.
Ellipse animation in Unity C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
// Add this MonoBehaviour to anything on the scene and it will create a prototype comet moving around a pivot | |
public class LerpEllipseExample : MonoBehaviour | |
{ | |
// These are 'a' and 'b' in the ellipse formula, think aphelion and perihelion | |
float trajectoryHeight = 4; // y | |
float trajectoryWidth = 12; // x | |
float timeItTakesToGoAroundTheSun = 6; // 6 seconds, pretty fast | |
float timeSinceBeginning = 0; | |
GameObject comet; | |
GameObject pivotingPoint; | |
public void Start() | |
{ | |
// Comet, is going to move around the Sun | |
comet = GameObject.CreatePrimitive( PrimitiveType.Sphere ); | |
comet.name = "Comet"; | |
// Pivot around which we're going to move the comet | |
// Note that the comet is "inside" the Sun GameObject, if you rotate the pivot point, you rotate movement | |
pivotingPoint = new GameObject( "Pivoting Point" ); | |
comet.transform.parent = pivotingPoint.transform; | |
// Adding a sun, just because | |
var sol = GameObject.CreatePrimitive( PrimitiveType.Sphere ); | |
sol.name = "Sol"; | |
sol.transform.localScale = Vector3.one*3; | |
} | |
public void Update() | |
{ | |
// Increment the time since beginning variable, this is used to know how long it has been since the animation/movement started | |
timeSinceBeginning += Time.deltaTime; | |
// Calculate and assign the position of the comet | |
var positionInEllipse = LerpEllipse( trajectoryWidth, trajectoryHeight, timeSinceBeginning, | |
Vector3.zero, timeItTakesToGoAroundTheSun ); | |
comet.transform.localPosition = positionInEllipse; | |
} | |
// --- AUXILLIARY FUNCTIONS --- | |
// Lerp Ellipse gives you the x and y position for something moving along an ellipse | |
// 'horizontalAxis' and 'verticalAxis' are the 2 axis of the ellipse | |
// 'time' is the time that has passed since the animation/movement has started | |
// 'center' is the central point around which the ellipse is centered, needs to have x and y | |
// 'duration' is the duration of the whole movement/animation | |
public Vector2 LerpEllipse( float horizontalAxis, float verticalAxis, float time, Vector2 center, float duration ) | |
{ | |
var x = center.x + ( horizontalAxis * Mathf.Cos( ( time / duration ) * Mathf.PI * 2 ) ); | |
var y = center.y + ( verticalAxis * Mathf.Sin( ( time / duration ) * Mathf.PI * 2 ) ); | |
return new Vector2( x, y ); | |
} | |
// To draw ellipses with rotation we need more math | |
// x = a* cos( t)*cos( angle) - b* sin( t)*sin( angle) | |
// y = a* cos( t)*sin( angle) + b* sin( t)*cos( angle) | |
// | |
// To try out in wolfram alpha: | |
// having a = 20; b = 120; rotation angle = pi / 8 | |
// put the line bellow in wolfram alpha and you will see a rotated ellipse | |
// x= 20*cos( t)*cos( pi/8)-120*sin( t)*sin( pi/8), y=20*cos( t)*sin( pi/8)+120*sin( t)*cos( pi/8) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment