GithubHelp home page GithubHelp logo

cultrarius / swarmz Goto Github PK

View Code? Open in Web Editor NEW
131.0 7.0 9.0 17 KB

A free, header-only C++ swarming (flocking) library for real-time applications

License: The Unlicense

CMake 1.45% C++ 90.65% C 7.89%
boids swarm velocity position acceleration game public-domain library flocking algorithm unreal-engine

swarmz's Introduction

Swarmz

A free, header-only C++ swarming (flocking) library for real-time applications.

An example usage can be seen in this particle editor plugin for the Unreal Engine 4.

Usage

Just include the header file to start:

#include "swarmz.h"

using namespace sw;

To create a new swarm, a pointer to the list of Boids is required. A boid is a single member of the swarm. Each boid has a position and a velocity:

// define our swarm members
vector<Boid> boids;
boids.push_back(Boid(Vec3(1, 0, 0), Vec3(1, 0, 0)));
boids.push_back(Boid(Vec3(1.5, 0, 0), Vec3(1, 1, 0)));
boids.push_back(Boid(Vec3(1, 0.5, 0.5), Vec3(0, 1, 0)));
boids.push_back(Boid(Vec3(4, 4, -2), Vec3(1, 0, 0)));

//create the swarm!
Swarm swarm(&boids);

The swarm has a lot of different settings, but the most important ones are the following:

  • PerceptionRadius - determines the vision radius of each boid. Only boids within this distance influence each other.
  • SeparationWeight - how much boids repel each other
  • AlignmentWeight - how much boids want go in the same direction
  • CohesionWeight - how much boids want to be in the center of the swarm
  • MaxAcceleration - how fast each boid can change its direction
  • MaxVelocity - how fast each boid can move

Each setting has a sensible default parameter, but playing around with them is half the fun:

// configure swarm
swarm.PerceptionRadius = 100;
swarm.CohesionWeight = 0.2;

To calculate the swarm movement, you usually update the swarm a few times a second in a loop. The swarm does not automatically move the boids, it just updates the velocity and acceleration for each boid. The position has to be updated by you.

// your tick/update loop
while (true) {
  swarm.Update(deltaSeconds);
  
  // each boid now has an updated velocity
  for (auto& boid : boids) {
    // update position
    boid.Position += boid.Velocity * deltaSeconds;
    
    // draw boid
  }
}

If you do not want the swarm to even update the boids velocity automatically (because you want to debug the result or you want to scale the acceleration per boid), then you can just call UpdateAcceleration() instead of the Update(deltaSeconds) on the swarm.

You can of course adjust the swarm parameters as well as the boid list during the loop to change the swarm behaviour over time. The library is, however, not thread-safe (so please no changes during the swarm update)!

That's basically it, have fun! :)

swarmz's People

Contributors

cultrarius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

swarmz's Issues

Building with Visual Studio 2016 and MinGW (MinGW Builds gcc 6.1.0)

It is interesting! Thanks! :-)

Unfortunately I am unable to build the library with Visual Studio 2015.

1>------ Build started: Project: enjambre_msvc2015, Configuration: Debug x64 ------
1>  Principal.cpp
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(121): warning C4305: 'argument': truncation from 'double' to 'float'
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(194): warning C4305: 'initializing': truncation from 'double' to 'float'
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(262): warning C4244: 'argument': conversion from 'unsigned __int64' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(265): warning C4244: 'argument': conversion from 'unsigned __int64' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(268): warning C4244: 'argument': conversion from 'unsigned __int64' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(337): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(338): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(339): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>  Generating Code...
1>  Compiling...
1>  main.cpp
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(121): warning C4305: 'argument': truncation from 'double' to 'float'
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(194): warning C4305: 'initializing': truncation from 'double' to 'float'
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(262): warning C4244: 'argument': conversion from 'unsigned __int64' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(265): warning C4244: 'argument': conversion from 'unsigned __int64' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(268): warning C4244: 'argument': conversion from 'unsigned __int64' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(337): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(338): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>p:\mis-proyectos\personal\enjambre\inc\swarmz\swarmz.h(339): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>  Generating Code...
1>Principal.obj : error LNK2005: "bool __cdecl std::operator==(struct sw::Vec3 const &,struct sw::Vec3 const &)" (??8std@@YA_NAEBUVec3@sw@@0@Z) already defined in main.obj
1>Principal.obj : error LNK2005: "float __cdecl sw::TransformDistance(float,enum sw::DistanceType)" (?TransformDistance@sw@@YAMMW4DistanceType@1@@Z) already defined in main.obj
1>P:\Mis-Proyectos\Personal\Enjambre\prj\enjambre_msvc2015\x64\Debug\enjambre_msvc2015.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Neither I can build with MinGW Builds GCC 6,1,0
-------------- Build: Debug in enjambre_cdbmingw (compiler: MinGW GNU GCC Compiler x64)---------------

g++.exe  -o bin\Debug\enjambre_cdbmingw.exe obj\Debug\src\main.o obj\Debug\src\Principal.o   
obj\Debug\src\Principal.o: In function `__gnu_cxx::__promote_2<float, int, __gnu_cxx::__promote<float, std::__is_integer<float>::__value>::__type, __gnu_cxx::__promote<int, std::__is_integer<int>::__value>::__type>::__type std::pow<float, int>(float, int)':
p:\Mis-Proyectos\Personal\Enjambre\prj\enjambre_cdbmingw/../../inc/Swarmz/swarmz.h:130: multiple definition of `sw::TransformDistance(float, sw::DistanceType)'
obj\Debug\src\main.o:p:\Mis-Proyectos\Personal\Enjambre\prj\enjambre_cdbmingw/../../inc/Swarmz/swarmz.h:130: first defined here
obj\Debug\src\Principal.o: In function `std::operator==(sw::Vec3 const&, sw::Vec3 const&)':
p:\Mis-Proyectos\Personal\Enjambre\prj\enjambre_cdbmingw/../../inc/Swarmz/swarmz.h:163: multiple definition of `std::operator==(sw::Vec3 const&, sw::Vec3 const&)'
obj\Debug\src\main.o:p:\Mis-Proyectos\Personal\Enjambre\prj\enjambre_cdbmingw/../../inc/Swarmz/swarmz.h:163: first defined here
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
5 error(s), 0 warning(s) (0 minute(s), 0 second(s))

The problem seems to be the same (multiple definition) . Any idea? ;-)

DJuego

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.