0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Processing]ポリゴンで波を表現

Last updated at Posted at 2020-03-14

こんな

69行目の「stroke(255);」の時と「noStroke();」の2種類の映像
Waver.gif
線が入ると正方形が連なってるのがわかりますね
手前から奥に向かって帯が揺れています

プログラム

Wavers.java
PShape Ball;

class SeaSell{
  
  SeaSell prev; //位置をマネする対象
  
  float x;
  float y;
  float z;
  float siner = 0;
  
  SeaSell(float x , float Z){
    this.x = x;
    z = Z;
  }
  
  void Draw(){
    
    if(prev != null)y = prev.y;
    else y += sin(siner += 0.05f);
    
    vertex(x,y,z);
  }
  
}


SeaSell sells[][] = new SeaSell[100][100];

void setup(){
  size(1800,800,P3D);
  frameRate(30);
  
  Ball = createShape(SPHERE,100);
  Ball.setTexture(loadImage("Ball.png"));
  Ball.setStrokeWeight(0);
  
  //Sea sellの間隔
  int distribution = 15;
  //波打つ帯を横に並べてる
  for(int i=0;i<sells.length;i+=2){
    for(int j=0;j<sells[i].length;j++){
      sells[i][j] = new SeaSell(i * distribution -distribution,j * distribution); //位置登録
      sells[i+1][j] = new SeaSell(i * distribution +distribution,j * distribution); //位置登録
    }
  }
  
  for(int i=0;i<sells.length;i++){
    for(int j=0;j<sells[i].length;j++){
      if(j != sells[i].length-1){   //jが端じゃない時、同列の隣のセルをprevにいれる
        sells[i][j].prev = sells[i][j+1];
      }
      else if(i != sells.length-1){  //jが端だけどiが端じゃない時、隣の列のセルを入れる
        sells[i][j].prev = sells[i+1][j];
      }
    }
  }
}

void draw(){
  background(0);
  lights();
  perspective();
  
  pushMatrix();
    translate(5,0,-1500);
    rotateX(-PI/6f);
    fill(50,50,200);
    //stroke(255);
    noStroke();
    
    
    for(int i=0;i<sells.length;i+=2){
      beginShape(QUAD_STRIP); //帯状の波
        for(int j=0;j<sells[i].length;j++){
          sells[i][j].Draw();
          sells[i+1][j].Draw();
        }
      endShape();
    }
  
    pushMatrix();
      translate(sells[50][50].x,sells[50][50].y,sells[50][50].z);
      translate(0,-30,0); shape(Ball);
    popMatrix();
  popMatrix();
  
}

簡単に解説

SeaSellという座標と隣のSeaSellを保持するクラスが等間隔で配置されます
動作は2通り

解説.java
if(隣のSeaSellが登録されてる){
  隣のSeaSellの位置に向かって移動する
}
else{ //隣のSeaSellが登録されていない場合
  上下に動く //←映像の一番右下で上下してるやつ
}

以前投稿した「[Processing]クリック点を保存してアート作品を描く」から参考に制御してみました

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?