示例#1
0
    IEnumerator finished(VerletV3ControlThread myJob)
    {
        yield return(StartCoroutine(myJob.WaitFor()));

        //Debug.Log("finished "+myJob.ThreadName+"	it was: "+myJob.p);
        for (int i = 0; i < myJob.names.Length; i++)
        {
            VerletObjectV3 thisObject = ((VerletObjectV3)massObject[i].GetComponent("VerletObjectV3"));
            thisObject.newPos(myJob.positions[i]);
            thisObject.previousPosition = myJob.previousPositions[i];
        }
        masterTimeCounter  = myJob.masterTimeCounter;
        lastFrameCompleted = myJob.frameCompleted;
        multiThreadFlag   += 1;
        runThreads();
    }
    private void runThreads()
    {
        if (multiThreadFlag == multithreadedJobs.Length && timeStep != 0)
        {
            //If the threads have completed
            if (timeStep != 0 && timeStep == previousTimeStep)
            {
                Debug.Log(lastFrameCompleted + "	"+ stepTimeStep + "	" + timeStep + "	"+
                          stepTimeStep / timeStep + "	"+
                          Mathf.CeilToInt(stepTimeStep / timeStep));
                //Debug.Log(lastFrameCompleted + "	" + multiple + "	" + stepTimeStep + "	" + (multiple - Mathf.Log(Mathf.Pow(multiple, 1/6))));
                if (lastFrameCompleted && multiple >= 1.05)
                {
                    stepTimeStep  = stepTimeStep / multiple;
                    stepsPerFrame = Mathd.CeilToInt(timeStep / stepTimeStep);
                }

                if (!lastFrameCompleted && multiple >= 1.05)
                {
                    stepTimeStep  = stepTimeStep * multiple;
                    stepsPerFrame = Mathd.CeilToInt(timeStep / stepTimeStep);
                    multiple      = multiple - Mathf.Log(Mathf.Pow(multiple, 1 / 6));
                }

                if (lastFrameCompleted && multiple < 1.05 || Mathf.Abs(stepTimeStep) < 0.05)
                {
                    Debug.Log("Ping");
                    multiple = 1;
                }

                if (!lastFrameCompleted && multiple < 1.05)
                {
                    multiple = 5;
                }
            }
            else if (previousTimeStep != timeStep)
            {
                multiple     = 10;
                stepTimeStep = 10;
            }

            multiThreadFlag = 0;
            massObject      =
                GameObject
                .FindGameObjectsWithTag("massObject"); //Gathers all of the massObjects for calculation
            var masses = new float[massObject.Length]; //Gathers all of the masses in order
            var
                positions = new Vector3d[massObject
                                         .Length]; //Gathers all of the vectors of the objects in order
            var names             = new string[massObject.Length];
            var previousPositions = new Vector3d[massObject.Length];
            for (var i = 0; i < massObject.Length; i++)
            {
                //For every object, get data for mass and position
                var obj = (VerletObjectV3)massObject[i].GetComponent("VerletObjectV3");
                masses[i]            = obj.mass;
                positions[i]         = obj.position;
                names[i]             = obj.name;
                previousPositions[i] = obj.previousPosition; //Assign previous position
            }

            simulationControlThread                   = new VerletV3ControlThread();
            simulationControlThread.masses            = masses;
            simulationControlThread.massObjectNames   = names;
            simulationControlThread.masterTimeCounter = masterTimeCounter;
            simulationControlThread.positions         = positions;
            simulationControlThread.timestep          = stepTimeStep;
            simulationControlThread.steps             = stepsPerFrame;
            simulationControlThread.names             = names;
            simulationControlThread.previousPositions = previousPositions;
            simulationControlThread.previousTimeStep  = stepTimeStepP;

            simulationControlThread.Start();                     //Start thread
            StartCoroutine("finished", simulationControlThread); //Start end coroutine

            previousTimeStep = timeStep;
            stepTimeStepP    = stepTimeStep;
        }
    }