【问题标题】:I am trying to add collision detection to this particle system I made我正在尝试将碰撞检测添加到我制作的这个粒子系统中
【发布时间】:2018-02-17 23:39:04
【问题描述】:

我在本质上是 java 的处理中这样做,我以前从未尝试过这样的事情。找不到任何使用数组映射像素的碰撞检测示例。

我并不是真的想让它们产生逼真的碰撞。我在想它会像撞到墙一样有相同的响应,这只是为了让它在任何适合它撞到的墙的轴上改变方向。

我已尝试检查 x 和 y 位置是否相同,但似乎无法实现。对此我将不胜感激。

import java.util.Arrays;

int numOfParticles = 10;


float[] x = new float[numOfParticles]; //initial position of y only matters
float[] px = new float[numOfParticles];
float[] y = new float[numOfParticles]; 
float[] py = new float[numOfParticles];

int speed = 10;//inversly related to speed

float[] xIncrement = new float[numOfParticles]; //the ratio of increments determines the pattern
float[] yIncrement = new float[numOfParticles]; // it is the slope of the line

//float xIncrement = 10/speed; //the ratio of increments determines the pattern
//float yIncrement = 11/speed; // it is the slope of the line

color currentColor;
int alpha = 100;//range of 0-255

//radius of ball
int radius = 1;

//thickness of line behind ball
int thickness = 5;

int rateOfColor = 5; //this is inversely related to rate but also changes the range of colors

int maxColor = 255;
int minColor = 0;

void setup(){
  size(500,500);
  background(0);
  colorMode(HSB);
  strokeWeight(thickness);
  frameRate(60);

  //initialize particles
  for(int i = 0;i<numOfParticles;i++){
    xIncrement[i] = random(0,100)/speed; //the ratio of increments determines the pattern
    yIncrement[i] = random(0,100)/speed; // it is the slope of the line
    x[i] = random(0,width);
    px[i] = x[i];
    y[i] = random(0,height);
    py[i] = y[i];
  }

  //you can either initialize all of them individually or do a random one
  //x[0] = 0;
  //px[0] = x[0];
  //y[0] = 450;
  //py[0] = y[0];

  //x[1] = width;
  //px[1] = x[1];
  //y[1] = 450;
  //py[1] = y[1];
}

void draw(){  
  background(0);  //comment out for criss cross

  for(int i = 0; i < numOfParticles; i++){
    particle(i);
  }

}

void particle(int particleNum){
  currentColor = color(minColor + (x[particleNum]/rateOfColor)%maxColor,255,255,alpha);

  stroke(currentColor);
  fill(currentColor);

  ellipse(x[particleNum],y[particleNum],radius,radius);
  line(px[particleNum],py[particleNum],x[particleNum],y[particleNum]);


  px[particleNum] = x[particleNum];
  py[particleNum] = y[particleNum];

  y[particleNum]+= yIncrement[particleNum];
  x[particleNum]+= xIncrement[particleNum];

  if(x[particleNum] > width + 1 || x[particleNum] < 0){
    x[particleNum] -= 2*xIncrement[particleNum];
    xIncrement[particleNum]*=-1;
  }

  if( y[particleNum] > height + 1 || y[particleNum] < 0){
    y[particleNum] -= 2*yIncrement[particleNum];
    yIncrement[particleNum]*=-1;
  }

  //if(Arrays.binarySearch(x,x[particleNum]) >= 0 && Arrays.binarySearch(y,y[particleNum]) >= 0){
  //  xIncrement[particleNum]*=-1;
  //  yIncrement[particleNum]*=-1;
  //  print("*\n");
  //  stop();
  //} 

  print("x[0] = " + x[0] + "\n");
  print("x[1] = " + x[1] + "\n");
  print("y[0] = " + y[0] + "\n");
  print("y[1] = " + y[1] + "\n");
}

【问题讨论】:

  • 请发布minimal reproducible example,这样我们就不必猜测或假设缺少信息,例如ellipseline 的定义
  • 它正在处理中,我在我的帖子中进行了解释。它建立在java之上。它的参考资料在 processing.org/reference/ 对不起,如果我不够清楚

标签: java processing particles


【解决方案1】:

Stack Overflow 并不是针对一般的“我该怎么做”类型的问题而设计的。这是针对特定的“我尝试了 X,预期 Y,但得到了 Z”类型的问题。但我会尽量提供一般意义上的帮助:

您需要break your problem down into smaller pieces,然后一次拿走这些碎片。不要担心整个粒子系统。使其适用于单个粒子。对collision detection做一些研究。

然后,如果您遇到困难,您可以发布更具体的问题以及MCVE。祝你好运。

【讨论】:

  • 感谢您的意见。我会牢记这一点,以备将来之用。它实际上把我在这里的很多互动放到了上下文中。我试过用单个粒子来做,但似乎无法让它发挥作用。无论如何我会坚持下去。我肯定会到达那里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
相关资源
最近更新 更多