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

Bezier Curve, I am doing it wrong!

Discussion in 'Scripting' started by Sea_Laszlo, Jul 15, 2014.

  1. Sea_Laszlo

    Sea_Laszlo

    Joined:
    Jun 13, 2013
    Posts:
    12
    Hello, today I thought it would be neat to curve a line renderer, so I took to Google and a few hours later I think I have something...

    Code (CSharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. [ExecuteInEditMode()] //EquivalenttoJS@scriptExecuteInEditMode()
    6. publicclassBezierCurve : MonoBehaviour
    7. {
    8.  
    9. publicGameObjectstart, control, end;
    10. publicintnumberOfPoints = 20;
    11. privateLineRendererlineRenderer;
    12. privateVector3p0, p1, p2;
    13.  
    14. //Usethisforinitialization
    15. voidStart ()
    16. {
    17. //Getthelinerendererattachtedtothescript
    18. lineRenderer = GetComponent<LineRenderer>();
    19.  
    20. }
    21.  
    22. //Updateiscalledonceperframe
    23. voidUpdate ()
    24. {
    25. if (numberOfPoints > 0)
    26. {
    27. lineRenderer.SetVertexCount(numberOfPoints); //Be sure to match the vertex count to the points
    28. }
    29.  
    30. p0 = start.transform.position;
    31. p1 = control.transform.position; //The control point helps shape the curve
    32. p2 = end.transform.position;
    33.  
    34. for(inti = 0; i < numberOfPoints; i++)
    35. {
    36. float t = (float)i / (float)(numberOfPoints - 1);
    37.  
    38. //http://en.wikibooks.org/wiki/Cg_Programming/Unity/Bézier_Curves
    39. //B(t) = (1-t)2 * p0 + 2(1-t)* t * p1 + t^2 * p2
    40. //Be sure to check your Vector3 axis
    41. Vector3position = (1.0F - t) * (1.0F - t) * newVector3(p0.x,0,p0.z)
    42. + 2.0F * (1.0F - t) * t * newVector3(p1.x,0,p1.z)
    43. + t * t * newVector3(p2.x,0,p2.z);
    44.  
    45. lineRenderer.SetPosition(i,position); //Assign each new position via for loop
    46. }
    47.  
    48. }
    49.  
    50. }
    51.  
    As it stands right now, this thing will paint you a sweet pink straight line and not the awesome curve. I think I missing something, so I decided to post up what I have while re-going over the wiki. Basically line SetPositions don't seem to account for the control point, so the end and control are identical.

    Okay, it was pretty late last night when I decided to type this up. The first thing I discovered this morning, my Vector3's were wrong. I was using x and y, not x and z. Since all of my points where zero on the y axis, I was only getting back a straight line. The script works perfectly now, just add it to a game object, and add a line renderer.
     
    Last edited: Jul 15, 2014
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Im not sure how to interpret the square brackets in the algorithm, but your time calculation looks wrong.

    I think it should be i/numberOfPoints (ie: value between 0-1 aka percentage)

    It also looks like youre using the wrong points in your calcuation... looks to me like youre using p0, p1, p3 rather than p0,p1,p2 which is what the formula wants
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Code (csharp):
    1. float t = (float)i / (float)(numberOfPoints - 1);
    If you don't cast those two int as float, you will always get 0 or 1.
     
  4. Sea_Laszlo

    Sea_Laszlo

    Joined:
    Jun 13, 2013
    Posts:
    12
    Yeah, I should have caught that earlier.
     
  5. Sea_Laszlo

    Sea_Laszlo

    Joined:
    Jun 13, 2013
    Posts:
    12
    I was under the impression that p1was the control point? Also, I found a much better article on the subject as it pertains to programming. I think every thing on this page is Java;
    http://en.wikibooks.org/wiki/Cg_Programming/Unity/Bézier_Curves
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    looks like it has the entire code solution there for you...
     
  7. Sea_Laszlo

    Sea_Laszlo

    Joined:
    Jun 13, 2013
    Posts:
    12
    AH, yeah thanks for pointing that out. Anyway, It's a little late here so I am headed to bed but I will post up what I got next morning. I also noticed the wiki uses a Vector3 straight up in their Bezier calculation, I tried breaking it up by X and Y, and then create a new Vector3 for it to work with C#.

    I had my axis wrong in my Vector3's, new Vector3(x,0,y) hence the straight-line. I changed the code (above) so it shows the correct Vector: new Vector3(x,0,z). Also, I didn't realize you could multiply Vector3s in Java without specifying a particular axis.

    Code (JavaScript):
    1.  position = (1.0 - t) * (1.0 - t) * p0
    2.          + 2.0 * (1.0 - t) * t * p1
    3.          + t * t * p2;
     
    Last edited: Jul 15, 2014