Physics

2005/07/28 15:28 by civet | |

摘自Robert PennerProgramming Marcomedia Flash MX

(提示:以下代码中的Vector是一个AS1.0的自定义类,这个Vector类的代码请到作者的网站下载)

一、运动学

  • 位置  

    // 用二维向量表示的位置
    pos = new Vector(2, 5);

  • 位移  

    // 计算位移向量
    pointA = new Vector(2, 3);
    pointB = new Vector(4, 2);
    disp  = pointB.minusNew(pointA);
    trace(disp); // 输出:[2,-1]

  • 距离  

    // 继续前面的例子
    trace(disp.getLength()); // 输出:2.23606797749979

  • 速度  

    1. 恒定速度的运动  

      // 恒定速度的运动
      pos = new Vector(0, 0);
      vel = new Vector(2,  3);
      this.onEnterFrame = function() {
          pos.plus(vel);
          this._x =  pos.x;
          this._y = pos.y;
      };

    2. 随机速度的运动  

      // 随机速度的运动
      pos = new Vector(200, 200);
      vel = new Vector(0,  0);
      this.onEnterFrame = function() {
          vel.reset(random(9)-4,  random(9)-4);
          pos.plus(vel);
          this._x = pos.x;
          this._y =  pos.y;
      };

    3. 以鼠标控制速度的运动  

      // 以鼠标控制速度
      pos = new Vector(200, 200);
      vel = new Vector(0,  0);
      this.onEnterFrame = function() {
          vel.reset(this._xmouse/10,  this._ymouse/10);
          pos.plus(vel);
          this._x = pos.x;
          this._y =  pos.y;
      };

  • 速率  

    vel = new Vector(3,4);
    speed = vel.getLength();
    trace(speed); //  输出:5

  • 加速度  

    1. 恒定加速度的运动  

      // 恒定加速度的运动
      pos = new Vector(0, 0);
      vel = new Vector(0, 0);
      accel =  new Vector(1, 0);
      this.onEnterFrame = function()  {
          vel.plus(accel);
          pos.plus(vel);
          this._x = pos.x;
          this._y =  pos.y;
      };

    2. 随机加速度的运动  

      // 随机加速度的运动
      pos = new Vector(200, 200);
      vel = new Vector(0,  0);
      accel = new Vector(0, 0);
      this.onEnterFrame = function()  {
          accel.reset(random(5)-2,  random(5)-2);
          vel.plus(accel);
          pos.plus(vel);
          this._x =  pos.x;
          this._y = pos.y;
      };

    3. 以鼠标控制加速度的运动  

      // 以鼠标控制加速度
      pos = new Vector(200, 200);
      vel = new Vector(0,  0);
      accel = new Vector(0, 0);
      this.onEnterFrame = function()  {
          accel.reset(this._xmouse/100,  this._ymouse/100);
          vel.plus(accel);
          pos.plus(vel);
          this._x =  pos.x;
          this._y = pos.y;
      };

内文分页: [1] [2]
默认分类 | 评论(0) | 引用(0) | 阅读(105)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]