How to detect when distance is calculated and it reached to specific pixels

0 favourites
  • 7 posts
From the Asset Store
Change the size and position of everything without calculating anything!
  • I am calculating distance from touch start to current position of touch. then I want to trigger when distance is crossed every 50 pixel. i.e. when distance reached 50px then trigger it, when 100 pixel crossed then trigger it.

    I want to do something where its triggered. How can I achieve this?

  • | Global number StartY‎ = 0

    | Global number StartX‎ = 0

    + Touch: On any touch start

    -> System: Set StartX to Touch.X

    -> System: Set StartY to Touch.Y

    -> Text: Set text to "Touch Started"

    + Touch: Is in touch

    + System: (int(distance(Touch.X, Touch.Y, StartX, StartY) ÷ 10) × 10) % 50 = 0

    + System: Trigger once

    -> Text: Set text to int(distance(Touch.X, Touch.Y, StartX, StartY) ÷ 10) × 10

    Try something like this. Compare the distance using the current touch values and the original touch values using the following equation. This will return true if the distance rounded to the nearest 10 is a multiple of 50.

    (int(distance(Touch.X, Touch.Y, StartX, StartY) ÷ 10) × 10) % 50 = 0

    I do it this way because if you just check to see if the distance is a multiple of 50 and the touch moves fast enough, it will miss the 1 pixel where it will be true. Doing this gives a 10 pixel area that will register as true instead of just 1.

  • hello InDWrekt thanks for your answer.

    this is triggering after 3-5 pixels. How do I setup it for my custom pixel (for example 50px distance).

    and I'm also not sure how its working. for example If distance is 300px then how these events working

    can you explain how this event working?

  • If it's triggering that early, you may need to add a check to ensure the distance is > 10. The modulo operator (%) can return 0 if the the number to the left = 0.

    Here's what is going on:

    distance(Touch.X, Touch.Y, StartX, StartY) gives the distance in pixels the touch has traveled.

    int(distance(Touch.X, Touch.Y, StartX, StartY) ÷ 10) next we divide by 10 and get just the integer value. This removes all but the number in the 10s position. IE, if the distance, is 10, 11, 15, etc..., this returns 1.

    int(distance(Touch.X, Touch.Y, StartX, StartY) ÷ 10) × 10 we then multiply by to to get it back to a multiple of 10. Again, we do this so there isn't just a single pixel that will trigger the event.

    (int(distance(Touch.X, Touch.Y, StartX, StartY) ÷ 10) × 10) % 50 = 0 Finally we use the modulo operator. Modulo is a division operator that returns the remainder. IE, 15 / 10 = 1.5 so 15 % 10 = 5. If the modulo returns 0, the original value is a multiple of the divisor. 20 % 10 = 0 because there is no remainder so, 20 is a multiple of 10. In your question, why does it work for 300, 300 is a multiple of 50 so 300 % 50 = 0.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • No This is not what I want. What I want is Every 50 pixel distance I want to trigger.

    i.e 50,100,150,200,250 ... and so on.

  • That is literally what this is doing but without the problem of the trigger not happening because the player is moving too fast.

    If you only want to trigger on the 50, 100, etc... all you need is:

    distance(Touch.X, Touch.Y, StartX, StartY) % 50 = 0

    However, if the person moves their finger 3 pixels per tick, this is what will happen.

    Touch starts at x =1 y = 1.

    Drag to the right so x increases.

    At tick 18, system registers x = 49.

    At tick 19, system registers x = 52.

    The system never sees the touch x = 50.

    This isn't a hypothetical either. It is guaranteed to happen. And, this is even considering the system is only recording position in integers. Unfortunately, the system doesn't just register position in integers. You could get a value like 50.00000000000001 instead of 50 and it would not trigger your event.

    Take a look at this simple sample.

    drive.google.com/file/d/1ATDZyF5o97m_T86IXhtoVZAH6SOtKyGi/view

    If you click and drag in the green, you will get a particle effect every 50 pixels. It uses the exact equation I gave you in my previous post. If you click and drag in the red, you MAY get a particle effect every 50 pixels if the distance happens to land exactly on a multiple of 50 during a tick. This uses the distance only (but I did change it to just get the integer value to remove the decimal issue).

    Remember, timing and positioning are also heavily dependent on the system. The slower the system, the less precise therefore, more likely to miss detecting a player reaching a single pixel.

  • It was my mistake, I was using ..(..(...) % 10) × 10) % 50 = 0 instead of ..(..(...) / 10) × 10) % 50 = 0

    I was doing remainder on both side

    Thanks

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