From The Mana World
Revision as of 01:32, 27 February 2007 by Avaniel (talk | contribs) (Just saving changes)

This article is currently only a proposal

The features or design guidelines described in this article are only a proposal made by one or some persons. It has not been evaluated or accepted by the core development team yet. Feel free to add your personal opinion about them or make counter proposals.

This page was created to document the collision detection algorithms used in The Mana World. After the algorithms are converted to C++ and added to the code base it is very difficult to verify the assumptions and math of the author. Therefore an explanation of the algorithms used is given below.


Circle-Segment with Circle

In order to determine if a circle-segment and a circle collide, we start with transforming the coordinates of the 2d space in such a way that the center of the circle-segment is located at the origin. The circle segment can then be defined by a radius R, a half-top angle alpha and a placement angle beta, as shown in diagram (1).

The circle can simply be defined by its center-point P and its radius RP, where point P can be anywhere in the 2d space. See diagram (1) for an overview.

Diagram (1)

In order to determine if the circle-segment and the circle overlap, the x-y plane is divide in 6 Voronoi like regions. The regions are based on the vertexes O-A, O-B and the circular vertex A-B. They are shown in diagram (2).

Diagram (2)

For each region it is now determined what the pre-conditions for an overlap are, if P is in that region. If necessary, the pre-conditions for point P being in the region will be derived as well.

In the following text, a subscript x and y are used to indicate x and y coordinate of a point. If the coordinates considered are in a tranformed coordinate system, an extra indicator will be added. For example the x-coordinate of point A in the x1-y1 coordinate system is denoted as: Ax1


Region 1

Region 1 is highlighted in diagram (3), it includes the interior of the Circle-segment. For region 1, the coordinates for point P are transformed to coordinate system 1 (shown in diagram (4)).

Diagram(3) and (4)

The coordinates for point P in coordinate system 1 are:

Px1 = Px cos(beta-alpha) + Py sin(beta-alpha)

Py1 = Py cos(beta-alpha) - Px sin(beta-alpha)

Using goniometric relations this can be simplified to:

Px1 = Px {cos(alpha)cos(beta) + sin(alpha)sin(beta)} + Py {-sin(alpha)cos(beta) + cos(alpha)sin(beta)}

Py1 = Py {cos(alpha)cos(beta) + sin(alpha)sin(beta)} - Px {-sin(alpha)cos(beta) + cos(alpha)sin(beta)}


Point P is inside region 1 if

Px1 >= 0 , Py1 >= 0 and Py1 <= Px1 tan(2alpha)

The Circle-segment and the circle then intersect if

Px2 + Px2 <= (R + RP)2


Region 2

Region 2 is highlighted in diagram (5). For this region no coordinate transformations are necessary.

Diagram (5)

If point P is inside region 2, the circle-segment and the circle can only intersect if point B is inside the circle.

(Px - Bx)2 + (Py - By)2 <= RP2

The coordinates of point B are:

Bx = R cos(alpha + beta) = R {cos(alpha)cos(beta) - sin(alpha)sin(beta)}

By = R sin(alpha + beta) = R {sin(alpha)cos(beta) + cos(alpha)sin(beta)}

With the coordinates for B the condition can now be evaluated.


Region 3

Region 3 is highlighted in diagram (6). For this region no coordinate transformations are necessary.

Diagram (6)

Analogous to region 2, the circle-segment and the circle can only intersect if point A is inside the circle. The condition for this is:

(Px - Ax)2 + (Py - Ay)2 <= RP2

The coordinates of point A are:

Ax = R cos(-alpha + beta) = R {cos(alpha)cos(beta) + sin(alpha)sin(beta)}

Ay = R sin(-alpha + beta) = R {-sin(alpha)cos(beta) + cos(alpha)sin(beta)}

With the coordinates for A the condition can now be evaluated.


Region 4

Region 4 is highlighted in diagram (7). For this region no coordinate transformations are necessary.

Diagram (7)

Analogous to regions 2 and 3, the circle-segment and the circle can only intersect if point O is inside the circle. The condition for this is:

Px2 + Py2 <= RP2


Region 5

Region 5 is highlighted in diagram (8). For region 5, the coordinates for point P are transformed to coordinate system 2 (shown in diagram (9)).

Diagrams (8) and (9)

In the local coordinates the following conditions for P being in region 5, can be formulated:

Px2 >= 0, Px2 <= R and Py2 >= 0

The condition for the circle-segment and the circle intersecting is:

Py2 <= RP

The coordinates for P in coordinate system 2 are given by:

Px2 = (Px - Ax)cos(alpha + beta + pi) + (Py - Ay)sin(alpha + beta + pi)

Py2 = (Py - Ay)cos(alpha + beta + pi) - (Px - Ax)sin(alpha + beta + pi)

Using the goniometric relations to handle the pi term first, the coordinates become:

Px2 = -(Px - Ax)cos(alpha + beta) + (Py - Ay)sin(alpha + beta )

Py2 = -(Py - Ay)cos(alpha + beta) - (Px - Ax)sin(alpha + beta )

Now using the goniometric relations to seperate alpha and beta:

Px2 = -(Px - Ax){cos(alpha)cos(beta) - sin(alpha)sin(beta)} + (Py - Ay){sin(alpha)cos(beta) + cos(alpha)sin(beta)}

Py2 = -(Py - Ay){cos(alpha)cos(beta) - sin(alpha)sin(beta)} - (Px - Ax){sin(alpha)cos(beta) + cos(alpha)sin(beta)}

With the coordinates for A, calculated for region 3, the conditions can now be evaluated.


Region 6

Region 6 is highlighted in diagram (10). For region 6, the coordinates for point P are transformed to coordinate system 3 (shown in diagram (11)).

Diagrams (10) and (11)

In the local coordinates the following conditions for P being in region 6, can be formulated:

Px3 >= 0, Px3 <= R and Py3 >= 0

The condition for the circle-segment and the circle intersecting is:

Py3 <= RP

The coordinates for P in coordinate system 3 are given by:

Px3 = Pxcos(alpha + beta) + Pysin(alpha + beta)

Py3 = Pycos(alpha + beta) - Pxsin(alpha + beta)

Now using the goniometric relations to seperate alpha and beta:

Px2 = Px{cos(alpha)cos(beta) - sin(alpha)sin(beta)} + Py{sin(alpha)cos(beta) + cos(alpha)sin(beta)}

Py2 = Py{cos(alpha)cos(beta) - sin(alpha)sin(beta)} - Px{sin(alpha)cos(beta) + cos(alpha)sin(beta)}

The conditions can now be evaluated.