# How to detect if object is moving in clockwise- or counterclockwise direction?

I've read this one, but I need more info: rotating an object from sourceAngle to destAngle, both 0-359, clockwise or counter clockwise?

I have a ball. The user is able to drag the ball in any direction clockwise/anticlockwise around a circle. I need to know whether or not the ball was dragged clockwise or anticlockwise.

I'd like to get both vector/non-vector methods.

• If the angle was 90 one frame, and 270 the next, which way is it rotating? i.e. you'd have to assume it moved in the direction of the minimum rotation, then have some default for 180 degree turns. – George Duckett Jan 9 '12 at 11:52
• please see edit – Vishnu Jan 9 '12 at 11:55
• i also have a middle point – Vishnu Jan 9 '12 at 14:21
• I'm a little confused here, what data do you have? You talk about both angles and points, one must be derived from the other. Don't tell us about derived data, just the data that you have and the result you need. Anyone can compute the middle point between two points, but that doesn't actually solve the problem @GeorgeDuckett states. In any case the answer to that is that this question probably calls for 4 different outputs, right rotation, left rotation, no rotation and exactly 180 degrees rotation. – aaaaaaaaaaaa Jan 9 '12 at 14:39
• @eBusiness: Well, simple take this as I have user dragging a ball around a circle. I need to know in which direction clockwise/anticlockwise he did drag it – Vishnu Jan 9 '12 at 15:01

Keep track of the position of the ball on the previous frame. Then let's say we have:

A
|\                // A = Rotation Center
| \               // B = Previous Frame Position
|  C              // C = Current Frame Position
B


You need to check using the 2D analog of a cross product whether C lies to the left or to the right of the AB line segment. If it's to the right, then it's rotating clockwise. If it's to the left, then it's rotating counter-clockwise.

You can use this method to check:

bool isLeft(Vector2 a, Vector2 b, Vector2 c)
{
return ((b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x)) > 0;
}

• I find a problem arising sometimes though, now ! can you brief. Some times it shows 1 when I really require -1 – Vishnu Jan 10 '12 at 9:01
• What if user drags the ball 270 degree clockwise? In this case, your solution will determinates that the ball rotate reverse clockwise 90 degree, Right? – Nguyen Minh Binh May 30 '13 at 17:13

Given the points Source, Destination and Centre, where a move has happened from Source to Destination first compute the vectors:

CentreSource = Source - Centre
CentreDestination = Destination - Centre


Compute the dot product of the tværvector of CentreSource and CentreDestination:

RorL = CentreSourceX * CentreDestinationY - CentreSourceY * CentreDestinationX


Now RorL should be positive if the point is moving counterclockwise around the centre, negative in case of clockwise, and 0 if the move was either 0 or 180 degrees.

You can further check if the move was above or below 90 degrees by computing the dot product of CentreSource and CentreDestination:

AorB = CentreSourceX * CentreSourceY + CentreDestinationX * CentreDestinationY


If AorB is positive then the move was below 90 degrees, if it is negative the move was above 90 degrees, and if it is 0 then the move was either exactly 90 degrees or it either started or ended in the point Centre.

• What's a tværvector? Can't seem to find any reference to it online, besides your posts. – David Gouveia Jan 9 '12 at 15:43
• There are no other references. First hit on Google gives you the definition (unless of course if this post should overtake it). No one by the way seems to be using the term 2D cross product either, the page you link to call it a determinant, then you can follow a link to a a page about determinants where it is written that "Determinants are defined only for square matrices.". That is not exactly coherent. Using the word tværvector we can at least talk about the maths without having to touch this linguistic mess. – aaaaaaaaaaaa Jan 9 '12 at 16:14

How I would achieve this is to have a stack of 3 positions and use these to determine rotation of the object.

example:

double[] position = new double[2];

double[][] positions = new double[3][2];
void bool clockwise () {
for (int i = 0; i < 2; i++) {
positions[i + 1][0] = positions[i][0];
positions[i + 1][1] = positions[i][1];
}
positions[0][0] = position[0];
positions[0][1] = position[1];
double[] averageOfEnds = new double[]{ (positions[0][0] + position[2][0]) / 2, (positions[0][1] + position[2][1]) / 2 };
if (positions[2][0] < averageOfEnds[0] || positions[2][1] > averageOfEnds[1]) {
return true;
}
return false;
}


this assumes you axes are like

+
|
|
|
- ------ +


Starting with the representation:

[x2, y2]' = R(th) * [x1 y1]';
R(th) = [cos(th) -sin(th)
sin(th)  cos(th)]


Which is basically a rotation operation on a vector. x2 and y2 are the rotated position of x1 and y1. The idea is to get cos(th) and sin(th) in terms of x1,x2,y1 and y2 variables. In order not to fall in to wrong quadrant, atan2 is used with the values of sin(th) and cos(th) which boils down to:

  x1 = cos(th1); y1 = sin(th1);
x2 = cos(th2); y2 = sin(th2);

diff = atan2((x1*y2 - x2*y1), (y1*y2 + x1*x2)); # atan2(Yterm,Xterm) (Matlab notation


Derivation:

The easiest solution would be to keep a reference to the object's previous and current or previous, current and extrapolated next positions and do a simple compare.

• Can you explain at least a little more. Thanks for the effort, though – Vishnu Jan 9 '12 at 11:42