World::Create()

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

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.

0 comments:

Post a Comment