Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Boids - group AI implemented in ThinBASIC

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Boids - group AI implemented in ThinBASIC

    Hi,

    during Game Access 2010 there was one nice technique mentioned "by the way" as approach used for modelling chicken flock animation - the boids.

    I googled a bit on the topic, it seems it is one of the most elemental algorithms for group AI, but I like it very much so I present it here in version, where "boids" (individual members of flock) are visualised as simplified fishes in the sea.

    The code is organised again around entities and concept of actors and animators I mentioned a while ago. It is very close to OOP and for now it seems very natural way to implement ideas in TB (at least for me ).

    The actors are the boids, that means fish in this case, the animators handle the flock behavior. There are two flocks in the demo, one are prey and second the predators.

    The code is strongly based on great pseudocode from here:
    http://www.vergenet.net/~conrad/boids/pseudocode.html
    where the 3 main rules - position, collision avoidance and speed matching are discussed in detail and very clear fashion.

    You will need thinBASIC 1.8.6.0 + latest TBGL to run it (or anything newer).

    It is not meant to be visually very impressive, I could create model for the fishes and animate it, but I wanted the code to stay as "pure" for general boids as it could be, so others can use it in their projects. That is why fishes are represented just with elipsoids.
    As said before, code is strongly OOP, so you can "instantiate" as many boids and their flocks as you need in your projects.

    The main application can be launched from Boids_DemoApp.tBasic, the actor_boid.tBasic and animator_boid.tBasic are just "units" used by the main code.


    I hope you will like it,
    Petr

    UPDATE: Added new download of BoidsEnhanced.zip where you can play more with the script:

    • Arrow keys to move view
    • F5, F6 to change area used for prey
    • F7, F8 to change area used for hunters
    • F9 default camera
    • F12 hunter camera
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Petr Schreiber; 08-01-2011 at 21:10.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    I'm getting an error Petr.

    Line 64, I corrected by making it like this:
    TBGL_EntitySetScale(%sScene, eBoid, Rndf(1, 1.1),1,1)

    Line 83:
    TBGL_EntitySetScale(%sScene, eBoid, 2,1,1)

    Thanks for the demo. I had to make it bigger resolution so I could see it. The current screensize was just too small.
    hWnd = TBGL_CreateWindowEx("Demo of Boids for ThinBASIC - press ESC to quit", 1024, 768, 32, %TBGL_WS_FULLSCREEN)
    Will be good study material, so thanks again.
    Last edited by kryton9; 09-12-2010 at 02:58.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  3. #3
    Thanks Petr,

    Glad I am not a fish!

    Very interesting AI and a powerful basis for more complex behaviour. - I will retain this for reference.

    Charles

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Kent,

    the problem you had was because of old TBGL, that is why I mentioned necessity to update it in the original post. New release allows specifying single factor for scale, as it is the most used scenario and saves some coding. The equivalent of:
    TBGL_EntitySetScale(%sScene, eBoid, Rndf(1, 1.1))
    
    is in old TBGL something like this:
    DIM factor AS Number = Rndf(1, 1.1)
    TBGL_EntitySetScale(%sScene, eBoid, factor, factor, factor)
    
    Thanks for trying and for the comments, you are right it is not visible very much, I made it even harder by using fog But I liked the feeling when the fishes are almost invisible, then change direction and thanks to specular material their bodies shine in the light from the dark.

    Charles - thanks! I am sure it could get serious performance boost using Oxygen, as it uses lot of loops. With bigger number of fishes it becomes a bit slow here.

    The use of this technique is not limited to fishes, but also to birds, sheeps (and other animals organised in "hordes") + for example for part of movement AI in FPS or strategy games, where group of characters travel through environment.


    Petr
    Last edited by Petr Schreiber; 09-12-2010 at 10:31.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    As this demo is not very intensive on fragment operations, it could be also possible to directly switch to desktop resolution immediately without any hesitation:
      Dim width, height, bitDepth As Long
      TBGL_GetDesktopInfo(width, height, bitDepth)
      hWnd = TBGL_CreateWindowEx("Demo of Boids for ThinBASIC - press ESC to quit", width, height, bitDepth, %TBGL_WS_FULLSCREEN) 
      TBGL_ShowWindow
    

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    I downloaded the latest tbgl before I ran the demo Petr. I must not have installed it correctly, will check again. Thanks.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi Kent,

    after I read about your problems, I deleted TBGL from Lib directory, downloaded the one marked as latest, "installed" it and the script works fine. Maybe you really just extracted it to other folder. Please let me know.

    Another possibility is that in folder you extracted script to there is old thinBASIC_TBGL.DLL placed by some weird accident.

    I attach what you should see when looking at thinBASIC_TBGL.DLL in your Lib directory (of course it is from Czech Windows, so some things will be a bit different )


    Petr
    Attached Images Attached Images
    Last edited by Petr Schreiber; 09-12-2010 at 23:44.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  8. #8
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    Thanks Petr. I must have clicked on one of the windows 7 prompts wrongly and not replace what was there, that is all I can think of.

    It is working fine now. Again really cool to watch, good job!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  9. #9
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109
    hi petr, I have tried your swimming animals and boids_demoApp works fine. I have resized the camera to 10,10,10 to see more of the swarm but perhaps here's a mistake.. "actor_" + "animator_boid.tbasic" files there may be a problem with
    "tvector3f"
    . I have installed new tbgl version and copied new tbgl dll into lib folder. interesting work, thanks for sharing that! frank
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Kent,

    I am happy it finally works for you, perfect

    Frank,

    the actor_* and animator_* files are just include files, which are not supposed to be launched separately. The Boids_DemoApp.tBasic is the main application which uses them.

    The reason you get the run time error when trying to launch them separately is because they use type defined in TBGL. The main application uses "TBGL" and then includes these files, that is why it works in the main app and not alone. But this is by design.

    Thanks for trying!


    Petr
    Last edited by Petr Schreiber; 10-12-2010 at 11:52.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Page 1 of 2 12 LastLast

Similar Threads

  1. Article: Boids - group AI implemented in ThinBASIC
    By Petr Schreiber in forum vBCms Comments
    Replies: 0
    Last Post: 09-01-2011, 11:53

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •