在Processing中使用正弦算法缓和路径

时间:2013-06-11 11:34:21

标签: algorithm processing easing sine trail

这是我的源代码:

int index;
int num = 60;
float mx[] = new float[num];
float my[] = new float[num];
float explosion;
float x;
float y;
float px;
float py;
float xold;
float yold;
float xplode1;
float yplode1;
float xplode2;
float yplode2;
float xplode3;
float yplode3;
float xplode4;
float yplode4;
float easing = 0.05;

void setup() {
  size(1366, 768);
  noStroke();
//  noFill();
  fill(25, 155);
}

void draw() {
  int which = frameCount % num;

  explosion = explosion + 0.32;
  background(92, 55, 169);

  float targetX = mouseX;
  float dx = targetX - px;
  float lx = targetX - x;
  if (abs(dx) > 1) {
    mx[which] += dx * easing;
    x += lx * easing;
    if (mousePressed && (mouseButton == LEFT)) {
      xplode1 = dx + 50 + sin(explosion)*30;
      xplode2 = dx + 50 + sin(explosion)*30;
      xplode3 = dx - 50 - sin(explosion)*30;
      xplode4 = dx - 50 - sin(explosion)*30;
    }
    else {
      xplode1 = -10;
      xplode2 = -10;
      xplode3 = -10;
      xplode4 = -10;
    }
  }

  float targetY = mouseY;
  float dy = targetY - py;
  float ly = targetY - y;
  if (abs(dy) > 1) {
    my[which] += dy * easing;
    y += dy * easing;
    if (mousePressed && (mouseButton == LEFT)) {
      yplode1 = dy + 50 + sin(explosion)*30;
      yplode2 = dy - 50 - sin(explosion)*30;
      yplode3 = dy - 50 - sin(explosion)*30;
      yplode4 = dy + 50 + sin(explosion)*30;
    }
    else {
      yplode1 = -10;
      yplode2 = -10;
      yplode3 = -10;
      yplode4 = -10;
    }
  }

  for(int i = 0;i<num;i++){
    index = (which + 1 + i) % num;
    ellipse(mx[index], my[index], i, i);
  }
  ellipse(xplode1, yplode1, 10, 10);
  ellipse(xplode2, yplode2, 10, 10);
  ellipse(xplode3, yplode3, 10, 10);
  ellipse(xplode4, yplode4, 10, 10);
}

我想有一条约60的痕迹,并且整个事情也有一些缓和。我已经让每个功能单独工作,但是当我添加了褪色时。有很多不需要的变量,我根本没有清理过代码,我已经工作了几个小时,我知道可能有一个非常简单的解决方案,我现在看不到。任何帮助都会很棒,谢谢。

1 个答案:

答案 0 :(得分:1)

不要咬你的东西,也不要咀嚼,学习小东西。 向量将使您的代码更加混乱。您可以在Processing site上找到Vector类的详细说明。这样,不是有两个不同的变量xplode1和xplode2,而是会有一个Vector对象存储这两个值。您可能最初会发现这些概念很困难,但它们将成为未来草图的小型工具。

如果您对变量,函数,条件和循环等基本概念感到满意,请开始学习 OOP (面向对象编程)。再次,Daniel Shiffman comes to help

此外,在询问StackOverflow时更具体。解决问题通常意味着找到正确的问题。