向量加法:
//实现弹球程序
PVector location;
PVector veclocity;
void setup(){
size(480, 100);
smooth();
location = new PVector(random(0, width), random(0, height));
veclocity = new PVector(0.5, 1);
frameRate(180);
}
void draw(){
background(255);
location.add(veclocity);
if((location.x > width) || (location.x < 0)){
veclocity.x = veclocity.x*-1;
}
if((location.y > height) || (location.y < 0)){
veclocity.y = veclocity.y*-1;
}
noStroke();
fill(255, 255, 0, 255);
ellipse(location.x, location.y, 20, 20);
}
向量减法:
//鼠标坐标和窗口中心点的差值,利用窗口中心和差值绘制直线
void setup(){
size(400, 100);
}
void draw(){
background(255);
PVector mouse = new PVector(mouseX, mouseY);
PVector center = new PVector(width/2, height/2);
mouse.sub(center);
translate(width/2, height/2);
line(0, 0, mouse.x, mouse.y);
}向量乘法&除发://对向量的差值缩小为 自身的 1/2,再除以 0.5 倍,等于大小没变
mouse.sub(center);
mouse.mult(0.5);
mouse.div(0.5);
向量长度://计算向量的长度
float m = mouse.mag();
单位化向量://单位化向量后,将向量的长度设置为固定值
void setup(){
size(600, 200);
}
void draw(){
background(255);
PVector mouse = new PVector(mouseX, mouseY);
PVector center = new PVector(width/2, height/2);
mouse.sub(center);
mouse.normalize();
mouse.mult(100);
translate(width/2, height/2);
line(0, 0, mouse.x, mouse.y);
}
限制加速度绘制移动的小球:class Mover{
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
Mover(){
location = new PVector(random(width), random(height));
velocity = new PVector(random(-2, 2), random(-2, 2));
acceleration = new PVector(-0.001, 0.001);
topspeed = 10;
}
void update(){
acceleration = PVector.random2D();
acceleration.mult(0.5);
velocity.add(acceleration);
//velocity.limit(topspeed);
location.add(velocity);
}
void display(){
stroke(0);
fill(175);
ellipse(location.x, location.y, 16, 16);
}
void checkEdges(){
if(location.x > width)
location.x = 0;
else if(location.x < 0)
location.x = width;
if(location.y > height)
location.y = 0;
else if(location.y < 0)
location.y = height;
}
}
Mover mover;
void setup(){
size(200, 200);
smooth();
mover = new Mover();
}
void draw(){
background(255);
mover.update();
mover.checkEdges();
mover.display();
}朝向鼠标移动的小球:
//修改update(),实现小球朝向鼠标位置移动
void update(){
PVector mouse = new PVector(mouseX, mouseY);
PVector dir = PVector.sub(mouse, location);
dir.normalize();
dir.mult(0.5);
acceleration = dir;
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
///
//一组同时朝着鼠标加速的运动物体
Mover[] movers = new Mover[20];
void setup(){
size(200, 200);
smooth();
for(int i = 0; i < movers.length; i++){
movers[i] = new Mover();
}
}
void draw(){
background(255);
for(int i = 0; i < movers.length; i++){
movers[i].update();
movers[i].checkEdges();
movers[i].display();
}
}
PVector类中的所有函数及对应的向量运算:add() //向量相加
sub() //向量相减
mult() //乘以标量以延伸向量
div() //除以标量以缩短向量
mag() //计算向量的长度
setMag() //设定向量的长度
normalize() //单位化向量,使其长度为1
limit() //限制向量的长度
heading2D() //计算向量的方法,用角度表示
rotate() //旋转一个二维向量
lerp() //线性插值到一个向量
dist() //计算两个向量的欧几理得距离
angleBetween() //计算两个向量的夹角
dot() //计算两个向量的点乘
cross() //计算两个向量的叉乘(只涉及三维空间)
random2D() //返回一个随机的二维向量, 静态函数
random3D() //返回一个随机的三维向量
版权声明:本文为Ant_look原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。