Search Unity

Resolved How to spread points on a line evenly

Discussion in 'Scripting' started by Aviation_Simmer, Jan 27, 2023.

  1. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    Hi! I am working on a wing script and I want it to be quite realistic, so I have to make more physic calculation per wing than just one. I kinda got the spreading working after a few painful hours, but they arent in center...
    Code (CSharp):
    1.         float interval = size.x / (PhysicSteps + 1);
    2.         for (int ps = 0; ps < PhysicSteps; ps++)
    3.         {
    4.             Vector3 offsetx = new Vector3((interval * ps) - size.x / 2, 0, 0);
    5.  
    6.             Debug.DrawRay(transform.position + offsetx + positionOffset, liftDirection * liftforce, Color.green); //Lift
    7.             Debug.DrawRay(transform.position + offsetx + positionOffset, -velocity * dragforce, Color.blue); //Drag
    8.         }
    9.  
    upload_2023-1-27_22-5-32.png
    here with less physics for better visual
    upload_2023-1-27_22-6-2.png
    thoose should be in center..
    upload_2023-1-27_22-6-53.png
     

    Attached Files:

  2. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    Im not certain i understand your issue, but it seems that you just dont add enough points. heres the math of it at least.
    you have a total of S steps, that you want to span X distance.

    if you set your interaval I = X / S (you used X / (S + 1), so adjust the following accordingly if its necesary for parity or wtvr)
    then step_0 starts at x = 0 * I = 0... this is good
    your last step, step_n-1 = step_S-1 starts at x = (S - 1) * I = (S - 1) * (X / S) which does not equal X, as desired.

    basically you want your last step to be EQUAL TO S, step_S starts at x = S * I = S * (X / S) = X
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    Well, it's difficult to reason about your code without knowing any of the values involved here. Though obvious issues are for example this line:
    Dividing by
    (PhysicSteps + 1)
    doesn't make much sense. Your ps variables goes from 0 to
    PhysicSteps - 1
    , so why do you essentially offset one side by 2. So that
    +1
    probably should be a
    -1
    . That would make your offset to actually go from
    -size.x/2
    to
    +size.x/2
    .

    What exactly is "positionOffset"? What value is size.x?

    Note that those offsets are applied in worldspace. You probably should think in local space coordinates first and then just use transform.TransformPoint to get a worldspace point if necessary. Though the overall purpose of the script is not entirely clear since it's just drawing lines at the moment. Certain physics calculations could probably be done in local space and you may just convert the final result to worldspace. Though we can't really say anything definitely without more context.
     
    Aviation_Simmer likes this.
  4. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110

    Thanks! till now I was still working on that... my brain is molten now lol
    Code (CSharp):
    1. float interval = size.x / (PhysicSteps + 1);
    2. Vector3 offsetx = new Vector3(((interval * (ps + 1)) - (size.x / 2)), 0, 0);
    3.