Processing - 练习(3)"飞碟追踪" - (渐进、延迟效果)

在void draw()函数中,使用background()函数,相当于屏幕刷新的效果;


实例1:画一个简化的"飞碟",让它渐渐地“飞”向鼠标。(距离越近,渐进越慢)

float x,y;
//x,y是圆心坐标
float easing = 0.01;


float diameterWidth = 32.0,
    diameterHeight = 4.0;


void draw_a_UFO(float x, float y)
{
   ellipse(x,y,diameterWidth / 3,diameterHeight * 2.5);
   ellipse(x,y,diameterWidth,diameterHeight); 
}    


void setup()
{
    size(720,404);
    smooth();
    x = 0.5 * width;
    y = 0.5 * height;
}


void draw()
{
    background(20);
    float targetX = mouseX,
          targetY = mouseY;
    
    x += (targetX - x) * easing;
    y += (targetY - y) * easing;
    noStroke();
    draw_a_UFO(x,y);
    println(targetX + " : " + x);
}
生气 说明:简易“飞碟”,由两个椭圆组成,够“简易”吧!

这样就一个傻乎乎的飞碟,看起来不够酷,制造它的魅影效果吧!

代码改进:(魅影效果)

float x,
      x0=0.0,
      x1=0.0,
      x2=0.0,
      x3=0.0,
      x4=0.0,
      x5=0.0,
      x6=0.0,
      y,
      y0=0.0,
      y1=0.0,
      y2=0.0,
      y3=0.0,
      y4=0.0,
      y5=0.0,
      y6=0.0;

void positonsSet()
{
    y6=y5;
    y5=y4;
    y4=y3;
    y3=y2;
    y2=y1;
    y1=y;    
    x6=x5;
    x5=x4;
    x4=x3;
    x3=x2;
    x2=x1;
    x1=x;
}

//x,y是圆心坐标
float easing;

float diameterWidth = 32.0,
    diameterHeight = 4.0;

void draw_a_UFO(float x, float y, int c)
{
   fill(c);
   ellipse(x,y,diameterWidth / 2,diameterHeight * 2.5);
   ellipse(x,y,diameterWidth,diameterHeight); 
} 

int colorSet = 250;
int myColor;   
void colorChange()
{
    myColor -= 33;
    fill(myColor);
}

void setup()
{
    frameRate(10);
    size(720,404);
    smooth();
    x = 0.5 * width;
    y = 0.5 * height;
    easing = 0.2;
}

void draw()
{
    background(20);
    float targetX = mouseX,
          targetY = mouseY;
    
    x += (targetX - x) * easing;
    y += (targetY - y) * easing;
    noStroke();
    positonsSet();
    myColor = colorSet;
    draw_a_UFO(x0,y0,myColor);
    colorChange();
    draw_a_UFO(x1,y1,myColor);
    colorChange();
    draw_a_UFO(x2,y2,myColor);
    colorChange();
    draw_a_UFO(x3,y3,myColor);
    colorChange();
    draw_a_UFO(x4,y4,myColor);
    colorChange();
    draw_a_UFO(x5,y5,myColor);
    colorChange();
    draw_a_UFO(x6,y6,myColor);
    colorChange();
    println(frameCount);
    //println(targetX + " : " + x);
}
还是没学到数组,搞得有些重复,以后再改进技术吧。

动画截图:











版权声明:本文为Losacii原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。