Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 105367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:25:41+00:00 2026-05-11T01:25:41+00:00

I have a simple sketch (in Processing ), basically a bunch of dots wander

  • 0

I have a simple sketch (in Processing), basically a bunch of dots wander around, if they come into contact with each other they fight (each has a strength value, increased each time they win, if it’s equal the winner is randomly chosen)

It works well with about 5000 12-pixel ‘zombies’ (there’s a slight slowdown for a half a second, while the zombies initially collide with each other), the problem is when the zombies are made smaller, they don’t collide with each other as quick, and the slowdown can last much longer..

The code is really simple – basically each zombie is a class, which has an X/Y coordinate. Each frame all the zombies are nudged one pixel, randomly turning lurching degrees (or not). I think the biggest cause of slowness is the collision detection – each zombie checks every other one (so zombie 1 checks 2-5000, zombie 2 checks 1,3-5000 etc..)

I’d like to keep everything simple, and ‘plain Processing’ (not using external libraries, which might be more efficient and easy, but I don’t find it very useful for learning)

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);   } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:25:41+00:00Added an answer on May 11, 2026 at 1:25 am

    Like 1800 INFORMATION says, somehow you need to reduce the number of comparisons.

    Splitting the playing area into zones is a good idea. I would imagine the time it takes to compare current location against zone boundaries and add/remove zombies from the appropriate collections is worth it. Assuming they generally will go in straight lines, they shouldn’t be changing zones too frequently.

    We have the problem though of possible collisions between zones. To piggyback on the idea, you could divide the screen into 4 zones then 9 zones again. Think a tic-tac-toe board overlaid on a cross. This is a bad drawing, but:

        |  ! |     |  ! | ----+--!-+----     |  ! | ====|==x=|==== ----+--!-+----     |  ! |     |  ! | 

    This way each zombie is in two zones at once and every border in one scheme is covered by another zone. You wouldn’t even have to check all the same zombies again because either we’d be dead or they would. So the only double-processing is a single others[i].dead check.


    Another thing I can see quickly is you still loop through the rest of the elements even though you’re dead:

      if (i == id || dead || others[i].dead){     continue;   } 

    It might not save a lot of processing, but it can certainly cut some instructions if you:

      if (dead) return; 

    instead.


    Also as a side note, do you want to be checking the diameter or the radius against the distance?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer sigh ... seconds after I asked, I stumbled across http://download.opensuse.org/repositories/Apache:/Modules/Apache_openSUSE_11.1/repodata/… May 11, 2026 at 3:52 pm
  • added an answer When you compile, the old database gets erased and a… May 11, 2026 at 3:52 pm
  • added an answer I do a lot of scraping, using excellent python packages… May 11, 2026 at 3:52 pm

Related Questions

I've written PL/SQL code to denormalize a table into a much-easer-to-query form. The code
This is different than the simple 2 column layout. I need to have this
Here's a mockup: A mockup http://img.skitch.com/20090228-mqdj17xijycc98spf181a8q6q7.jpg I have been trying to find some sample
I have a simple webform that will allow unauthenticated users to input their information,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.