# 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;

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


this assumes you axes are like

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


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