Angle() between angles?

0 favourites
  • 5 posts
  • Hello everyone,

    I have a quick question; I need to test if 3 points are collinear (with a bit of tolerance). So, what I am doing is finding the angle() between a base point and another point, and the base point and the last point. If these are within a certain range of each other, it needs to return true.

    I tried to do this, but I can't find a way to test if the angle()s are within a range of each other. I used the 'is within range', which works most of the time, however it does NOT work when I encounter the +/- 180 gap given by the angle() expressions.

    Any advice?

  • Angle() gives you the angle between two points, so if you are going beyond that try reversing the points.

  • newt, I am not going beyond that. I merely wish to compare two angle()s to see how close they are, but it gets hairy when I encounter the +/- 180 gap.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • you could put your points in a family fpoint

    and then

    Global alignement = 0
    +[pick the three points you want to check with the object type point]
    +[pick the three points you want to check with the family fpoint]
    +system: pick point at random
      local number a1 = 0  // angle between point and the first fpoint
      local number a2 = 0  // angle between point and the second fpoint
      +system: for each fpoint
      +system: point.UID != fpoint.UID // don't pick itself
        +loopindex = 0
          ->system: set a1 to angle(point.X,point.Y,fpoint.X,fpoint.Y)
        +loopindex = 1
          ->system: set a2 to angle(point.X,point.Y,fpoint.X,fpoint.Y)
      -> System: set alignement to min(1,abs(cos(a1)*cos(a2)+sin(a1)*sin(a2)))

    Basically it should look at one of your 3 points and calculate the angle between the other two.

    So if the point randomly picked is at one extremity, the angle between this point and the two others should be the same.

    If the point randomly picked is the middle one, the angle should be opposite.

    To compare alignement between two direction (or vectors) there's a neat operation called dot product.

    If done on unit vectors (the ones with length = 1) you get a number from -1 to 1:

    * 1 = aligned and pointing toward the same direction

    * -1 = aligned and pointing toward opposite direction

    * 0 = perpendicular

    and between these values it would be a "percentage of alignement"

    The formulat is:

    given two vectors v1(x1,y1) and v2(x2,y2)

    the dot product v1.v2 = x1*x2+y1*y2

    If you have an angle 'a', the corresponding unit vector would be (cos(a),sin(a))

    That's why cos(a1)*cos(a2)+sin(a1)*sin(a2) should give you the dot product.

    the abs() is to have values from 0 to 1 that should give you a percentage of alignement.

    you then just have to check if 'alignement' is greater than something lik 0.9 and you're done (:

  • Thanks a lot Yann! I'll see what I can do.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)