java - 我正在尝试为我制作的粒子系统添加碰撞检测

标签 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");
}

最佳答案

Stack Overflow 并不是真正为一般的“我该怎么做”类型的问题而设计的。它适用于特定的“我尝试了 X,预期是 Y,但得到了 Z”类型的问题。但我会尽力在一般意义上提供帮助:

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

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

关于java - 我正在尝试为我制作的粒子系统添加碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48846998/

相关文章:

java - 像素化 : Cannot read dicom file

java - 为什么java可执行文件不接受参数文件?

java - 绘制数千个粒子的更高效方法(Java/Android)

python - 用于说明温度的色标,Python 脚本

java - eclipse更新速度很慢

java - 多线程通信: how good is the use of Atomic Variables like AtomicInteger? 为什么没有AtomicFloat?

javascript - 如何沿着多边形移动对象

java - 用许多方法分解一个类以提高可读性

java - 将 PImage 转换为 Mat - Processing IDE P3D (OpenGL) 渲染器中的 Java OpenCV

javascript - Three.js - 如何使用 Three.js 创建烟雾?