Example #1
0
    public SplineMesh.CurveSample ClosestTrackSample(SplineMesh.Spline track)
    {
        int segmentA = raceController.GetTrackSegment(CarInfo?.name);
        int segmentB = -1;

        if (segmentA < 0)
        {
            segmentA = 0;
            segmentB = raceController.GetNumSegments();
        }
        else
        {
            segmentB = raceController.NextSegment(segmentA);
        }

        float begin = Math.Max(0, raceController.TrackDistanceAtSegment(segmentA));
        float end   = Math.Max(0, raceController.TrackDistanceAtSegment(segmentB));

        var   closest         = track.GetSampleAtDistance(begin);
        float closestDistance = float.MaxValue;

        for (float i = begin; i < end; i += 0.05f)
        {
            var curveSample = track.GetSampleAtDistance(i);
            var d           = (gameObject.transform.position - 0.048f * Vector3.up - curveSample.location);
            d.y *= 10;
            var distance = d.magnitude;
            if (distance < closestDistance)
            {
                closestDistance = distance;
                closest         = curveSample;
            }
        }
        return(closest);
    }
 void Start()
 {
     track = GetComponent <SplineMesh.Spline>();
     LoadTrack(RaceParameters.readRaceParameters().track);
     UpdateFinishLine();
     UpdateTriggers();
     track.NodeListChanged += NodeListChanged;
 }
    //  TerrainTile tile;

    public void RunIt()
    {
        spline = GetComponent <SplineMesh.Spline>();

        //  tile = spline.gameObject.transform.parent.parent.GetComponent<TerrainTile>();

        if (mapMagicTransform == null)
        {
            mapMagicTransform = Component.FindObjectOfType <MapMagicObject>().transform;
        }


        TryToFloor();
    }
    void SaveTrackJson()
    {
        track = GetComponent <SplineMesh.Spline>();
        if (File.Exists(trackFileName))
        {
            Debug.Log(trackFileName + " already exists.");
            return;
        }
        var sr = File.CreateText(trackFileName);

        sr.WriteLine(ToJson(track.nodes.ToArray(), true));
        sr.Close();
        Debug.Log("Saved track to " + trackFileName);
    }
