- Home /
How to tell if an object is moving in a circle?
I'm working on a 3D game as a hobby and have been having a lot of trouble figuring out how I would do this.
The player character can run super fast. I want them to be able to run in a circle a couple times and have it simulate a tornado, but I can't figure out how to track if the player is moving in a circle.
I'm not the best with geometry. Is there some sort of algorithm or formula to see if one object has been moving in a circlular motion based on a handful of points? I was thinking maybe something like you would see in a phone game where you can drag your finger in a circle, except it would be a gameobject.
Please help.
Answer by BastianUrbach · Apr 14, 2018 at 09:51 PM
First of all, we need to consider always at least four points at the same time (since one two or three points always lie on some circle). So lets take four consecutive points and test if they lie on a circle. This is quite simple if the speed of the player (and thereby the distance between the points) is constant. In that case you just need to check if the angle (p1, p2, p3) is the same as (p2, p3, p4). Or in code:
bool wasCircular = Mathf.Abs(Vector3.SignedAngle(p1 - p2, p3 - p2, Vector3.up) - Vector3.SignedAngle(p2 - p3, p4 - p3, Vector3.up)) < threshold;Repeating this for multiple groups of four points should give fairly reliable results. So for example you would do this for (p1, p2, p3 p4) then (p2, p3, p4, p5) then (p3, p4, p5, p6) and so on.
If the speed was not constant it gets a bit more difficult. One approach would be determining a center based on three points and then testing if the distance to that center remained constant and if some angle around the center has been covered with the sign of that angle remaining the same (otherwise we would also detect circular motion if the player is standing still or switched direction).
One thing to keep in mind is that every four points we test should have been sampled at regular intervals that are long enough that some meaningful angle should have been covered (e.g. 30°). Otherwise we would have to set the threshold extremely low to avoid detecting a straight line as circular motion and a low threshold would mean the circle has to be near perfect, which is unlikely to happen.
Answer by drv14 · Apr 14, 2018 at 08:14 PM
You could divide your screen into quadrants with a collider taking up each quadrant, then see if the player hits the colliders in order.
You could modify the colliders into a flat torus shape or something to avoid shenanigans at the center.