Tutorial 7 - Magnets

Top  Previous  Next

The Default Particle Systems contain Magnets, where Magnets are objects that can attract and repel particles which are within a specified distance of the Magnet.  In this tutorial we will look at the Magnets provided by the Default Particle Systems.

 

Load the Tutorial 7 project and open up MyParticleSystem.cs.

 

 

Types of Magnets

 

The Default Particle System classes provide 4 types of Magnets, which are the MagnetPoint, MagnetLine, MagnetLineSegment, and MagnetPlane classes.

 

Point - A single point in 3D space that attracts or repels particles.

 

Line - An infinite line in 3D space that attracts or repels particles.

 

Line Segment - A line with 2 distinct ends in 3D space that attracts or repels particles.

 

Plane - An infinite 2D plane in 3D space that attracts or repels particles.

 

All 4 of the provided Magnet classes inherit from the DefaultParticleSystemMagnet abstract class.  This allows you to create your own type of magnet class as long as it too inherits from DefaultParticleSystemMagnet.  For example, you may want to create a Plane Segment magnet, a Multi-Line Segment magnet, or a Spiral Line magnet; good luck with the math ;).

 

The code for the 4 provided Magnet classes is provided in the DPSF install directory (i.e. C:\DPSF\Templates\DPSF Defaults), and the code that defines how they affect a particle is in the CalculateForceMagnetShouldExertOnParticle() function of the DefaultBase.cs file (also in the DPSF Defaults folder).

 

TRY IT OUT: To see the different type of effect that each type of Magnet creates, run Tutorial 7.  Use the C, V, B, and N keys to toggle between the different Magnet types.  You will want to rotate the camera a little bit in order to see the difference between the Line and Plane Magnets.  Also, move the Emitter around to see that only particles that are within a certain distance of the Magnet are affected by it.

 

 

Magnet Modes

 

The 4 provided Magnets support 2 modes:

 

Attract - Particles are attracted to the magnet.

 

Repel - Particles are repelled away from the magnet.

 

TRY IT OUT: When running Tutorial 7, use the H key to toggle between having the current Magnet attract or repel the particles, and then move the Emitter around.

 

 

Distance Functions

 

Each Magnet has a MinDistance and MaxDistance property; only particles whose distance from the Magnet falls within this range will be affected by the Magnet.  Each Magnet also has a MaxForce property that helps determine how much a Magnet will affect a particle;  The other determining factor is the Magnet's DistanceFunction.  The DistanceFuction is used to determine how much affect a Magnet should have on a particle based on the Particle's distance away from the Magnet.  For example, should a Magnet have the same amount of pull on a particle that is 5 units away as a particle that is 50 units away, or should it have more of a pull on the closer particle.  The DistanceFunctions are used to specify this relationship.  There are 7 Distance Functions to choose from:

 

Constant - The Max Force will be applied to the particle regardless of how far away it is from the Magnet.

 

Linear - When the Particle is at the Min Distance from the Magnet no force will be applied, when the Particle is at half of the Max Distance from the Magnet 1/2 of the Max Force will be applied, and when the Particle is at the Max Distance from the Magnet the Max Force will be applied to the Particle.

 

LinearInverse - When the Particle is at the Min Distance from the Magnet the Max Force will be applied, when the Particle is at half of the Max Distance from the Magnet 1/2 of the Max Force will be applied, and when the Particle is at the Max Distance from the Magnet no force will be applied to the Particle.

 

Squared - When the Particle is at the Min Distance from the Magnet no force will be applied, when the Particle is at half of the Max Distance from the Magnet 1/4 of the Max Force will be applied, and when the Particle is at the Max Distance from the Magnet the Max Force will be applied to the Particle.

 

SquaredInverse - When the Particle is at the Min Distance from the Magnet the Max Force will be applied, when the Particle is at half of the Max Distance from the Magnet 1/4 of the Max Force will be applied, and when the Particle is at the Max Distance from the Magnet no force will be applied to the Particle.

 

