-
Detecting an object’s direction change
Hi all:
I’m trying to write an expression to detect whether an object in motion has changed its direction or not. The way I’m trying to do it is:
1) Read the object’s X/Y position 0.5 seconds and 0.2 seconds before the current time, as well as its current position.
2) Calculate the difference between the object’s X position 0.5 sec. ago and 0.2 sec. ago (let’s call it Mov1X), and the difference between its X position 0.2 sec. ago and now (let’s call it Mov2X).
3) If Mov1X and Mov2X have different signs, then the object has changed directions. I.e.: the object was moving in the X-positive direction in the 0.5 sec. ago – 0.2 sec. ago interval, but in the X-negative direction in the 0.2 sec. ago – present interval. In that case, Mov1X would be positive, while Mov2X would be negative.
4) Do the same for the Y position, obviously.Now, my problem is that I can’t think of a way to compare the signs of two numbers. I don’t need their absolute values or anything, just to know whether one is positive and the other negative or viceversa. I’m sure that this is very simple, but I just can’t think of a way to do it. Anyone can help? (Alternatively, is there a more elegant way to detect direction changes?)
Paulo.