Example #5
0
    void Start()
    {
        if (!spline)
        {
            spline = GetComponent <SplineMesh.Spline>();
        }
        if (!line)
        {
            line = GetComponent <LineRenderer>();
        }
        Debug.Assert(spline);
        Debug.Assert(line);

        spline.CurveChanged.AddListener(UpdateLineFromSpline);

        UpdateLineFromSpline();
    }
    public void visualizeUndeformed(string frameName)
    {
        Frame targetFrame = myConstructorController.findFrame(frameName);

        targetFrame.setReleaseNeither();
        SplineMesh.Spline targetSpline = targetFrame.GetGameObject().GetComponentInChildren <SplineMesh.Spline>();
        int numNodesPrevious           = targetSpline.nodes.Count;

        while (targetSpline.nodes.Count > 2)
        {
            targetSpline.RemoveNode(targetSpline.nodes[targetSpline.nodes.Count - 1]);
        }

        targetSpline.nodes[0].Position  = new Vector3(0, 0, 0);
        targetSpline.nodes[0].Direction = new Vector3(0, (float)0.001, 0);
        targetSpline.nodes[1].Position  = new Vector3(0, 1, 0);
        targetSpline.nodes[1].Position  = new Vector3(0, (float)1.001, 0);
    }
    public static float GetDistanceFromSample(SplineMesh.Spline spline, SplineMesh.CurveSample sample)
    {
        float cumulatedDistance = 0;

        foreach (SplineMesh.CubicBezierCurve curve in spline.curves)
        {
            if (sample.curve != curve)
            {
                cumulatedDistance += curve.Length;
            }
            else
            {
                cumulatedDistance += sample.distanceInCurve;
                return(cumulatedDistance);
            }
        }
        return(cumulatedDistance);
    }
    public void visualizeDeformation(string frameName, jointDispl specialPointsJointDispl)
    {
        Frame targetFrame = myConstructorController.findFrame(frameName);

        targetFrame.setReleaseNeither();
        SplineMesh.Spline targetSpline = targetFrame.GetGameObject().GetComponentInChildren <SplineMesh.Spline>();
        int     numNodesPrevious       = targetSpline.nodes.Count;
        double  frameScale             = targetFrame.getTransform().lossyScale.y;
        double  frameLength            = targetFrame.getLength();
        double  baseX = 0;
        double  baseY = 0;
        double  baseZ = 0;
        Vector3 basePosition;

        while (targetSpline.nodes.Count < specialPointsJointDispl.numberResults)
        {
            targetSpline.AddNode(new SplineMesh.SplineNode(Vector3.zero, Vector3.zero));
        }

        int maxIndex = specialPointsJointDispl.numberResults - 1;

        for (int i = 0; i <= maxIndex; i++)
        {
            double u1 = specialPointsJointDispl.u1[i]; // WAIT global unity x
            double u2 = specialPointsJointDispl.u2[i]; // global unity z
            double u3 = specialPointsJointDispl.u3[i]; // global unity y
            double r1 = specialPointsJointDispl.r1[i];
            double r2 = specialPointsJointDispl.r2[i];
            double r3 = specialPointsJointDispl.r3[i];

            baseY = (double)i / (double)maxIndex;

            // Explanation for the following if-else-if block:
            // If a special point is set exactly at the end of a frame in SAP,
            // then that points will have a local coordinate system that may not align with every frame that ends on the point.
            // As a stopgap fix, SAPTranslator currently places the "end" points slightly into the length of the frame (by 0.001 of the frame's length).
            // This if-else block reflects that positioning in the VR visualization for accuracy.
            // Currently, the VR environment does not show those last 0.1% of the frame on either end, when showing deformation,
            // but that shouldn't be a problem just for a general visualization of the result.
            // It's a problem that needs to be solved if one wishes to be able to click on endpoints specifically and see their exact displacements in VR, though.
            if (baseY == 0)
            {
                baseY += 0.001;
            }
            else if (baseY == 1)
            {
                baseY -= 0.001;
            }


            Vector3 localBasePosition = new Vector3((float)baseX, (float)baseY, (float)baseZ);

            Vector3 globalBasePosition = targetFrame.getTransform().TransformPoint(localBasePosition);

            //float deltaX = (float)(u2 * visualizationScale);
            //float deltaY = (float)((u1 / frameScale) * visualizationScale);
            //float deltaZ = (float)(-1 * u3 * visualizationScale);
            //Vector3 deltaPosition = new Vector3(deltaX, deltaY, deltaZ);

            Vector3 globalDelta = new Vector3((float)(u1 * visualizationScale), (float)(u3 * visualizationScale), (float)(u2 * visualizationScale));

            //Vector3 globalFinal = globalBasePosition + globalDelta;

            //Vector3 localFinal = targetFrame.getTransform().InverseTransformPoint(globalFinal);
            Vector3 localDelta = targetFrame.getTransform().InverseTransformVector(globalDelta);
            localDelta.Set(localDelta.x, (float)(localDelta.y / 5.0), localDelta.z);
            //Vector3 iDontKnowWhyThisIsNecessary = new Vector3(localDelta.z, localDelta.x, localDelta.y);
            //localFinal.y =  (float)(localFinal.y / frameScale);



            Vector3 updatedNodePosition  = localBasePosition + localDelta;
            Vector3 updatedNodeDirection = new Vector3(updatedNodePosition.x, updatedNodePosition.y + (float)0.001, updatedNodePosition.z);
            Vector3 updatedNodeUp        = new Vector3(0, 0, -1);
            //lookAt = Quaternion.Euler((float)(r3 / frameScale), (float)(r1), (float)(r2 / frameScale)) * lookAt;

            //SplineMesh.SplineNode newSplineNode = new SplineMesh.SplineNode(nodePosition, lookAt);
            targetSpline.nodes[i].Position  = updatedNodePosition;
            targetSpline.nodes[i].Direction = updatedNodeDirection;
            targetSpline.nodes[i].Up        = updatedNodeUp;
        }
    }
