- Home /
Creating fractals images with particles, but getting weird image
I'm trying to create fractal images with the following equation :
And it should look something like this (but in 3D) :
But I'm getting a very strange output. See the green particles are the 40 particles that are emitted. Even trying with 4000 or 400000 particles, it still forms a very similar shape:
This is my code:
#pragma strict
function Start () {
var myParticleSystem : ParticleSystem;
var myParticles: ParticleSystem.Particle[];
myParticleSystem = GetComponent( ParticleSystem );
myParticleSystem.Emit(40);
var num_particles = myParticleSystem.particleCount;
myParticles = new ParticleSystem.Particle[num_particles+1];
var a = -55;
var b = -1 ;
var c = -42;
var x : float;
var y : float;
var z : float;
var particlePosition : Vector3;
for (var i = 0; i < num_particles; i++){ // Generalization of Barry Martin's original
particlePosition = myParticles[i].position;
x = Random.value * -1 ;
y = Random.value * 10;
// HERE I FOLLOW THE EQUATION EXACTLY HOW IT'S GIVEN
//
var x1 = y- x / Mathf.Abs(x) * Mathf.Sqrt(Mathf.Abs(b * x + c));
var y1 = a - x;
var newposition = new Vector3(x1,y1,Random.value);
myParticleSystem.GetParticles(myParticles);
myParticles[i].position = newposition;
myParticleSystem.SetParticles(myParticles, num_particles);
}
}
It looks to me like that equation is calculating the position of the next particle based on the current particles location. Xi
is the current x position and Xi+1
is the next x position (I could be wrong).
Why are you assigning random values to x and y? That won't give you any kind of fractal structure I believe.. Try storing the previous x and y positions while going through the for loop.
Your code comment states //HERE I FOLLOW THE EQUATION EXACTLY AS GIVEN yet, to me, the code looks different to the formula given at the top of your post?
In the equation, {x,y}[i+1] is defined in relation to {x,y}[i] yet, in your implementation you've substituted random values in place of x[i] and y[i] (and also provided a random z[i])..?
What is the particlePosition variable used for? And where do the a,b,c values come from?
Answer by ByteSheep · May 29, 2015 at 06:32 PM
Gave it a test and with some tweaks it looks quite nice:
#pragma strict
var particleCount : int;
var a : int = 1;
var b : int = 1;
var c : int = 0;
function Start ()
{
var myParticleSystem : ParticleSystem;
var myParticles: ParticleSystem.Particle[];
myParticleSystem = GetComponent(ParticleSystem);
myParticleSystem.Emit(particleCount);
myParticles = new ParticleSystem.Particle[particleCount + 1];
var x : float;
var y : float;
var prevPos : Vector3;
for (var i = 0; i < particleCount; i++)
{
// Get previous particle position
prevPos = (i < 1) ? Vector3.one : myParticles[i - 1].position;
x = prevPos.x;
y = prevPos.y;
var x1 = y - x / Mathf.Abs(x) * Mathf.Sqrt(Mathf.Abs(b * x + c));
var y1 = a - x;
var newPosition = new Vector3(x1, y1, 0);
myParticleSystem.GetParticles(myParticles);
myParticles[i].position = newPosition;
myParticleSystem.SetParticles(myParticles, particleCount);
}
}
Yay fractals!
Thanks! I did something similar, and it fixed the result. In your code, though, you didn't specify what particleCount was. I tweaked yours so that particleCount = 400; and only one particle was emitted.... How did you get yours to work? Did you have emission enabled for the ParticleSystem in Inspector?
particleCount is just the amount of particles you would like to spawn. $$anonymous$$ake sure that in the inspector none of the values are 0 (the c variable is 0 by default) otherwise all the particles will be spawned on the same spot.
The emission rate of the particle system is set to 0.
Oh I see! Thanks very much! A design question: If I wanted to have say seven of these fractals flow at the camera at a time, one after another, would it be best if I made: a) seven different particle systems in the Hierarchy, and start at increasing delays? b) created a prefab out of this one here, and then in the update script instantiate 6 more? or is there a better option? Both of these options seem rather loopy
Both of those options sound like viable approaches. Depending on the effect you are trying to get you could also put the above code in a separate function so you can easily create new fractals from the same particle system. You would simply clear the particle list and then call the function again to generate new particles / fractals.
But if you need the fractals to move independently of one another it would probably be easiest to have a separate particle emitter for each.
Sounds good. Thanks again. Also, if I'm trying to have the ParticleSystem loop, I have myParticleSystem.loop = true; but it only runs once still. How can I enable infinite loops? EDIT: Just read about FixedUpdate. Woo!
Your answer
Follow this Question
Related Questions
Trying to create fractals images with particles, but getting weird pattern instead 0 Answers
Camera.Render() & DockArea:OnGUI() seemingly random & unrelated errors after particles launch 0 Answers
Need tips for optimizing main func that loops through thousands of particles 0 Answers
particles emitter loop 1 Answer
Custom particle system loop optimization 0 Answers