互动媒体技术——processing学习:随机行为及牛顿运动学

Chap0:随机游走与噪声

根据这一章的知识学习到了随机游走类class Walker,并且将Perlin噪声和游走结合做成模型,用Perlin噪声同时生成x坐标和y坐标,再加上一点颜色渐变效果,代码如下:

class Walker{
   
  float x,y;
  float tx,ty;
  float r,b,g;
  float tr,tb,tg;
  
  Walker()
  {
   
    tx=0;
    ty=10000;
    tr=0;
    tb=1000;
    tg=2000;
  }
  void step()
  {
   
    x=map(noise(tx),0,1,0,width);
    y=map(noise(ty),0,1,0,height);
    tx+=0.01;
    ty+=0.01;
  }
  void c()  
  {
   
    r=map(noise(tr),0,1,100,255);
    b=map(noise(tb),0,1,100,255);
    g=map(noise(tg),0,1,100,255);
    tr+=2;
    tb+=2;
    tg+=2;  
  }
}
Walker w;
void setup()
{
   
  size(480,480);
  background(135,206,250);
   w=new Walker();
  
}
void draw()
{
   
   w.step();
   w.c();
   fill(w.r,w.b,w.g);
   ellipse(w.x,w.y,30,30);
   noStroke();   
}

运行结果:
在这里插入图片描述
结果做出来有点花里胡哨,如果颜色连贯一点看起来会好看一点,于是改一下:

void c()  
  {
   
    r=map(noise(tr),0,1,100,255);
    b=map(noise(tb),0,1,100,255);
    g=map(noise(tg),0,1,100,255);
    tr+=0.01;
    tb+=0.01;
    tg+=0.01;  
  }

就连贯了许多:
在这里插入图片描述

Chap1:向量

欧几里得向量(Euclidean vector,以希腊数学家欧几里 得的名字命名,也称作几何向量)它的定义是:一个既有大小又有方向的几何对象。
向量通常被绘制为一个带箭头的线段,线段的长度代表向量的大小,箭头所指的方向就是向量的方向。
先试了一个简单的,给上面随机运动的小球加上了随机向量,得到如下效果:
在这里插入图片描述
代码如下:

class Mover
{
   
  PVector location1,location2;
  PVector v1,v2;
  PVector acceleration1,acceleration2;
  Mover()
  {
   
  location1=new PVector(random(width),random(height));
  v1=new PVector(random(-2,2),random(-2,2));
  acceleration1=PVector.random2D();
  location2=new PVector(random(width),random(height));
  v2=new PVector(random(-2,2),random(-2,2));
  acceleration2=PVector.random2D();
 
  }
  void update()
  {
   
   PVector dir1=PVector.sub(location2,location1);
   PVector dir2=PVector.sub(location1,location2);
    dir1.normalize();
    dir1.mult(0.5);
    acceleration1=dir1;
    v1.add(acceleration1);
    v1.limit(20);
    location1.add(v1);
    
    
    dir2.normalize();
    dir2.mult(0.5);
    acceleration2=dir2;
    v2.add(acceleration2);
    v2.limit(20);
    location2.add(v2);
  }
  void display()
  {
   
    stroke(0);
    fill(255);
    ellipse(location1.x,location1.y,1,1);
    ellipse(location2.x,location2.y,1,1);
  }
  void checkEdges()
  {
   
    if(location1.x>width)
    {
   
      location1.x=0;
    }
    else if(location1.x<0)
    {
   
      location1.x=width;
    }
     if(location1.y>height)
    {
   
      location1.y=0;
    }
    else if(location1.y<0)
    {
   
      location1.y=height;
    }
  }
}
Mover mover;
void setup()
{
   
   mover=new Mover();
   background(255);
   size(500,500);
   smooth();
 
}
void draw()
{
   
  mover.update();
  mover.checkEdges();
  mover.display();
}

这一章里面我学习了向量的加减乘除以及加速度,在1.11的启发下,我想实现一个类似烟花炸开的效果,在open processing上面参考了一下之后实现了如下效果:
在这里插入图片描述
在这里插入图片描述

鼠标点击就可以实现炸开的效果,各个粒子的加速度由大到小,像是一盘颜料洒出来的感觉,大致的想法是将爆炸范围控制在一个圆内,然后设置鼠标点击,从中央炸开,具体代码如下:

final int PARTICLE_COUNT = 1000;
Particle[] particles = new Particle[PARTICLE_COUNT];
PVector direction = PVector.random2D();
PVector point;
PVector center;
void setup()
{
   
  size(800, 800);
  center = new PVector(width/
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值