2013年3月15日 星期五

<分享>How to use timer component into C# script with multiply points

目標 : Using timer component into C# script.

檔案 : https://dl.dropbox.com/u/29860061/bouncing%20points%20with%20array.gh


流程步驟 :

1.在全域區塊(global variable)中設定需要被內部區塊改變的變數。

  List<Point3d> ListPts1 = new List<Point3d>();
  List<Point3d> ListPts2 = new List<Point3d>();
  List<Point3d> ListPts3 = new List<Point3d>();

  double[] xx = {0,1,2};
  double[] yy = {0,1,2};
  double[] speedx = {1,4,8};
  double[] speedy = {2,2,2};
注意: 在全域區塊中 GH C# component 不能使用迴圈去assign 陣列中的數值....很奇怪

2.創建初始點,我們即將改變點的x,y座標。

Point3d myPoint = new Point3d(xx[i], yy[i], 0);

3.隨著timer 的計數器增加,點座標的x,y值會依據使用者設定的speedx/speedy 增加。

xx[i] += speedx[i];
yy[i] += speedy[i];



4.設定彈跳範圍,如果彈跳超出範圍則程式需要改變speedx/speedy 的方向。
if(x[i]x > boundX )
if(xx[i] < 0 )
if(yy[i] > boundy )
if(yy[i] < 0 )



5.清除紀錄,如果使用者重新執行bounce point,請程式清除上一次的點座標,並使x,y座標返回預設值。

      ListPts1.Clear();
      ListPts2.Clear();
      ListPts3.Clear();
      for(int i = 0;i < 3;i++){
        xx[i] = yy[i] = 0;
      }


Main Code:


if(start){

      for(int i = 0;i < 3;i++){

        Point3d myPoint = new Point3d(xx[i], yy[i], 0);

        if(i == 0){
          ListPts1.Add(myPoint);
        }

        if(i == 1){
          ListPts2.Add(myPoint);
        }

        if(i == 2){
          ListPts3.Add(myPoint);
        }

        xx[i] += speedx[i];
        yy[i] += speedy[i];

        if(xx[i] > boundX ){
          xx[i] = boundX;
          speedx[i] *= -1;
        }

        if(xx[i] < 0 ){
          xx[i] = 0;
          speedx[i] *= -1;
        }

        if(yy[i] > boundy ){
          yy[i] = boundy;
          speedy[i] *= -1;
        }

        if(yy[i] < 0 ){
          yy[i] = 0;
          speedy[i] *= -1;
        }
      }

    }else{
      ListPts1.Clear();
      ListPts2.Clear();
      ListPts3.Clear();
      for(int i = 0;i < 3;i++){
        xx[i] = yy[i] = 0;
      }
    }

    A = ListPts1;
    B = ListPts2;
    C = ListPts3;


影片:



結果圖片:
GH C# Definition




timer count A

timer count B

timer count C


沒有留言:

張貼留言