Пример #1
0
    //Applies forces to neighbour and returns reaction force to this
    Vector3 ApplyTorqueToPair(int alphaIndex, int betaIndex)
    {
        CellNeighbour alphaNeighbour = GetNeighbour(alphaIndex);
        CellNeighbour betaNeighbour  = GetNeighbour(betaIndex);

        Vector3 alphaVector = alphaNeighbour.coreToThis; //Allways after = lower angle
        Vector3 betaVector  = betaNeighbour.coreToThis;  //Allways ahead = lower angle
        //float angle = Vector3.Angle(alphaVector, betaVector);
        float angle = LongAngle(alphaVector, betaVector);

        float goalAngle = CardinalDirectionHelper.GetAngleBetween(alphaIndex, betaIndex);
        //Debug.Log(this.id + " Spring: " + alphaNeighbour.cell.id + "-" + betaNeighbour.cell.id + " = GoalA: " + goalAngle + " A: " + angle);
        float diff = (goalAngle - angle);


        float   k1         = 0.0005f;
        float   k2         = 0.0003f;
        float   magnitude  = k1 * diff + Mathf.Sign(diff) * k2 * diff * diff;
        Vector3 alphaForce = Vector3.Cross(alphaVector, new Vector3(0f, 0f, 1f)) * magnitude;
        Vector3 betaForce  = Vector3.Cross(betaVector, new Vector3(0f, 0f, -1f)) * magnitude;

        alphaNeighbour.cell.GetComponent <Rigidbody2D>().AddForce(alphaForce, ForceMode2D.Impulse);
        betaNeighbour.cell.GetComponent <Rigidbody2D>().AddForce(betaForce, ForceMode2D.Impulse);

        return(-(alphaForce + betaForce));
    }
Пример #2
0
        public override ParcelWrapper GetNext(CellNeighbour direction, ushort gridId)
        {
            int pos = FindByGridId(gridId);

            int row    = pos / Columns;
            int column = pos % Columns;

            if (pos >= 0)
            {
                if (direction == CellNeighbour.UP && row < Rows)
                {
                    return(Cells[pos + Rows]);
                }

                if (direction == CellNeighbour.DOWN && row > 0)
                {
                    return(Cells[pos - Columns]);
                }

                if (direction == CellNeighbour.RIGHT && column < Columns)
                {
                    return(Cells[pos + 1]);
                }

                if (direction == CellNeighbour.LEFT && column > 0)
                {
                    return(Cells[pos - 1]);
                }
            }

            return(null);
        }
Пример #3
0
 /// <summary>
 /// This method finds the adjacent neighbour following the distribution
 /// </summary>
 /// <param name="direction">The direction to look up</param>
 /// <param name="gridId">The id of the cell</param>
 /// <returns></returns>
 public abstract ParcelWrapper GetNext(CellNeighbour direction, ushort gridId);