0

I've been struggling the last few days to make a procedurally generated mesh that can bend, aka a road. The road script has a function that makes it turn at a given angle, and a function that lets it go forward at the given angle. My problem arises when trying to make a right turn after having already moved forward (as shown in the picture below).

I can't for the life of me figure out the issues in my code, but I'm assuming that I am making an accounting error with the x and y coordinates somewhere.

enter image description here

using UnityEngine;
using System.Collections;


public class procPlane : MonoBehaviour {

    public Mesh mesh;
    public Vector3[] vertices;
    public int currentVertice;
    public Vector3[] normales;
    public Vector2[] uvs;
    public int[] triangles;
    public int currentTriangle;

    //current vertice number
    public int verticeNumber = 4;

    //current x and z outer values.
    public float x_out = -0.5f;
    public float z_out = 0.1f;

    public float angle = 0.0f;

    private float x_in = 0.5f;
    public float z_in = 0.1f;

    public float xi=0.5f, xo=-0.1f, zi=0.1f, zo=0.1f;

    // Use this for initialization
    void Start () {

        // You can change that line to provide another MeshFilter
        MeshFilter filter = gameObject.AddComponent< MeshFilter >();
        mesh = filter.mesh;
        mesh.Clear();

        #region Vertices        
        vertices = new Vector3[1000];
        currentVertice = 0;

        vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, -0.1f);
        vertices[currentVertice++] = new Vector3(0.5f, 0.0f, -0.1f);
        vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, 0.0f);
        vertices[currentVertice++] = new Vector3(0.5f, 0.0f, 0.0f);
        vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, 0.1f);
        vertices[currentVertice++] = new Vector3(0.5f, 0.0f, 0.1f);
        #endregion

        #region Normales
         normales = new Vector3[ vertices.Length ];
        for( int n = 0; n < normales.Length; n++ )
            normales[n] = Vector3.up;
        #endregion

        #region Triangles

        triangles = new int[2*6*100];
        currentTriangle = 0;
        triangles[currentTriangle++] = 2;
        triangles[currentTriangle++] = 1;
        triangles[currentTriangle++] = 0;
        triangles[currentTriangle++] = 2;
        triangles[currentTriangle++] = 3;
        triangles[currentTriangle++] = 1;

        triangles[currentTriangle++] = 4;
        triangles[currentTriangle++] = 3;
        triangles[currentTriangle++] = 2;
        triangles[currentTriangle++] = 4;
        triangles[currentTriangle++] = 5;
        triangles[currentTriangle++] = 3;

        #endregion

        mesh.vertices = vertices;
        mesh.normals = normales;
        mesh.uv = uvs;
        mesh.triangles = triangles;

        mesh.RecalculateBounds();
        mesh.Optimize();
    }

    // Update is called once per frame
    void Update () {
        if(Input.GetKeyDown(KeyCode.UpArrow))
        {

            Vector3 A = Quaternion.Euler(0,angle,0)*Vector3.forward*0.1f;

            x_out+=A.x;
            x_in+=A.x;
            z_in += A.z;
            z_out += A.z;
            Debug.LogWarning(z_in);
            xo += A.x;
            xi += A.x;

            zo += A.z;
            zi += A.z;

            vertices[currentVertice++] = new Vector3(x_out, 0.0f, z_out);
            vertices[currentVertice++] = new Vector3(x_in, 0.0f, z_in);

            //normals

                normales[currentVertice] = Vector3.up;

            //triangles
            verticeNumber += 2; 
            triangles[currentTriangle++] = verticeNumber;
            triangles[currentTriangle++] = verticeNumber-1;
            triangles[currentTriangle++] = verticeNumber-2;
            triangles[currentTriangle++] = verticeNumber;
            triangles[currentTriangle++] = verticeNumber+1;
            triangles[currentTriangle++] = verticeNumber-1;

            mesh.vertices = vertices;
            mesh.normals = normales;
            mesh.uv = uvs;
            mesh.triangles = triangles;

            mesh.RecalculateBounds();
            mesh.Optimize();
        }
        if(Input.GetKeyDown(KeyCode.RightArrow))
        {

            //z_out += z_out;
            //z_in += z_in;
            angle += 9.0f;

            float x_outer = (float)-(2.0f * Mathf.Cos(angle*Mathf.PI/180.0f))+1.5f;
            float z_outer = (float)(2.0f * Mathf.Sin(angle*Mathf.PI/180.0f));

            x_out =  x_outer;

            //Debug.LogWarning(z_out);

            z_out = zo + z_outer;


            float x_inner = (float)-(1.0f * Mathf.Cos(angle*Mathf.PI/180.0f))+1.5f;
            float z_inner = (float)(1.0f * Mathf.Sin(angle*Mathf.PI/180.0f))-0.1f;


            //x_in = x_in - (x_inner+(x_out));
            x_in = x_inner;
            z_in = zi + z_inner;
            Debug.LogWarning(zi);
            Debug.LogWarning(z_inner);
            //outer vertice
            vertices[currentVertice++] = new Vector3(x_out, 0.0f, z_out);
            //inner vertice
            vertices[currentVertice++] = new Vector3(x_in, 0.0f, z_in);

            //normals
            normales[currentVertice] = Vector3.up;

            //triangles
            verticeNumber += 2;
            triangles[currentTriangle++] = verticeNumber;
            triangles[currentTriangle++] = verticeNumber-1;
            triangles[currentTriangle++] = verticeNumber-2;
            triangles[currentTriangle++] = verticeNumber;
            triangles[currentTriangle++] = verticeNumber+1;
            triangles[currentTriangle++] = verticeNumber-1;

            mesh.vertices = vertices;
            mesh.normals = normales;
            mesh.uv = uvs;
            mesh.triangles = triangles;

            mesh.RecalculateBounds();
            mesh.Optimize();
        }
    }
}
Oliver
  • 111
  • 2
  • 10

1 Answers1

0

Not sure but you could be running into the Gimbal Lock problem of using Quaternion.Euler. Check what your angle is when the math goes haywire.

Check this as well...

http://answers.unity3d.com/questions/573035/avoiding-gimbal-lock.html

Ryan

Ryan Jones
  • 111
  • 1
  • 9