Help with Avoider type game

0 favourites
  • 8 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Okay, I've been working on this all day, and I feel like I am getting close to a (clumbsy) solution that will work for me. First, the Capx:

    What I am trying to do:

    I'm trying to write a "herding" game, where you give the dog commands, it runs out and basically chases around the sheep. The sheep should stay bunched up (in a little herd) but should move away from the dog. They tend to move away as a group, but if the dog gets too close (buzzes the sheep) they will eventually split off.

    Right now, I am JUST trying to get a working herd of four sheep to clump together, move together, and flee the dog. I have them clumping (sort of) around the "ram" object and following the ram, and the ram is supposed to be fleeing the dog.

    Preferably, the ram would move away from the dog more or less directly away. IE: if the dog runs to the right at the sheep, the sheep run away right. If the dog runs right and up from below and left, the sheep should run right and up as well.

    Basically, the sheep need to avoid the dog. I can get them to FOLLOW the dog all day long using "Set angle toward dog.X,dog.Y" and a setting a speed, but inverting the equation with "Set angle toward -dog.X,-dog.Y" causes the sheep to run in the same direction every time.

    I tried the Boids flocking behavior addon earlier this evening, and ran into problems with that, also. The sheep wound up stacking on top of one another, or when I finally got them to behave like solid objects would push the ram all over the screen, then also they blocked the ram from moving if the dog got close.

    Any assistance or suggestions on this would be helpful.

  • Instead of Set Angle Towards try Set Angle, to dog.angle

    It seemed to work for me, if that is what you are going for.


  • Thanks. The set angle to dog.angle worked a treat. Had some difficulty with the file you posted, it tells me you are using R117, but I can only find R114. I found the spot in my file you were indicating and changed that.

    Capx file: Sheepherder.75

    Then I tried to fix the herding behavior. I made a few changes, removed the ram, added a "Herd" object and was trying to get the sheep all to move to the "Herd" object (pink circle, later to be made invisible, presently visible for testing) using an If-Else loop:

    If: dog near sheep, avoid dog (event 4)

    Else if sheep is not near herd, move to herd (event 5)

    Else stop. (event 6)

    Only I am running into the problem that all of the sheep are "Sheep" in the If-Else, so the sheep are satisfying all conditions of the IF-Else at the same time, one is close to the dog, one is close to the herd, one is far away, and one is ice-skating about like a fool.

    Is there a way (without calling each sheep "Sheep 1, Sheep 2, Sheep 3, Sheep 4") to force each sheep to go through the same If-Else? or have I implemented the If-Else improperly in C2?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hmm, yeah I always run the latest beta of construct, sorry.

    What if you you first do a For Each Sheep, then a sub event with your If dog near sheep?

  • <img src="" border="0" />

    This is what I have going on right now, and they all seem to be skating around like fools now.

    Have I not got the proper grasp of how C2 handles If-Else? I tried adding a system-else into the sub groups, but it kicks it out as "Not Valid Here"

    Edit: Just realized the text got pretty trashed by the image resize.

    4 - System for each sheep                            

        System distance(dog.x,dog.y,sheep.x,sheep.y) <= 30

        System Every Tick

       5 - System for each sheep

           System distance(herd.x,herd.y,sheep.x,sheep.y) >=30

           System Every Tick

          6 - System for each sheep

              System distance(herd.x,herd.y,sheep.x,sheep.y)

              System Every Tick


    4 sends the sheep at dog.angle by speed,

    5 sends the sheep at herd.x,herd.y by speed

    6 stops the sheep at the herd.

  • Seems the nesting events was causing the problem. Dropped them all down to the same priority and it seems to be working better. Still can't quite get them to react to the dog object, they seem too focused on staying with the herd object. Makes sense a bit, but I have a feeling it will give me problems later when I try to include shedding behavior (splitting a sheep from the herd).

    Might try using a !dog.angle and !herd.angle...

  • Good luck. Sometimes even just posting and typing out the issues can help you solve it. I don't know how many times I posted an issue, just to find a solution right after I post :).

  • FINALLY got a TRUE avoidance behavior out of the "herd" object.

    I had been using the "Set Angle To dog.Angle" as you suggested, but that was giving me bizare behavior when the dog and herd object were parallel, instead of moving away, you could actually move the herd object along side the dog, even if the dog changed direction so that at times the sheep were chasing the dog!

    After reading the angle commands a little more, I settled on:

    "Set Angle to (Angle(dog.X,dog.Y,herd.X,herd.Y)) degrees".

    This fixed the odd chasing the dog behavior and caused the herd to move away from the dog no matter how they were facing.

    I had tried:

    "Set Angle to (Angle(dog.X,dog.Y,herd.X,herd.Y)+180) degrees"

    at first, but the problem I ran into there was the herd always faced and moved towards the dog object. Useful if you are looking to cause the object to run AT the "avoider" but useless in my case.

    Thanks ArcadEd, your suggestions on Every Sheep and other pointers put me in the right direction!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)