How do I Find the edges of a square?

0 favourites
  • 8 posts
From the Asset Store
Gravity Square is a game where I aim to reach the square at the polka dot door :)
  • I want to find the edges of a square depends on the position of the player.

    Here is a picture of what I'm talking about.

    1, 2, 3 and 4 are ImagePoints. So depends on the player's position, the edges are different. In the left one, 3 and 4 are the edges, In the middle 3 and 2 and in the right 1 and 4 are the edges. There are overall 8 possibilities, I just showed 3 of them. I would appreciate if someone make a capx file and uplaod it.

    Thank you

  • Tricky.......... here is my attempt. https://www.dropbox.com/s/vbn9xluozgmf8 ... .capx?dl=0

    I don't know if this is a good way to go about it though, in terms of efficiency or logic.

    I've saved each imagepoints' angle difference from the center point to an array, then sorted the array to get the two points with the largest difference.

    EDIT: Updated original capx to clean it up, and now also works for any number of imagepoints/vertices.

  • Tricky.......... here is my attempt. https://www.dropbox.com/s/vbn9xluozgmf8 ... .capx?dl=0

    I don't know if this is a good way to go about it though, in terms of efficiency or logic.

    I've saved each imagepoints' angle difference from the center point to an array, then sorted the array to get the two points with the largest difference.

    EDIT: Updated original capx to clean it up, and now also works for any number of imagepoints/vertices.

    Thank you, this is what I needed.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • One slightly different way would be to get the angle from each imagepoint to the player and subtract from that the angle to the next imagepoint.

    Or in the simple case of a square and the imagepoints are layed out in this order:

    41

    32

    It simplifies to:

    set a to angle(Target.ImagePointX(loopindex+1),Target.ImagePointY(loopindex+1),Sprite.X,Sprite.Y) -90*loopindex

    Then if the following is true for any imagepoint you can create it.

    (-sin(a)<0 & cos(a)>0) | (cos(a)<0 & -sin(a)>0)

  • Made a capx for myself to help understand/visualize above approach, leaving it here.

    https://www.dropbox.com/s/stuquplu2gph1 ... .capx?dl=0

  • Thanks guys, great solutions, really helped to solve my problem.

  • Marked this, so i can find it back. Sorry to bother.

  • I guess it would probably be haelpful to expalin the formula.

    Here's a general view for one corner. P1 is the current corner and P0,P2 are the previous and next corners. "A" is the location of the player.

       A
      /
     /
    P1----P2
    |     |
    |     |
    |     |
    P0----+
    
    So next I made the obsevation that the current point (A) would only need to be created if it's in either of these two regions (b or c).
    
         .........
         .........
         bbbbbbb..
         bbbbbbb..
         bbbbbbb..
    ..cccP1---+
    ..ccc|    |
    ..ccc|    |
    ..ccc+----+
    ..ccc
    .....
    .....
    
    To calculate which region I used a vector dot product to calculate a vector projection.  That sounds more vague than it is but it looks like this. It works relative to any corner.
    
    (A-P1) dot (P0-P1) < 0 and 
    (A-P1) dot (P2-P1) > 0
    or
    (A-P1) dot (P2-P1) < 0 and
    (A-P1) dot (P1-P1) > 0
    [/code:2mpj4t2g]
    The rest was a lot of math simplification.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)