简体   繁体   English

如何在处理中旋转和振荡相同的对象

[英]How do I rotate and oscillate the same object in Processing

I am creating a snowflake simulation in Processing however i am not sure how to perform more than one transformation to an image as it seems you can only perform one. 我正在处理中创建雪花模拟,但是我不确定如何对图像执行多个转换,因为您似乎只能执行一次转换。

class Snowflake{
  float imgWidth;
  float imgHeight;
  PVector pos;
  PVector vel;
  final float firstXPos;

  float a = 0.0;
  float angularVel = 0.01;

  float x;
  float amp;
  float period;

  Snowflake(float xWidth, float yHeight){
    imgWidth = xWidth;
    imgHeight = yHeight;
    pos = new PVector(random(width), 0);
    vel = new PVector(0,1);
    firstXPos = this.pos.x;
  }
  void descend(){
    amp = 75;  
    period = 200;

    x = amp * sin((frameCount/period) * TWO_PI);

This is where i am trying to rotate the image and oscillate it back and forth. 这是我试图旋转图像并使其来回振荡的地方。

    pushMatrix();
    translate(firstXPos,this.pos.y);
    image(snowflakeImg, x, this.pos.y, imgWidth, imgHeight);
    popMatrix();

    //creating a line for oscillation reference
    //translate(firstXPos, this.pos.y);
    //stroke(255);
    //line(0,0,x,y);

}
  void update(){
    pos.add(vel);
    a += angularVel;
  }

}

Here is my sketch, just loading in assets and setting up the sketch 这是我的草图,只需加载资产并设置草图

PImage snowflakeImg;
Snowflake snowFlake;

void setup() {
  imageMode(CENTER);
  snowflakeImg = loadImage("snowflake.png", "png");
  snowFlake = new Snowflake(25, 25);
  size(800,600);
}

void draw(){
  background(0);
  snowFlake.descend();
  snowFlake.update();  
}

You're creating a single Snowflake instance. 您正在创建一个Snowflake实例。 You need to create more, then update each one. 您需要创建更多,然后更新每个。

In your case you should initialise an array of Snowflake objects first (at the top where you declare your variables). 在您的情况下,您应该首先初始化一个Snowflake对象数组(在声明变量的顶部)。 Here's an example allocating an array of 99 Snowflake objects: 这是分配99个Snowflake对象的数组的示例:

int numSnowflakes = 99;
Snowflake[] snowflakes = new Snowflake[numSnowflakes]; 

Then in setup() you can initialise each array element as a new Snowflake instance: 然后在setup() ,可以将每个数组元素初始化为新的Snowflake实例:

for(int i = 0 ; i < numSnowflakes; i++){
    snowflakes[i] = new Snowflake(25, 25);
  }

Finally in draw() you can loop through each object so it can descend() and update() : 最后,在draw()您可以循环遍历每个对象,以便它可以descend()update()

for(int i = 0 ; i < numSnowflakes; i++){
    snowflakes[i].descend();
    snowflakes[i].update();  
  }

If you're not familiar with arrays in Processing yet I can recommend the following resources: 如果您还不熟悉“处理”中的数组,我可以推荐以下资源:

  1. Processing arrays tutorial 处理数组教程
  2. Processing ArrayObjects example 处理ArrayObjects示例
  3. Daniel Shiffman's Array of Objects Coding Train video tutorial along with Arrays and Loops Daniel Shiffman的“对象数组编码”视频教程以及数组和循环

Once you've got the hang of this you should look into ArrayList as well. 一旦掌握了这一点,您也应该研究ArrayList

Update To address your comment, you can use push/pop matrix calls to further isolate coordinate systems and rotate the image around it's centre: 更新为了解决您的评论,您可以使用push / pop矩阵调用来进一步隔离坐标系并围绕其中心旋转图像:

x = amp * sin((frameCount/period) * TWO_PI);
// enter local coordinate system #1
pushMatrix();
// move to flake position
translate(firstXPos,this.pos.y);
  // enter local coordinate system #2
  pushMatrix();
    // move to updated (oscillated) x position
    translate(x, this.pos.y);
    // rotated from translated position (imageMode(CENTER) helps rotate around centre)
    rotate(frameCount * 0.1);
    // render the image at it's final transformation
    image(snowflakeImg,0,0, imgWidth, imgHeight);
  popMatrix();
popMatrix();

For more info check out the Coordinate Systems Processing tutorial . 有关更多信息,请查看“ 坐标系处理”教程

For reference here's a test sketch I've used with each flake at random width: 供参考,这是我对每个薄片使用随机宽度的测试草图:

PImage snowflakeImg;
Snowflake snowFlake;

int numSnowflakes = 99;
Snowflake[] snowflakes = new Snowflake[numSnowflakes]; 

void setup() {
  imageMode(CENTER);
  //snowflakeImg = loadImage("snowflake.png", "png");
  PGraphics snowflakeG = createGraphics(25,25);
  snowflakeG.beginDraw();
  snowflakeG.rectMode(CENTER);
  snowflakeG.rect(0,0,25,25);
  snowflakeG.endDraw();
  snowflakeImg = snowflakeG;

  for(int i = 0 ; i < numSnowflakes; i++){
    snowflakes[i] = new Snowflake(25, 25);
  }

  size(800,600);
}

void draw(){
  background(0);
  for(int i = 0 ; i < numSnowflakes; i++){
    snowflakes[i].descend();
    snowflakes[i].update();  
  }
}
class Snowflake{
  float imgWidth;
  float imgHeight;
  PVector pos;
  PVector vel;
  final float firstXPos;

  float a = 0.0;
  float angularVel = 0.01;

  float x;
  float amp;
  float period;

  Snowflake(float xWidth, float yHeight){
    imgWidth = xWidth;
    imgHeight = yHeight;
    pos = new PVector(random(width), 0);
    vel = new PVector(0,1);
    firstXPos = this.pos.x;
  }
  void descend(){
    amp = 75;  
    period = 200;

    x = amp * sin((frameCount/period) * TWO_PI);
    // enter local coordinate system #1
    pushMatrix();
    // move to flake position
    translate(firstXPos,this.pos.y);
      // enter local coordinate system #2
      pushMatrix();
        // move to updated (oscillated) x position
        translate(x, this.pos.y);
        // rotated from translated position (imageMode(CENTER) helps rotate around centre)
        rotate(frameCount * 0.1);
        // render the image at it's final transformation
        image(snowflakeImg,0,0, imgWidth, imgHeight);
      popMatrix();
    popMatrix();

    //creating a line for oscillation reference
    //translate(firstXPos, this.pos.y);
    //stroke(255);
    //line(0,0,x,y);

}
  void update(){
    pos.add(vel);
    a += angularVel;
  }

}

You should also check out Daniel Shiffman's Snowfall coding challenge . 您还应该查看Daniel Shiffman的Snowfall编码挑战 Even though it's p5.js the same logic can be easily applied in Processing. 即使是p5.js,相同的逻辑也可以轻松应用于Processing中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM