我正在尝试将碰撞检测添加到我制作的粒子系统中

时间:2018-02-17 23:39:04

标签: java processing particles

我在处理中这样做本质上是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");
}

1 个答案:

答案 0 :(得分:1)

Stack Overflow并非真正针对一般&#34;我如何做到这一点&#34;输入问题。这是特定的&#34;我试过X,期望Y,但得到了Z而不是#34;输入问题。但我会在一般意义上尝试提供帮助:

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

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