World::Create()

AvatarProgramming ramblings with some vague emphasis on Games, Games Tools and Web.

Ellipse Maths 2 - Tangents

Now we can find the point on an ellipse, given the angle around the ellipse we are positioned. What if we want to orientate our object to face in the direction it is moving as we increase or decrease the angle? Or what if we want our object to leave the ellipse and smoothly move onto another path, like a spline?

What we need is the tangent to the ellipse at a given angle. I knew this should be straight forward, but it took a while for my brain to slip back to A-Level maths mode and remember how to do this. Too much time writing Tools code I guess. Anyway, Calculus to the rescue!

So if P is the point on the ellipse that we defined in the previous post, we are looking for T the tangent on the line.


P = m_h * cos(t) * m_up + m_l * sin(t) * m_along + m_centre;


To find the tangent we want the derivative of this equation, that is the rate of change of P with respect to t ( the angle around the ellipse ). So we can simply differentiate w.r.t. t which is simple if you know that the differential of sin(t) is cos(t) and the differential of cos(t) is -sin(t). Giving us

T = - m_h * sin(t) * m_up + m_l * cos(t) * m_along;

Note, that we've lost m_centre, because that is a constant.
And that is it, here is the full method

Vector3 TangentAt(float t) const
{
float c = cos(t);
float s = sin(t);

return - m_h * s * m_up + m_l * c * m_along;
}

Ellipse Maths

So, let's say you are writing a game, and you want something ( an enemy, the player, whatever ) to move in an ellipse. This ellipse is a 2D shape but can exist in any orientation in a 3D space.

So first off, how do you represent the ellipse?

We can define the ellipse as a centre point, two orthogonal axes and two radii ( or a height and length if you prefer )

class Ellipse
{
Vector3 m_centre;
Vector3 m_up;
Vector3 m_along;
float m_h;
float m_l;
};


Ellipse e = Ellipse( Vector3(0,0,0), Vector3(0,1,0), Vector3(1,0,0), 1, 1);

is defining a circle of radius 1 at the origin, orientated with y up and x along.

Ellipse e = Ellipse( Vector3(0,0,0), Vector3(0, 1, 1), Vector3(1,0,0), 10, 20);

is defining an ellipse which is angled forward ( (0,1,1) is up ) and along the x axis.

In order to move your enemy/player around the ellipse you will need to be get a point on that ellipse, this is pretty straightforward:

Vector3 Ellipse::PointAt(float t)
{
float c = cos(t);
float s = sin(t);

return m_h * c * m_up + m_l * s * m_along + m_centre;
}

Here t is the angle around the ellipse where you want your point. You can see that if the angle is 0 then the c = 1, s = 0 so the returned point is just

(m_centre + m_h * m_up)

i.e. the top of the ellipse.

for t = π/2, c=0 and s=1 so the returned point is

(m_centre + m_l * m_along)

i.e. the right side of the ellipse.

There you go then, how to find a point on an ellipse. Next topic, getting the tangent to an ellipse.