【问题标题】:Easing trail with sine algorithms in Processing在处理中使用正弦算法轻松跟踪
【发布时间】:2013-06-07 05:49:22
【问题描述】:

这是我的源代码:

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 的轨迹,并且对整个事情也有一些缓和。我已经让每个功能都单独工作,但是当我添加到褪色中时。有很多不需要的变量,我根本没有清理代码,我已经研究了几个小时,我知道可能有一个非常简单的解决方案,我现在看不到。任何帮助都会很棒,谢谢。

【问题讨论】:

    标签: algorithm processing easing trigonometry trail


    【解决方案1】:

    不要咬得比你能咀嚼的多,学点小事。 向量将使您的代码不那么混乱。您可以在Processing site 上找到 Vector 类的详细说明。这样,将有一个 Vector 对象存储这两个值,而不是具有两个不同的变量 xplode1 和 xplode2。一开始您可能会发现这些概念很困难,但它们将成为未来草图的宝贵工具。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多