标量:只有大小没有方向
向量:既有大小,也有方向。
向量的模:向量的大小
单位向量:大小为1的向量。
向量单位化:把向量转为单位向量的过程
向量的运算:其实就是对某个点坐标的运算。将坐标轴原点设为(0 ,0).
Y轴左边为x的负值,右边为x的正值
X轴上边为y的正值,下边为y的负值。
向量的加法:
A(x1,y1) + B(x2,y2)=x1 + x2 , y1 + y2
向量的减法:
A(x1,y1) - B(x2,y2)= x1 - x2 , y1 - y2
向量的乘法
A(x1,y1) * 2 = x1 * 2 , y1 * 2
向量的点乘
得到两个向量之间的夹角度数
A . B = x1 * x2 + y1 * y2 = n = |a||b|cosθ = cosθ
脚本开发
每一个组件都是一个脚本,脚本是自定义组件
位移,旋转,缩放
console.log(this.node);
console.log(this.node.position);
console.log(this.node.rotation); //三维坐标系下的旋转。
console.log(this.node.angle); //一维坐标系下的旋转,2d游戏常用
console.log(this.node.scale);
//更改节点位置api
//第一种写法
this.node.setPosition(300, 0, 0);
//第二种写法
const p = new Vec3(300, 0, 0);
this.node.setPosition(p);
//第三种写法 使用获取位置的api和更改位置api结合使用
//不含参数的写法
//getPosition()如果不传参数,会返回一个新的三维向量,即 vec3.
const currentP = this.node.getPosition();
currentP.x += 300;
this.node.setPosition(currentP);
//第四种写法,使用获取位置的api和更改位置api结合使用
//getPosition()入一个参数,推荐使用,避免产生垃圾
const otherP = new Vec3();
this.node.getPosition(otherP);
otherP.x += 300;
this.node.setPosition(otherP);
//第四种写法在updat生命周期回调函数中的使用举例
curPos = new Vec3();
update(deltaTime: number) {
//实现节点向右移动的效果
//注意:以下写法会报错,不能直接这样赋值,node节点上不允许直接访问坐标位置
//this.node.position.x += 1
//依然是错误的赋值方法,但是能正常渲染运行
// const p = this.node.position;
// p.x += 1;
// this.node.position = p;
//正确的赋值方法一
// let p = this.node.position.x;
// this.node.setPosition((p += 1), 0, 0);
//正确的赋值方法二
this.node.getPosition(this.curPos);
this.curPos.x += 100 * deltaTime;
this.node.setPosition(this.curPos);
//angle旋转的赋值方法
// this.node.angle += 1;
}