Search Unity

Bezier Solution [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Nov 12, 2016.

  1. coolguy_platinum

    coolguy_platinum

    Joined:
    Mar 4, 2019
    Posts:
    15
    I have no idea what that means lol. Is it one of your scripts already made?
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    These are existing functions, you'll need to call them in your C# scripts (scripting knowledge is required unfortunately). BezierWalkerWithSpeed uses spline.MoveAlongSpline, for reference.
     
  3. coolguy_platinum

    coolguy_platinum

    Joined:
    Mar 4, 2019
    Posts:
    15
    Oh I see.
     
  4. singlearrowgames

    singlearrowgames

    Joined:
    Oct 14, 2018
    Posts:
    5
    @yasirkula Thanks for this great asset...

    I'm having an issue with the movement of 2d sprites. I created a spline and then added the BezierWalkerWithSpeed script to a triangle sprite. The sprite while moving is not facing the camera but some other direction.

    Is there anyway to make the sprite like in the 2d image below and over the spline? I changed the sorting layer but doesn't seem to work.
    spline.png

    spline2.png
     

    Attached Files:

  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @singlearrowgames Hi! Inside BezierWalkerWithSpeed, try changing Quaternion.LookRotations with
    Quaternion.FromToRotation(Vector3.up, segment.GetTangent())
    . Or you can add an empty parent object to the sprite and move the parent object instead of the sprite along the spline. Then you can rotate the sprite as you wish.

    Spline is rendered inside BezierSpline.OnRenderObject. At that point, you may try fiddling with the line material's ZWrite and ZTest values but I'm not sure if this issue can be resolved via that method. Alternatively you can render the spline via BezierLineRenderer and play with the LineRenderer's Sorting Layer.
     
  6. singlearrowgames

    singlearrowgames

    Joined:
    Oct 14, 2018
    Posts:
    5
    @yasirkula Thanks for the response. I will try to implement this...
     
    yasirkula likes this.
  7. bazkie_bumpercar

    bazkie_bumpercar

    Joined:
    Jul 11, 2019
    Posts:
    9
    *Update: I expected MoveAlongSpline() to be a utility function for adding distance, just like doing normalizedT += someDistance / splineLength; but I noticed it's actually very precise, so using this function I can solve my issues without having to use the evenly spaced points! So problem solved/worked around :)

    Hiya! I wonder if there's a function similar to FindNearestPointTo(), but for the evenly spaced points.

    For clarity: this is what I'm trying to achieve:
    1. Get a location on the spline, in NormalizedT units, closest to some Vec3; let's just call it normalizedT. spline.FindNearestPointTo(myVec3, out normalizedT);
    2. Move, say, 10 distance units forwards on the spline from this point on; spline.GetPoint(normalizedT + 10 / splineLength);
    However, the new location can be a bit different in distance than 10 units; so I like to use the evenly spaced points system, because for my use it needs to be a bit more precise.

    Now I can use evenly spaced points to do something like
    spline.GetPoint(evenlySpacedPoints.GetNormalizedTAtDistance(normalizedT * splineLength + 10));

    However, the "normalizedT" starting point that came from FindNearestPointTo() is different from the one that the evenlySpacedPoints wants! (makes sense, I guess!). So, I still get a wrong result.

    So I guess I need a evenlySpacedPoints.FindNearestPointTo function, unless there is maybe a way to convert between NormalizedT variables from normal <=> evenly spaced splines? Or any other creative solutions welcome :)

    I hope I explained my problem well! Cheers!
     
    Last edited: Oct 23, 2023
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @bazkie_bumpercar Sorry for the late reply. eventlySpacedPoints has 3 different distance measurements:

    - NormalizedT in [0,1] range: Same as normal spline functions, non uniform
    - Percentage in [0,1] range: Uniform normalized measurement
    - Distance in [0, splineLength] range: Uniform unit-based measurement

    You can convert between all 3 of these measurements via helper functions of evenlySpacedPoints. In your case, I think you should be calling
    evenlySpacedPoints.GetPercentageAtNormalizedT(spline.FindNearestPointTo(...)) * splineLength
    to convert NormalizedT nearest point measurement to distance measurement.
     
    bazkie_bumpercar likes this.
  9. bazkie_bumpercar

    bazkie_bumpercar

    Joined:
    Jul 11, 2019
    Posts:
    9
    Ohh I see, I did not realize Percentage was actually uniform! I thought it was the same as NormalizedT :) I had already solved my problem another way (see update on top of previous post), but now I have some more knowledge about your library, thanks!

    (and < 24h is not a late reply, so no apologies needed! you are very customer friendly :))
     
    yasirkula likes this.
  10. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    674
    hi, thank you for this cool spline tool.

    a bit confused why this is happening in some cases.
    the white line shows targetFWD, which should be direction
    also the target position should be on the right from the spline

    here's how it looks
    splinefwd.gif

    this is the code
    targetP is the position of the yellow cube
    Code (CSharp):
    1. targetP = spline.GetPoint (finalNT);
    2. if (driveDir == c.upNT) targetFWD = spline.GetTangent (finalNT);
    3. if (driveDir == c.dwnNT) targetFWD = -spline.GetTangent (finalNT);
    4.  
    5. roadUp = spline.GetNormal (finalNT);
    6. targetP -= Vector3.Cross(targetFWD.normalized, roadUp.normalized) * roadWidth;//right perpendicular
    7.  
    8. if (display.targetFwd) Debug.DrawRay (targetP, targetFWD.normalized *3);
    here's the spline section
    spline.jpg
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @bobadi Hi, this is really strange. The tangent shouldn't have pointed towards an awkward direction like that and at the moment, I don't have an explanation for that. Could you create a prefab from that spline and send it to me?
     
  12. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    674
    thanks for looking at it. the thing is, now doesn't seem to happen. I think I remember correctly and it was this spline (yes, that's the one, checked from the picture)

    the unity scene file (baffling why it's not a supported file on a unity forum)
    https://www.mediafire.com/file/1bs821g596pu0nc/splineF.zip/file

    curious if you find anything, now that it seems to be working

    other thing, it looks like the splines have a variable density of normalized-time, what formula is it to calculate smooth (if it is in the sample demo, sorry) progression of nt along spline? I already calculating coefficient by spline length, to be proportionate. (and know even points, just would like to know if there's a way to get that from normalized time)
     
    Last edited: Mar 13, 2024
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @bobadi The tangents looked correct in my Unity, as well. Happy to hear that it somehow got resolved on its own. I'm not seeing anything suspicious in the spline. BTW you could upload the zip directly to the forum thread or export the scene as unitypackage via Assets-Export Package option and upload it to the forum thread.

    BezierSpline has CalculateEvenlySpacedPoints and GeneratePointCache functions that you may find useful: https://github.com/yasirkula/UnityBezierSolution?tab=readme-ov-file#utility-functions