Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question What is the most efficient way to keep a vertex contained within a network of edges/vertices?

Discussion in 'Scripting' started by coakleyonenow, Nov 15, 2021.

  1. coakleyonenow

    coakleyonenow

    Joined:
    Aug 21, 2021
    Posts:
    13

    Given this picture, I want to move or displace the red vertex but I don't want it to pass the orange edge boundaries or the blue vertices. I either want to stop the red vertex when it gets close enough or I want to push the edge or vertex I'm closest to by the amount I'm moving. I can imagine this being a performance-heavy task and was wondering if there were any ways to implement this that would be as optimized as possible. I'm doing this through 2D mesh data in Unity.
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,112
    May I ask what effect you are trying to achieve?
    If high efficiency is a requirement you could/should look into shaders.
     
  3. coakleyonenow

    coakleyonenow

    Joined:
    Aug 21, 2021
    Posts:
    13
    I want to deform vertices during runtime without use of shaders but with mesh data. Deforming will possibly be through some key input or mouse input.
     
  4. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,112
    Okay, I wonder why the requirement is that it must not be a shader based solution.

    How many vertices are we talking about? The grid you showed seems regular. Would it suffice to represent the boundary as a circle through all the blue dots, or use a circle touching all the orange lines?

    Not knowing more about the expected end result makes suggesting solutions difficult.
     
  5. ubbelito

    ubbelito

    Joined:
    Sep 24, 2018
    Posts:
    23
    The problem formulation is made for 2d problems, such as the mesh surface in your example, so I will assume 2d for this. If you want to define the problem in 3d, you would use planes instead of lines, but the equations would otherwise be the same.

    Define a normal N for each edge. Now for any point P on the edge, compute Dot(V-P, N). Depending on how you set this up, it should be either always positive or always negative, and the thing to check for is when the sign flips. This tells you that you crossed the edge. If you get zero, that means you are on the edge.

    It is easier to pick a convention that gives you the same sign for all the edges, but the sign flipping is the signal to check for.

    In your example above, there are six edges you must not cross, so for each frame you would have to compute six dot products, and some vector subtractions.
     
  6. coakleyonenow

    coakleyonenow

    Joined:
    Aug 21, 2021
    Posts:
    13
    I'd say under 2000 vertices. I'm currently using a circular 2D mesh. The orange lines would act as a fence and the red vertex wouldn't be able to move out of the fence. It shouldn't be exactly touching the fence when stopped but rather a short distance from the fence.