Example #9
0
    protected void FixedUpdate()
    {
        distance        += Vector3.Distance(this.transform.position, previousPosition) / 100f;
        previousPosition = this.transform.position;
        splines          = GameObject.Find("HalfpipeManager").GetComponent <SpineSpawnManager>().spawnedSplines;
        splineScript     = splines[current].GetComponentInChildren <SplineMesh.Spline>();
        //move along the spline
        if (count >= splineScript.nodes.Count - 1)
        {
            if (current < splines.Count - 1)
            {
                count = 0;
                current++;
            }
        }
        //Debug.Log("Current Pos:"+(splines[current].GetComponentInChildren<SplineMesh.Spline>().GetSample(count)).location);
        Vector3 splineLocationLocal = (splineScript.GetSample(count)).location;         //location tracked 2 parents up

        splineLocation = splines[current].transform.GetChild(0).TransformPoint(splineLocationLocal);

        //Dynamic camera
        Vector3 splineLocationCam = new Vector3();

        if (count + .2f >= splineScript.nodes.Count - 1)
        {
            splineLocationCam = splines[current + 1].transform.GetChild(0).TransformPoint(splineScript.GetSample(count + .2f - splineScript.nodes.Count + 1).location);
        }
        else
        {
            splineLocationCam = splines[current].transform.GetChild(0).TransformPoint(splineScript.GetSample(count + .2f).location);
        }


        pCamera.transform.rotation = Quaternion.LookRotation(splineLocationCam - pCamera.transform.position + new Vector3(0, 5, 0), posObj.transform.up);

        /*
         * for(int i=0;i< splines[current].GetComponentInChildren<SplineMesh.Spline>().nodes.Count-1;i++)
         * {
         * splines[current].transform.FindChild("Extruder").transform.TransformPoint(splines[current].GetComponentInChildren<SplineMesh.Spline>().nodes[i].Position);
         * }*/
        splineRotation            = (splineScript.GetSample(count)).Rotation; //rotation tracked 1 parent up, camera being in same level but above
        posObj.transform.position = splineLocation;
        posObj.transform.rotation = splineRotation;

        //Professor Baker's code
#if UNITY_EDITOR
        //MoveChar(Input.GetAxis(k_HORIZONTAL),Input.GetAxis(k_VERTICAL));
        rotObj.transform.rotation = Quaternion.Slerp(transform.rotation,
                                                     splineRotation * Quaternion.Euler(0, 0, Input.GetAxis(k_HORIZONTAL) * 90), lowPassFilterFactor);
        //jump code
        if (Input.GetKeyDown(KeyCode.W) || Input.GetMouseButtonDown(0))
        {
#else
        rotObj.transform.rotation = Quaternion.Slerp(transform.rotation,
                                                     splineRotation * Quaternion.Euler(0, 0, Input.acceleration.x * 90), lowPassFilterFactor);
        //posObj.transform.position = new Vector3(posObj.transform.position.x,(posObj.transform.position.y+Mathf.Abs(Input.acceleration.y*2)),posObj.transform.position.z);
        //this.transform.localPosition= new Vector3(0,(Mathf.Abs(Input.acceleration.y*2)-6.5f),0);

        //jump code (replace acceleration.y with tap if need be)
        //if (Input.acceleration.y > 0)
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
#endif

            if (!isJumping & !isFalling)
            {
                maxY      = 4.0f;
                isJumping = true;
            }
        }
        if (isJumping & transform.localPosition.y + 6.5f >= maxY)
        {
            isFalling = true;
            isJumping = false;
        }
        if (isJumping)
        {
            transform.localPosition += new Vector3(0, (maxY + 6.5f) / 20, 0);
        }
        if (isFalling & transform.localPosition.y <= -6.5)
        {
            isFalling = false;
            transform.localPosition = localDefaultPos;
        }
        if (isFalling)
        {
            transform.localPosition += new Vector3(0, -((maxY + 6.5f) / 20), 0);
        }
        count += .01f;
    }
 private void OnEnable()
 {
     track = FindObjectOfType <SplineMesh.Spline>();
 }
 void LoadTrackJson()
 {
     track = GetComponent <SplineMesh.Spline>();
     LoadTrack(trackFileName);
 }
