0
\$\begingroup\$

I want to detect if my GameObject (Vector3 Point) being dragged by the player's finger is moving in a circular fashion.

I thought I could save the last 5 points every 10 frames and join them by lines. Then I'd project them into a "defined" plane. From there, I can form the perpendicular bisectors of those line segments - to check where they are intersecting.

If every intersection point is very nearby - the finger is swiping in a circle!

Diagram of proposed strategy using perpendicular bisectors

Now I only need to check if the intersection point is close.

How can I check if they intersect? Or is there a better way to solve this?

Here's my code so far:

public LinkedList<Vector3> FingertipPoints;
public Transform Fingertip;

private void Start()
{
    FingertipPoints = new LinkedList<Vector3>();

    FingertipPoints.AddFirst(new Vector3(0, 0, 0));
    FingertipPoints.AddLast(new Vector3(0, 0, 0));
    FingertipPoints.AddLast(new Vector3(0, 0, 0));
    FingertipPoints.AddLast(new Vector3(0, 0, 0));
    FingertipPoints.AddLast(new Vector3(0, 0, 0));
}

[Range(1,60)]
public int FramesPerCheck = 10; // 6checks per sec
private int Frame;
// Update is called once per frame
void LateUpdate()
{
    Frame++;
    if (FramesPerCheck == Frame)
    {
        Frame = 0;
        FingertipPoints.RemoveLast();
        FingertipPoints.AddFirst(Fingertip.position);
    }

    //Get the Direction Vectors
    Vector3 V1 = FingertipPoints.First.Value - FingertipPoints.First.Next.Value;
    Vector3 V2 = FingertipPoints.First.Next.Value - FingertipPoints.First.Next.Next.Value;
    Vector3 V3 = FingertipPoints.First.Next.Next.Value - FingertipPoints.First.Next.Next.Next.Value;
    Vector3 V4 = FingertipPoints.First.Next.Next.Next.Value - FingertipPoints.Last.Value;

    //Project them on a plane
    V1 = Vector3.ProjectOnPlane(V1, Fingertip.forward);
    V2 = Vector3.ProjectOnPlane(V2, Fingertip.forward);
    V3 = Vector3.ProjectOnPlane(V3, Fingertip.forward);
    V4 = Vector3.ProjectOnPlane(V4, Fingertip.forward);

    //Get the Perpendicular Vectors (is this even right?)
    Vector3 C1 = Vector3.Cross(V1, Fingertip.forward);
    Vector3 C2 = Vector3.Cross(V1, Fingertip.forward);
    Vector3 C3 = Vector3.Cross(V1, Fingertip.forward);
    Vector3 C4 = Vector3.Cross(V1, Fingertip.forward);

    //???
    //How to check if they intersect nearby??
    //

}
\$\endgroup\$
  • \$\begingroup\$ Why not just check wether the movement direction changes 360 degrees? \$\endgroup\$ – Bálint Aug 9 '18 at 16:05
0
\$\begingroup\$

The idea sounds interesting, but it might be too complicated. Depending on the type of steering mechanic used in your game, you could restrict the input so that circular motion is easy to control for the player (e.g. by simple left/right joystick/button input to go left/right on a circular arc). That would detection also make much easier.

If you use free motion, which makes it harder to steer the object on a circlar path, the detection must also be more general. A problem I do see with your idea above, is that the "normal rays" do only intersect in one single point if the motion is perfect circle. Otherwise you'll get multiple different intersection points... However, you could average the resulting intersection points of all the ray pairs to get an estimate I guess. If they are close together, and all on the same side of the input line segments, then the center/radius of a circle can be calculated/estimated to match the input approximately.

Geometry: Line-Line / Ray Intersection You'll find lots of tutorials and examples on the webs and also here Stack Overflow on how to do this. (If I remember correctly, there's also something in Unity called a Ray2D, maybe that can be used somehow too.)

Oh, but wait, I think I get it now: you want to detect if a touch motion path can be approximated with a circular arc: Circular gesture detection? - In that case, this detection method seems to be very promising!

Edit: Related topic: Simple circular gesture detection

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.