Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:58:57+00:00 2026-06-04T20:58:57+00:00

I have a particle system that as usually creates new particles, updates them and

  • 0

I have a particle system that as usually creates new particles, updates them and destroys…

In the emitter module there is for loop that resets particles:

foreach p in particles
    p.position = rand()
    p.velocity = rand()

usually when using C’s rand() function we get uniform distibution, but what when I would like to use some other distribution (gaussian for instance) ?

how to change that code so that it will handle several (or at least two) different ways of generating new particles’ parameters?

Of course you can create some object: like RandomGenerator, and use some virtual function calls and handle those different behaviours. But this piece of code should be very fast (when updating thousands of particles), so using virtual functions is not good I think.

or maybe I should not care and simply write:

foreach p in particles
    p.position = useGaussian ? gausRand() : UniRand()
    p.velocity = useGaussian ? gausRand() : UniRand()

we can narrow number of different distribution and use only two or three of them…

please notice that my example is very simple, but in real code you have several particle parametrs configurations.

I would like to get some general advice on that subject.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:58:59+00:00Added an answer on June 4, 2026 at 8:58 pm

    While @gavinb’s answer is a perfectly valid way to go, I’d suggest to avoid reinventing the wheel and go with the standard facilities: if you have c++11 support, use std::normal_distribution and its relatives (see, e.g. C++ TR1: how to use the normal_distribution?). Otherwise, use boost::random library.

    Since these are header-only (the boost version at least), there are no polymorphic calls involved, so you don’t have to worry about them. Which, of course, does not obviate the utmost relevance of @Oli Charlesworth’s advice.

    EDIT: if the overhead due to polymorphic calls is non-negligible, you can always template your functions on an enumerated type of distributions and specialize them as necessary.

    In a nutshell, it’s as simple as this:

    #include<iostream>
    
    // template on an int selector
    template<int N> void foo(){ std::cout<<"42\n"; }
    template<> void foo<1>() {std::cout<<"1\n";}
    
    //now use an enum
    enum  distr_types {UNIF, NORMAL, UNKNOWN}; 
    template<distr_types T> void bar() {std::cout<<"fourty two\n";}
    template<> void bar<UNIF>() {std::cout<<"UNIF\n";}
    template<> void bar<NORMAL>(){std::cout<<"NORMAL\n";}
    
    int main(){
      foo<3>();
      foo<1>();
    
      bar<UNIF>();
      bar<NORMAL>();
      bar<UNKNOWN>();
    }
    

    But if you find yourself doing things of this sort, it’s worth having a look at one of good C++ books.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a system that generates particles from sources and updates their positions. Currently,
If you have a script that creates a particle system and then delete the
I have a particle system using basic spritebatch, where the particles are being created
I have a particle system with X particles. Each particle tests for collision with
I need to create a particle system and retain the created particles. I need
im developing a very simple particle system in AS3, i have the particle (a
I am attempting to write a simple particle system that leverages CUDA to do
I have a particle system where the positions and various properties are stored in
I have recently used Visual Basic .Net to write a particle system which emits
In cocos2D I currently have a very simple particle emitter initialized like this: turnEmitter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.