Example #12
0
            private void RenderOutSplinesSoFar(GameObject splineHolder, int i, List <SplineNode> myarray)
            {
                GameObject child = new GameObject();

                child.transform.parent = splineHolder.transform;

                SplineMesh.Spline splineScriptObj = child.GetComponent <SplineMesh.Spline>();
                //  if (splineScriptObj == null) splineScriptObj = splineHolder.transform.parent.GetComponentInChildren<SplineMesh.Spline>();
                if (splineScriptObj == null)
                {
                    splineScriptObj = child.gameObject.AddComponent <SplineMesh.Spline>();
                }

                //finding holder
                SplineMesh.ExampleSower splineObj = child.GetComponent <SplineMesh.ExampleSower>();
                //   if (splineObj == null) splineObj = terrain.transform.parent.GetComponentInChildren<SplineMesh.ExampleSower>();
                if (splineObj == null)
                {
                    splineObj = child.gameObject.AddComponent <SplineMesh.ExampleSower>();
                }


                Transform  reft;
                GameObject go;

                //or creating it
                if (splineObj == null)
                {
                    go = new GameObject();
                }
                else
                {
                    go   = child.gameObject;
                    reft = child.gameObject.transform;
                }


                go.transform.parent = splineHolder.transform;


                // TODO make this an actual hash and shove it in a table
                // string hash = string.Format("{0}_{1}_{4}_{5}|{2}_{3}", startvec.x, startvec.y, startvec.z, endvec.x, endvec.y, endvec.z);
                //  string fullhash = string.Format("{0}_{1}|{4}_{5}|{2}_{3}", startvec.x, startvec.y, startvec.z, endvec.x, endvec.y, endvec.z);
                string hash = string.Format("__SPLINE__{0}__{2}|{3}__{5}", myarray[0].Position.x, myarray[0].Position.y, myarray[0].Position.z, myarray[myarray.Count - 1].Position.x, myarray[myarray.Count - 1].Position.y, myarray[myarray.Count - 1].Position.z);

                var newSpline = go;


                // Extract the stuff we welded in the settings and reweld it to the output

                newSpline.name = hash + splines[i].chosenType.prefab.name;

                splineScriptObj.nodes = myarray;

                newSpline.transform.localPosition = new Vector3();


                splineObj.prefab = splines[i].chosenType.prefab;

                splineObj.isRandomYaw = splines[i].isRandomYaw;

                splineObj.spacing = splines[i].spacing;

                splineObj.spacingRange = splines[i].spacingRange;
                splineObj.offset       = splines[i].offset;
                splineObj.offsetRange  = splines[i].offsetRange;

                splineObj.scale      = splines[i].scale;
                splineObj.scaleRange = splines[i].scaleRange;

                splineObj.spline.nodes = myarray.ToList();
                splineScriptObj.nodes  = myarray.ToList();

                splineScriptObj.RefreshCurves();

                var scrp = newSpline.AddComponent <AlignNodesToTerrainOnEnable>();

                splineObj.Sow();

                scrp.RunIt();
            }
Example #13
0
 private void OnEnable()
 {
     spline   = GetComponent <Spline>();
     toUpdate = true;
 }
 private void Start()
 {
     spline = GetComponent <Spline>();
 }