Help with boids

0 favourites
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • hi, I'm trying to recreate Sebastian Lague's video on boids in Construct.

    I have the first 2 rules (Separation and Alignment) working okay. Although the turning could use some work.

    Any suggestions on making the turning not so jittery? or at least the concept I should approach? right now I'm just rotating 3 degrees in valid direction where no other boid is. I need it fast to make sure they avoid each other, but it seems like there needs to be some sort of ramp tied into how close the other boid is.

    Also I sometimes get pairs swirling around each other endlessly... :/

    (see video I am trying to follow)


  • Maybe switching the rotate towards angle with an angleLerp?

    anglelerp(a, b, x) Linearly interpolate the angle a to b by x. Unlike the standard lerp, this takes in to account the cyclical nature of angles.

    As a quick test I replaced your rotate toward angle with this:

    -> boid: Set angle to anglelerp(Self.Angle,boids.Angle+3,0.01) degrees

    and it looks to me a bit smoother - you can fiddle with the 0.01 value to change the rotation speed. Also I'm not sure the "boids.Angle+3" part of the lerp is the correct conversion for move "3 degrees towards".

  • Cool demo btw! :-)

  • mekonbekon

    Thanks, it's a fun project to try to emulate..

    I'm not sure anglelerp works because the angle+3 is not constant. It is updating a new angle every tick, also I think it has to be some calculation since it has to turn faster than 3 if the collision is very close.

    So I think the anglelerp just makes it turn slower, which does make it looks less jittery.

    Ideally I'm looking for less collisions.

  • I played a little with your project, check it out:

    Not sure if this works better or worse, but maybe you can use some of my ideas.

  • dop2000 wow, that does look a lot smoother...looks like fish! I like how you tie in the new vector into the loop and I think you are looking for what is closer? cw or ccw? also great way to fix the 2 spinning off endlessly by randomizing the direction.

    I'm no good at RegEx? (whatever its called) can you explain with this is doing?

    (loopindex%2=0 ? dir : -dir)*loopindex

    I just realized that the video I was following has the c# code included!

    one thing I realize is that to find a new angle he uses SphereCast, which checks for a possible collision of the size of the boid rather than just a ray intersection. (its probably more important for him since he's in 3D). C3 doesn't have anything like that except Overlapping at Offset, but I'm not sure how efficient that is..I'm wondering if that would prevent more collisions?

    also I am using bullet behavior whereas he manually calculates the speed/accel/dir. So maybe Bullet is no good here?

     velocity += acceleration * Time.deltaTime;
     float speed = velocity.magnitude;
     Vector3 dir = velocity / speed;
     speed = Mathf.Clamp (speed, settings.minSpeed, settings.maxSpeed);
     velocity = dir * speed;
     cachedTransform.position += velocity * Time.deltaTime;
     cachedTransform.forward = dir;
     position = cachedTransform.position;
     forward = dir;

    pretty sure 'forward' in this case means this.forward (as in the boid's movement). same with position.

  • (loopindex%2=0 ? dir : -dir)*loopindex

    the % means modulo, or the remainder after dividing, so loopindex % 2 will be either 1 (if loopindex is odd) or 0 (if even)

    the rest is an "if then else" statement

    if the statement "loopindex%2=0" is true, then return what is in "dir"

    if it is not true, it will return -dir

    then the result is multiplied by loopindex

    I haven't looked at Dop's sample yet, but was just starting to have fun with yours! :)

  • awesome thanks AllanR!

    I'd love to see what you come up with if you have any ideas!

    Ideally I'd like to use Bullet just because it's easy but I'm going to mess around with not using bullet later and to see if I can emulate Sebastian's (from the video) movement.

    I won't touch the shared version though, I made a copy.

  • Yeah, so basically rays are cast at angles +1,-2,+3,-4...-60, or -1,+2,-3,+4...+60. This is to decrease the number of collision checks and to randomize turn direction. I don't know if you've noticed, but I also decreased LOS angle, otherwise all boids would form a single flock after a few seconds.

    As for the movement, I think Bullet is pretty lightweight. If you move boids with events, I doubt there will be much improvement in performance.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • dop2000 thanks! yes I noticed you made some optimizations. I think it works pretty well.

    no I didn't mean to make better performance than bullet, I mean better behavior with avoiding other boids, i.e. controlling acceleration based on steering away from other boids etc...

  • Still a lot of overhead if there's a lot of instances.

    Might be a great candidate for Wasm.

    Anything like this in the works? Nepeo

  • Hi all,

    Inspired by your efforts I've been playing around with this over the last few days:

    The group and align events seem to work pretty well, but I've got a problem with the avoid routine; at any avoidStrength above zero the birbs all head to the left of the screen and hug the wall.

    Try with just the avoidStrength and avoidRange on.

    The issue lies somewhere within events 24-29.

    Rather than try and calculate a group-based avoidance angle, I've gone for a simpler approach by just reacting to the nearest neighbour. My thinking is: for each birb, pick the next nearest birb; if the neighbour is within avoidRange and in front then try to turn turn 0-45 degrees in the opposite direction from the neighbour.

    Given that they all fall into a pattern it suggests there is some constant biasing their movement, but I can't pin it down. Any ideas?

  • I managed to find a fix:

    I think the issue was that the birb's picking for nearest neigbour was picking itself, so it was always trying to turn to avoid itself. Either way, I jiggled around the picking conditions to make sure the active birb was excluded and it seems to help.

    Other changes:

    - I swapped to using the range object for avoidance (like I do for the other flocking routines) to pare down the selected birbs, which made things a bit easier.

    - The birbs decelerate if they find a target in front of them, spacing them out

    - I now use a single range object for all birbs and shuttle it around for each check, rather than each birb having its own pinned range object.

    The sim works pretty well for about 100 or so birbs with all flocking routines running, but starts to slow down beyond that (still above 30fps but the lag is noticeable) - I'd be super appreciative of any optimisation suggestions.

  • mekonbekon hey this is great! I love how you set it up with the sliders..

    one thing that is weird, when there's a potential head-on-collision the boid doesn't react, it just collides and moves through. Did you do this purposefully? even when I increased the avoidance range it seemed to not do anything about incoming collisions. Just wondering what you thought process was here..

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