Tôi đã viết một applet hệ thống hạt; hiện tại tôi đang tạo và vẽ riêng từng hạt. (Đây là mã)Vẽ nhiều hạt một cách hiệu quả
BufferedImage backbuffer;
Graphics2D g2d;
public void init(){
backbuffer = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
setSize(WIDTH, HEIGHT);
//creates the particles
for (int i = 0; i < AMOUNTPARTICLES; i++) {
prtl[i] = new particleO();
prtl[i].setX(rand.nextInt(STARTX));
prtl[i].setY(rand.nextInt(STARTY));
prtl[i].setVel(rand.nextInt(MAXSPEED)+1);
prtl[i].setFAngle(Math.toRadians(rand.nextInt(ANGLESPREAD)));
}
//other code
}
public void update(Graphics g) {
g2d.setTransform(identity);
//set background
g2d.setPaint(BGCOLOUR);
g2d.fillRect(0,0,getSize().width,getSize().height);
drawp();
paint(g);
}
public void drawp() {
for (int n = 0; n < AMOUNTPARTICLES; n++) {
if (prtl[n].getAlive()==true){
g2d.setTransform(identity);
g2d.translate(prtl[n].getX(), prtl[n].getY());
g2d.setColor(prtl[n].getColor());
g2d.fill(prtl[n].getShape());
}
}
}
Đó là hiệu suất là không sao, tôi có thể nhận được ~ 40fps với 20.000 hạt (mặc dù, tôi có một máy tính xách tay đàng hoàng). Nhưng sau khi tôi thêm phát hiện va chạm (xem dưới đây), con số đó giảm mạnh xuống dưới 2000,
public void particleUpdate(){
for (int i = 0; i < AMOUNTPARTICLES; i++) {
//other update code (posx, posy, angle etc etc)
for (int j = 0; j < AMOUNTPARTICLES; j++) {
if (i!=j && prtl[j].getAlive()==true){
if(hasCollided(i, j)){
prtl[i].setcolor(Color.BLACK);
prtl[j].setcolor(Color.BLACK);
}
}
}
public boolean hasCollided(int prt1, int prt2){
double dx = prtl[prt1].getX() - prtl[prt2].getX();
double dy = prtl[prt1].getY() - prtl[prt2].getY();
int edges = prtl[prt1].getRadius() + prtl[prt2].getRadius();
double distance = Math.sqrt((dx*dx) + (dy*dy));
return (distance <= edges);
}
Tôi đã tìm kiếm khá một chút cho một cách tốt hơn vẽ các hạt vào màn hình, nhưng các ví dụ hoặc nhầm lẫn tôi hoặc không được áp dụng.
Tôi đang thực hiện tải trọng tính toán của thuyền (quá nhiều). Nhưng tôi không thể nghĩ ra một cách khác để làm điều đó, đề xuất được hoan nghênh.
http://stackoverflow.com/questions/13046033/an-efficient-way-to-simulate-many-particle-collisions –
Đừng quên chấp nhận câu trả lời khi bạn đã quyết định cái nào giúp bạn nhiều nhất. – PearsonArtPhoto