flash.geom.Vector3D解读

flash的Vector3D是用来描述3维位移,方法属性命名和C#的Vector3D惊人的一致!要了解这个类必须先了解向量,在游戏中我们用向量来表示场景中的光照、加速度、速度、各种重要参考线等;点是三维空间中的某个坐标,是绝对的,它的值是参照原点的,而向量用于表示力和速度等具有方向和大小的量, 通常用具有长度和方向的线段来表示,虽然他们都具有三个分量,但对于向量,如果将向量放在坐标系中的任何位置(平移),都不会改变其性质,因为向量表示的是方向和大小,与位置距离无关,它的值是相对与基准点的。

三维空间的表示如下


向量的长度:向量的大小(或长度)称为向量的模


public function getlength():Number{
    if(lengthSquared == 0 ){
     return0;
    }
    return Math.sqrt(lengthSquared);
}

public function getlengthSquared():Number{
   return x * x + y * y + z * z;
}

三维空间中两点的距离

public static function distance(pt1:Vector3D, pt2:Vector3D):Number{
   return (pt1.subtract(pt2)).length;
}

向量加法

public function add(a:Vector3D):Vector3D{

    return newVector3D(this.x + a.x,this.y + a.y,this.z + a.z);

}

public functionincrementBy(a:Vector3D):void{
    this.x += a.x;
    this.y += a.y;
    this.z += a.z;

}

向量减法

public function subtract(a:Vector3D):Vector3D{
    return newVector3D(this.x - a.x,this.y - a.y,this.z - a.z);
}

public functiondecrementBy(a:Vector3D):void{
     this.x -= a.x;
     this.y -= a.y;
     this.z -= a.z;
}

向量点积(dot product)又称数量积或内积


public function dotProduct(a:Vector3D):Number{
     return x * a.x  + y * a.y + z * a.z;

}
根据公式A.B = |A||B|cos(a)得出两个向量之间的弧度的角度

public static function angleBetween(a:Vector3D, b:Vector3D):Number{
   var val:Number = a.dotProduct(b) / (a.length * b.length);
   return Math.acos(val);// * 180 / Math.PI
}
其中a是A和B在3D空间中的夹角。如果已知两个向量,使用数量积我们就可以通过计算求得两个向量的夹角

向量叉积 : U和V的向量积(cross product,矢量积或外积)产生一个向量,它垂直于U和V,公式: U × V  =  n  | U |   | V |  sin(a),其中n为垂直于U和V的单位向量,a是U和V的夹角

public function crossProduct(a:Vector3D):Vector3D{

   return new Vector3D(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x ,1);

}


数乘向量:实数λ与向量b的积是一个向量,记作:a=λb。规定:当λ为正时,同向;当λ为负时,反向;实数λ,叫做向量的系数。数乘向量的几何意义就是把向量沿着相同方向或反方向放大或缩小

public function scaleBy(s:Number):void{
   this.x *= s;
   this.y *= s;
   this.z *= s;
}

向量比较
public functionequals(toCompare:Vector3D, allFour:Boolean = false):Boolean{
   var isquals:Boolean = (toCompare.x == this.x && toCompare.y == this.y && toCompare.y == this.y)  && (allFour ? toCompare.w == this.w : true); 
  return isquals;
}

public functionnearEquals(toCompare:Vector3D, tolerance:Number, allFour:Boolean = false):Boolean{
   var isquals:Boolean = Math.abs(toCompare.x - this.x) < tolerance  && Math.abs(toCompare.y - this.y) < tolerance && Math.abs(toCompare.z - this.z) < tolerance;

  if(allFour){
     isquals = isquals && Math.abs(toCompare.w - this.w) < tolerance;
}
 return  isquals;
}

向量的规范向量的规范化也称(归一化)就是使向量的模变为1,即变为单位向量。可以通过将向量都除以该向量的模来实现向量的规范化。规范化后的向量相当于与向量同方向的单位向量,可以用它表示向量的方向。由于方向的概念在3D编程中非常重要,所以此概念也很重要,单位向量有很多重要的性质,在表示物体表面的法线向量时用的更是频繁 

public function normalize():Number{
   var len:Number = this.length
   if(len == 0){
       return 0; 
    }
    scaleBy(1/len)
    return len;
}

投影:一般用于透视

向量b在A方向的投影为a

public function project():void{
   scaleBy(1/ this.w)
}

 

图来自互联网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值