Cubed - When the Particle is at the Min Distance from the Magnet no force will be applied, when the Particle is at half of the Max Distance from the Magnet 1/8 of the Max Force will be applied, and when the Particle is at the Max Distance from the Magnet the Max Force will be applied to the Particle.

 

CubedInverse - When the Particle is at the Min Distance from the Magnet the Max Force will be applied, when the Particle is at half of the Max Distance from the Magnet 1/8 of the Max Force will be applied, and when the Particle is at the Max Distance from the Magnet no force will be applied to the Particle.

 

The Inverse functions are the ones that more accurately model how you would expect a magnet to behave in real life, where the closer a particle gets to the Magnet, the more force the Magnet exerts on the particle.

 

TRY IT OUT: When running Tutorial 7, press the M key to toggle between the different Distance Functions.  Move the Emitter around and also experiment with switching between Attract and Repel modes.

 

 

Affecting Position vs. Velocity

 

Any Magnets that you create will not have an effect unless you use a Particle Update Function to specify how the Magnets should affect the particles.  The Default Particle Systems come with two different Particle Update Functions:

 

UpdateParticlePositionAccordingToMagnets - The Magnets affect the particle's position, but do not affect its velocity.  This results in the particles' movement being obviously affected while within the Magnet's range, but suddenly to stop being affected as soon as it is out of the Magnet's range.  This is especially apparent when the Magnet is set to repel particles, and may seem unnatural.

 

UpdateParticleVelocityAccordingToMagnets - The Magnets affect the particle's velocity, which in turn affect the particle's position.  This results in a smooth transition of the particles' movement when it goes within and out of the Magnets range.

 

As mentioned above, the code for these Particle Update functions is shown in the DefaultBase.cs file.  In addition to using these 2 Particle Update functions, you can also write your own.  Maybe you want to create a magnet that affects a particle's color instead of its position; you could write your own UpdateParticleColorAccordingToMagnets Particle Update function.

 

TRY IT OUT: When running Tutorial 7, press the P key to toggle between having the Magnet affect the particles' Position and Velocity.  Move the Emitter around, switch between Attract and Repel modes, and try out the different Distance Functions in each of these modes.

 

 

Show Me The Code

 

The Default Particle Systems come with a MagnetList variable that is meant to hold all of the Magnets that will affect the particle system's particles.  In this tutorial we only ever have one magnet active at a time, but you can have as many Magnets as you like.  MyParticleSystem.cs shows how to initialize each type of Magnet in the UseMagnetType() function, but for simplicity here I will show the code that you would simply need to add to your particle system's AutoInitialize() function.

 

The first thing you need to do is create a magnet.  The code below shows how to create a new Point magnet at position (0, 50, 0), that Attracts particles, uses the LinearInverse distance function (so the closer a particle is, the more force will be exerted on it), affects all particles within a distance of 10 to 100 units of the magnet, and exerts at most a force of 200 on the particles.

 

       MagnetPoint pointMagnet = new MagnetPoint(new Vector3(0, 50, 0), DefaultParticleSystemMagnet.MagnetModes.Attract,

               DefaultParticleSystemMagnet.DistanceFunctions.LinearInverse, 10, 100, 200, 0);

 

Then add your new Magnet to the particle system's MagnetList:

 

       MagnetList.Add(pointMagnet);

 

And finally, to have the Magnet affect the particle system's particles, make sure you have an EveryTimeEvent setup:

 

   ParticleEvents.AddEveryTimeEvent(UpdateParticlePositionAccordingToMagnets);

 

 

And that's it; With 3 lines of code you can have one of the built-in Magnets affecting your particle system's particles (technically this could be done it 2 lines of code by creating the new Magnet directly in the MagnetList.Add() function.

 

 

Remarks

 

Play around with the Tutorial 7 application.  Use the onscreen keys to change between the different types of Magnets and Distance Functions to see how they differ.  You'll likely notice that some combinations of settings cause the particles to seem to freak out and jump all over the place.  Change the source code to change the Min and Max distance that the magnets affect particles at.  Try changing the source code to have 2 Magnets active at once.