Search Unity

Brachistochrone Mesh shows weird behavior.(noob)

Discussion in 'Scripting' started by Mini_alfa, Oct 6, 2019.

  1. Mini_alfa

    Mini_alfa

    Joined:
    Dec 1, 2017
    Posts:
    1
    I am trying to build a plane mesh of the brachistochrone curve in unity. But when i start the game some triangles attach to wrong position (see below). I am just into C# and unity and have no idea what i am doing wrong.




    The long triangle going towards the screen shouldn't be there.

    And here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(MeshFilter))]
    6. public class terain : MonoBehaviour
    7. {
    8.     Mesh mesh;
    9.     Vector3[] vertices;
    10.     int[] triangles;
    11.     public int ySize;
    12.     public int xSize = 2;
    13. // some variables for the brachistochrone equation
    14.     public float increment = 1.0f;
    15.     public float distance = 1.0f;
    16.     public float height = 1.0f;
    17.  
    18.  
    19.     // Start is called before the first frame update
    20.     private void Awake()
    21.     {
    22.         Generate();
    23.     }
    24.  
    25. //generates mesh
    26.     private void Generate()
    27.     {
    28.         GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    29.         mesh.name = "Procedural Grid";
    30. // the zSize is equal to pi / increment  (+0.5 to add an extra index just to be sure)
    31.         int zSize = (int)(Mathf.PI / increment + 0.5f);
    32.         vertices = new Vector3[(xSize + 1) * (zSize + 1)];
    33.         float y = 1.0f;
    34. //phi devines x and z
    35.         float phi = 0.0f;
    36.         int z = 0;
    37.         for (int i = 0; phi <= Mathf.PI; phi=phi+increment)
    38.         {
    39.             z = (int)(10 * 0.5f * distance * (phi - Mathf.Sin(phi)));
    40.             y = 10 * -0.5f * height * (1 - Mathf.Cos(phi));
    41.  
    42.             for (int x = 0; x < xSize; i++, x++)
    43.             {
    44.                 int yInt = (int)y;
    45.                 vertices[i] = new Vector3(x, yInt, z);
    46.             }
    47.            
    48.         }
    49. // No idea what the rest of the code means
    50.         mesh.vertices = vertices;
    51.         z = 0;
    52.         int[] triangles = new int[xSize * zSize * 6];
    53.         for (int ti = 0, vi = 0; z < zSize; z++, vi++)
    54.         {
    55.             for (int x = 0; x < xSize; x++, ti += 6, vi++)
    56.             {
    57.                 triangles[ti] = vi;
    58.                 triangles[ti + 3] = triangles[ti + 2] = vi + 1;
    59.                 triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
    60.                 triangles[ti + 5] = vi + xSize + 2;
    61.             }
    62.         }
    63.         mesh.triangles = triangles;
    64.  
    65.         MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
    66.  
    67.  
    68.         meshc.sharedMesh = mesh;
    69.     }
    70.    
    71. }
    72.  

    Normally i would try to figure it out myself, but my time is limited because its for a school project. Somebody knows what i am doing wrong?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome.
    I'm rather tired right now, but since you wrote you are in a hurry i thought it's better than nothing.

    Your code for generating the triangle entries is very confusing. Generally speaking, these double-assignments in one row are bad practice. There is no practical benefit to doing so, other than saving a minimum amount of lines, in a tradeoff for worse maintainability, readability and higher perceived complexity. Try to keep it simple and easy to read.

    That said, you are somehow forming triangles between (i assume) the first and last vertice entries. But that's not the only problem! Your mesh also has indentations, as can be seen in the middle of the second screenshot. Both phenomenon come down to the same problem tho, which is most likely rooted in your triangle generation. So cleaning up that code should be your first priority.
    Generally speaking, you want to create some list of vertices, and then generate the corresponding triangles to connect these vertices in square-shapes. Each square is made up of 2 triangles, each of which is made up of 3 vertices, for a total of 6 vertices per square. I'd also start by generating a plane, because you only need to change the positions of the vertices from there to create any shape you want - for example your curve - as soon as the base mesh generation works. And for that there are nice tutorials, so i'll link you one:


    If you follow and reproduce what it done in the video, you will be able to create a mesh. Afterwards you only need to rearrange the vertices in the shape of your curve, to create the desired outcome.

    Sincerely hope this helps, as i know how much it sucks to stumble into a problem short before some deadline.
    Wish you best of luck! :)