0

I am working on a boids simulation in python with pygame. The basic behavior works, but I am now trying to add obstacle avoidance and a predator. So far, I am having trouble figuring out how to implement these behaviors.

First of all, I am trying to have the prey boids flee and the predators attack. In order to do that, I need to somehow find the closest boid. How would I do that?

Also, for obstacle avoidance, could someone please explain how I would make the prey avoid, but not actively flee from, a static obstacle?

My full code is here (github). I would really appreciate any and all explanations as to how I would accomplish these two things.

Thanks!

EDIT:

fucas has shown me how to accomplish that, but now I have a new problem.

For the predator and prey behavior I now have this:

    def attack(self, prey_list):
        nearest_prey = None
        shortest_distance = None

            for prey in prey_list:
                distX = self.rect.x - prey.rect.x
                distY = self.rect.y - prey.rect.y
                d = distX*distX+distY*distY
                if not shortest_distance or d < shortest_distance:
                    shortest_distance = d
                    nearest_prey = prey

                # do something with nearest_prey, shortest_distance
                trajectory_x = self.rect.x - nearest_prey.rect.x
                trajectory_y = self.rect.y - nearest_prey.rect.y

                self.velocityX -= trajectory_x
                self.velocityY -= trajectory_y

And this for the prey:

    def defend(self, predator_list):
        nearest_predator = None
        shortest_distance = None

            for predator in predator_list:
                distX = self.rect.x - predator.rect.x
                distY = self.rect.y - predator.rect.y
                d = distX*distX+distY*distY
                if not shortest_distance or d < shortest_distance:
                    shortest_distance = d
                    nearest_predator = predator

                # do something with nearest_prey, shortest_distance
                trajectory_x = self.rect.x - nearest_predator.rect.x
                trajectory_y = self.rect.y - nearest_predator.rect.y

                self.velocityX += trajectory_x
                self.velocityY += trajectory_y

(This code it applied last after all of the other rules).

Fake Name
  • 813
  • 2
  • 9
  • 17
  • Just so that I know how to improve my future posts, why the downvote? – Fake Name Feb 03 '16 at 02:24
  • to find nearest you have to calculate distance to every prey. Use Pythagoras `a^2 + b^2 = c^2` where `c` is distance and `a = x1-x2`, and `b = y1-y2`. btw to find nearest you can compare `c^2` instead of `c` – furas Feb 03 '16 at 03:33
  • Is there a more efficient way? Also, how would I compare the distance? – Fake Name Feb 03 '16 at 03:45
  • Btw I already have a function setup to calculate distance. I just need to know how to quickly compare all of them. – Fake Name Feb 03 '16 at 03:52
  • Python has `min(list_with_numbers)` – furas Feb 03 '16 at 03:54
  • Thanks, I will try that out. For obstacle avoidance, any ideas? – Fake Name Feb 03 '16 at 04:13
  • for obstacle avoidance maybe you could `collision detection`.PyGame has `pygame.Rect` class to keep object position an size, and this class has colliderect` to check collision with another `Rect`. But `collision detection` need more code. – furas Feb 03 '16 at 04:46
  • Yeah, but I want the boids to avoid the obstacle, not just bounce off of it. – Fake Name Feb 03 '16 at 04:48
  • I updated my question. How do I get that part to work? – Fake Name Feb 03 '16 at 05:05
  • BTW: I think someone downvote becaus your problem is too broad for SO. And because SO is place for question with short code and error message or returned values different than expected results. Your new text in question could be good question on SO (but without rest of your question) – furas Feb 03 '16 at 05:35
  • you can put `nearest_distance = min(...)` outside `for` loop. You can do it only once. – furas Feb 03 '16 at 05:37
  • So, should I get rid of everything before EDIT? – Fake Name Feb 03 '16 at 23:08
  • How should I fix this? – Fake Name Feb 04 '16 at 01:02
  • The question has been updated. – Fake Name Feb 04 '16 at 03:01
  • I'm not sure the recent edits are really appropriate, since you're really asking a different question now than you were before (and the old answers don't appear to make much sense now). Please consider reverting those edits and asking your new question separately. – Blckknght Feb 04 '16 at 23:43
  • Would it be alright if I edited the question so that the answer is valid, but still left the rest in? – Fake Name Feb 04 '16 at 23:46
  • @BlckknghtI I edited the question. Is this better? – Fake Name Feb 05 '16 at 00:32

1 Answers1

1

To find shortest distance you can do

def attack(self, prey_list):
    d_list = []

    for prey in prey_list:
        distX = self.rect.x - prey.rect.x
        distY = self.rect.y - prey.rect.y
        d = distX*distX+distY*distY
        d_list.append(d)

    shortest_distance = min(d_list)

To get shortest distance and prey you can do

def attack(self, prey_list):
    nearest_prey = None
    shortest_distance = None

    for prey in prey_list:
        distX = self.rect.x - prey.rect.x
        distY = self.rect.y - prey.rect.y
        d = distX*distX+distY*distY
        if not shortest_distance or d < shortest_distance
            shortest_distance = d
            nearest_prey = prey

   # do something with nearest_prey, shortest_distance
   print nearest_prey, shortest_distance
furas
  • 134,197
  • 12
  • 106
  • 148