我有一个简单的草图(在Processing),基本上有一堆点在四处游荡,如果他们接触到对方,他们会打(每个人都有一个实力值,每次赢时增加,如果相等,赢家是随机的选择)如何使Processing.org草图更高效?
它适用于约5000个12像素的“僵尸”(有一个半秒的轻微减速,而僵尸最初相互碰撞),问题是当僵尸变得更小,他们不要相互碰撞,并且减速时间可以更长。
代码非常简单 - 基本上每个僵尸都是一个类,它有一个X/Y坐标。每帧所有的僵尸都会轻推一个像素,随机转动lurching
度(或不)。我认为缓慢的最大原因是碰撞检测 - 每个僵尸检查每隔一个(所以僵尸1检查2-5000,僵尸2检查1,3-5000等。)
我想保留所有内容简单,“简单处理”(不使用外部库,这可能是更有效,更容易,但我不觉得学习是非常有用的)
int numZombies = 5000;
Zombie[] zombies = new Zombie[numZombies];
void setup(){
size(512, 512);
noStroke();
for(int i = 0; i < numZombies; i++){
zombies[i] = new Zombie(i, random(width), random(height), random(360), zombies);
}
}
void draw(){
background(0);
for(int i = 0; i < numZombies; i++){
zombies[i].move();
zombies[i].display();
}
}
class Zombie{
int id; // the index of this zombie
float x, y; // current location
float angle; // angle of zombies movement
float lurching = 10; // Amount angle can change
float strength = 2;
boolean dead = false; // true means zombie is dead
float diameter = 12; // How big the zombie is
float velocity = 1.0; // How fast zombie moves
Zombie[] others; // Stores the other zombies
Zombie(int inid, float xin, float yin, float inangle, Zombie[] oin){
id = inid;
x = xin;
y = yin;
angle = inangle;
others = oin;
}
void move(){
if(dead) return;
float vx = velocity * sin(radians(180-angle));
float vy = velocity * cos(radians(180-angle));
x = x + vx;
y = y + vy;
if(x + vx < 0 || x + vx > width || y + vy < 0 || y + vy > height){
// Collided with wall
angle = angle + 180;
}
float adecide = random(3);
if(adecide < 1){
// Move left
angle=angle - lurching;
}
else if(adecide > 1 && adecide < 2){
// Don't move x
}
else if(adecide > 2){
// Move right
angle = angle + lurching;
}
checkFights();
}
void checkFights(){
for (int i=0; i < numZombies; i++) {
if (i == id || dead || others[i].dead){
continue;
}
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
if (distance < diameter){
fight(i);
}
}
}
void fight(int oid){
Zombie o = others[oid];
//println("Zombie " + id + "(s: "+ strength +") fighting " + oid + "(s: "+ o.strength +")");
if(strength < o.strength){
kill();
o.strength++;
}
else if (strength == o.strength){
if(random(1) > 0.5){
kill();
o.strength++;
}
else{
o.kill();
strength++;
}
}
}
void kill(){
dead = true;
}
void display(){
if(dead) return;
ellipse(x, y, diameter, diameter);
}
}
我曾想过这样的事情,但对于区域之间的冲突?我的另一个/类似的想法是只检查附近的僵尸是否有碰撞,但是你仍然必须检查每个僵尸的其他位置。 – dbr 2009-01-07 08:47:53