Jump to content
  • Advertisement
Sign in to follow this  

Adding a World Matrix

This topic is 2111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys


Ive been experimenting with the particles3D sample(link below) from the xbox live website

and came across the method that does the view and projection,but the world matrix wasnt

included with it, and I wanted to ask how would I go about adding it to the method(code below)







particles3D sample





code wanting to modify

 /// <summary>
        /// Sets the camera view and projection matrices
        /// that will be used to draw this particle system.
        /// </summary>
        public void SetCamera(Matrix view, Matrix projection)

to something like this

 /// <summary>
        /// Sets the camera view and projection matrices
        /// that will be used to draw this particle system.
        /// </summary>
        public void SetCamera(Matrix view, Matrix projection, Matrix world)

particle system class code

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Particle3DSample
    /// <summary>
    /// The main component in charge of displaying particles.
    /// </summary>
    public abstract class ParticleSystem : DrawableGameComponent
        #region Fields

        // Settings class controls the appearance and animation of this particle system.
        ParticleSettings settings = new ParticleSettings();

        // For loading the effect and particle texture.
        ContentManager content;

        // Custom effect for drawing point sprite particles. This computes the particle
        // animation entirely in the vertex shader: no per-particle CPU work required!
        Effect particleEffect;

        // Shortcuts for accessing frequently changed effect parameters.
        EffectParameter effectViewParameter;
        EffectParameter effectProjectionParameter;
        EffectParameter effectViewportHeightParameter;
        EffectParameter effectTimeParameter;

        // An array of particles, treated as a circular queue.
        ParticleVertex[] particles;

        // A vertex buffer holding our particles. This contains the same data as
        // the particles array, but copied across to where the GPU can access it.
        DynamicVertexBuffer vertexBuffer;

        // Vertex declaration describes the format of our ParticleVertex structure.
        VertexDeclaration vertexDeclaration;

        // The particles array and vertex buffer are treated as a circular queue.
        // Initially, the entire contents of the array are free, because no particles
        // are in use. When a new particle is created, this is allocated from the
        // beginning of the array. If more than one particle is created, these will
        // always be stored in a consecutive block of array elements. Because all
        // particles last for the same amount of time, old particles will always be
        // removed in order from the start of this active particle region, so the
        // active and free regions will never be intermingled. Because the queue is
        // circular, there can be times when the active particle region wraps from the
        // end of the array back to the start. The queue uses modulo arithmetic to
        // handle these cases. For instance with a four entry queue we could have:
        //      0
        //      1 - first active particle
        //      2 
        //      3 - first free particle
        // In this case, particles 1 and 2 are active, while 3 and 4 are free.
        // Using modulo arithmetic we could also have:
        //      0
        //      1 - first free particle
        //      2 
        //      3 - first active particle
        // Here, 3 and 0 are active, while 1 and 2 are free.
        // But wait! The full story is even more complex.
        // When we create a new particle, we add them to our managed particles array.
        // We also need to copy this new data into the GPU vertex buffer, but we don't
        // want to do that straight away, because setting new data into a vertex buffer
        // can be an expensive operation. If we are going to be adding several particles
        // in a single frame, it is faster to initially just store them in our managed
        // array, and then later upload them all to the GPU in one single call. So our
        // queue also needs a region for storing new particles that have been added to
        // the managed array but not yet uploaded to the vertex buffer.
        // Another issue occurs when old particles are retired. The CPU and GPU run
        // asynchronously, so the GPU will often still be busy drawing the previous
        // frame while the CPU is working on the next frame. This can cause a
        // synchronization problem if an old particle is retired, and then immediately
        // overwritten by a new one, because the CPU might try to change the contents
        // of the vertex buffer while the GPU is still busy drawing the old data from
        // it. Normally the graphics driver will take care of this by waiting until
        // the GPU has finished drawing inside the VertexBuffer.SetData call, but we
        // don't want to waste time waiting around every time we try to add a new
        // particle! To avoid this delay, we can specify the SetDataOptions.NoOverwrite
        // flag when we write to the vertex buffer. This basically means "I promise I
        // will never try to overwrite any data that the GPU might still be using, so
        // you can just go ahead and update the buffer straight away". To keep this
        // promise, we must avoid reusing vertices immediately after they are drawn.
        // So in total, our queue contains four different regions:
        // Vertices between firstActiveParticle and firstNewParticle are actively
        // being drawn, and exist in both the managed particles array and the GPU
        // vertex buffer.
        // Vertices between firstNewParticle and firstFreeParticle are newly created,
        // and exist only in the managed particles array. These need to be uploaded
        // to the GPU at the start of the next draw call.
        // Vertices between firstFreeParticle and firstRetiredParticle are free and
        // waiting to be allocated.
        // Vertices between firstRetiredParticle and firstActiveParticle are no longer
        // being drawn, but were drawn recently enough that the GPU could still be
        // using them. These need to be kept around for a few more frames before they
        // can be reallocated.

        int firstActiveParticle;
        int firstNewParticle;
        int firstFreeParticle;
        int firstRetiredParticle;

        // Store the current time, in seconds.
        float currentTime;

        // Count how many times Draw has been called. This is used to know
        // when it is safe to retire old particles back into the free list.
        int drawCounter;

        // Shared random number generator.
        static Random random = new Random();


        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        protected ParticleSystem(Game game, ContentManager content)
            : base(game)
            this.content = content;

        /// <summary>
        /// Initializes the component.
        /// </summary>
        public override void Initialize()

            particles = new ParticleVertex[settings.MaxParticles];


        /// <summary>
        /// Derived particle system classes should override this method
        /// and use it to initalize their tweakable settings.
        /// </summary>
        protected abstract void InitializeSettings(ParticleSettings settings);

        /// <summary>
        /// Loads graphics for the particle system.
        /// </summary>
        protected override void LoadContent()

            vertexDeclaration = new VertexDeclaration(GraphicsDevice,

            // Create a dynamic vertex buffer.
            int size = ParticleVertex.SizeInBytes * particles.Length;

            vertexBuffer = new DynamicVertexBuffer(GraphicsDevice, size, 
                                                   BufferUsage.WriteOnly |

        /// <summary>
        /// Helper for loading and initializing the particle effect.
        /// </summary>
        void LoadParticleEffect()
            Effect effect = content.Load<Effect>("ParticleEffect");

            // If we have several particle systems, the content manager will return
            // a single shared effect instance to them all. But we want to preconfigure
            // the effect with parameters that are specific to this particular
            // particle system. By cloning the effect, we prevent one particle system
            // from stomping over the parameter settings of another.
            particleEffect = effect.Clone(GraphicsDevice);

            EffectParameterCollection parameters = particleEffect.Parameters;

            // Look up shortcuts for parameters that change every frame.
            effectViewParameter = parameters["View"];
            effectProjectionParameter = parameters["Projection"];
            effectViewportHeightParameter = parameters["ViewportHeight"];
            effectTimeParameter = parameters["CurrentTime"];

            // Set the values of parameters that do not change.

                new Vector2(settings.MinRotateSpeed, settings.MaxRotateSpeed));
                new Vector2(settings.MinStartSize, settings.MaxStartSize));
                new Vector2(settings.MinEndSize, settings.MaxEndSize));

            // Load the particle texture, and set it onto the effect.
            Texture2D texture = content.Load<Texture2D>(settings.TextureName);


            // Choose the appropriate effect technique. If these particles will never
            // rotate, we can use a simpler pixel shader that requires less GPU power.
            string techniqueName;

            if ((settings.MinRotateSpeed == 0) && (settings.MaxRotateSpeed == 0))
                techniqueName = "NonRotatingParticles";
                techniqueName = "RotatingParticles";

            particleEffect.CurrentTechnique = particleEffect.Techniques[techniqueName];


        #region Update and Draw

        /// <summary>
        /// Updates the particle system.
        /// </summary>
        public override void Update(GameTime gameTime)
            if (gameTime == null)
                throw new ArgumentNullException("gameTime");

            currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds;


            // If we let our timer go on increasing for ever, it would eventually
            // run out of floating point precision, at which point the particles
            // would render incorrectly. An easy way to prevent this is to notice
            // that the time value doesn't matter when no particles are being drawn,
            // so we can reset it back to zero any time the active queue is empty.

            if (firstActiveParticle == firstFreeParticle)
                currentTime = 0;

            if (firstRetiredParticle == firstActiveParticle)
                drawCounter = 0;

        /// <summary>
        /// Helper for checking when active particles have reached the end of
        /// their life. It moves old particles from the active area of the queue
        /// to the retired section.
        /// </summary>
        void RetireActiveParticles()
            float particleDuration = (float)settings.Duration.TotalSeconds;

            while (firstActiveParticle != firstNewParticle)
                // Is this particle old enough to retire?
                float particleAge = currentTime - particles[firstActiveParticle].Time;

                if (particleAge < particleDuration)

                // Remember the time at which we retired this particle.
                particles[firstActiveParticle].Time = drawCounter;

                // Move the particle from the active to the retired queue.

                if (firstActiveParticle >= particles.Length)
                    firstActiveParticle = 0;

        /// <summary>
        /// Helper for checking when retired particles have been kept around long
        /// enough that we can be sure the GPU is no longer using them. It moves
        /// old particles from the retired area of the queue to the free section.
        /// </summary>
        void FreeRetiredParticles()
            while (firstRetiredParticle != firstActiveParticle)
                // Has this particle been unused long enough that
                // the GPU is sure to be finished with it?
                int age = drawCounter - (int)particles[firstRetiredParticle].Time;

                // The GPU is never supposed to get more than 2 frames behind the CPU.
                // We add 1 to that, just to be safe in case of buggy drivers that
                // might bend the rules and let the GPU get further behind.
                if (age < 3)

                // Move the particle from the retired to the free queue.

                if (firstRetiredParticle >= particles.Length)
                    firstRetiredParticle = 0;

        /// <summary>
        /// Draws the particle system.
        /// </summary>
        public override void Draw(GameTime gameTime)
            GraphicsDevice device = GraphicsDevice;

            // Restore the vertex buffer contents if the graphics device was lost.
            if (vertexBuffer.IsContentLost)

            // If there are any particles waiting in the newly added queue,
            // we'd better upload them to the GPU ready for drawing.
            if (firstNewParticle != firstFreeParticle)

            // If there are any active particles, draw them now!
            if (firstActiveParticle != firstFreeParticle)

                // Set an effect parameter describing the viewport size. This is needed
                // to convert particle sizes into screen space point sprite sizes.

                // Set an effect parameter describing the current time. All the vertex
                // shader particle animation is keyed off this value.

                // Set the particle vertex buffer and vertex declaration.
                device.Vertices[0].SetSource(vertexBuffer, 0,

                device.VertexDeclaration = vertexDeclaration;

                // Activate the particle effect.

                foreach (EffectPass pass in particleEffect.CurrentTechnique.Passes)

                    if (firstActiveParticle < firstFreeParticle)
                        // If the active particles are all in one consecutive range,
                        // we can draw them all in a single call.
                                              firstFreeParticle - firstActiveParticle);
                        // If the active particle range wraps past the end of the queue
                        // back to the start, we must split them over two draw calls.
                                              particles.Length - firstActiveParticle);

                        if (firstFreeParticle > 0)



                // Reset a couple of the more unusual renderstates that we changed,
                // so as not to mess up any other subsequent drawing.
                device.RenderState.PointSpriteEnable = false;
                device.RenderState.DepthBufferWriteEnable = true;


        /// <summary>
        /// Helper for uploading new particles from our managed
        /// array to the GPU vertex buffer.
        /// </summary>
        void AddNewParticlesToVertexBuffer()
            int stride = ParticleVertex.SizeInBytes;

            if (firstNewParticle < firstFreeParticle)
                // If the new particles are all in one consecutive range,
                // we can upload them all in a single call.
                vertexBuffer.SetData(firstNewParticle * stride, particles,
                                     firstFreeParticle - firstNewParticle,
                                     stride, SetDataOptions.NoOverwrite);
                // If the new particle range wraps past the end of the queue
                // back to the start, we must split them over two upload calls.
                vertexBuffer.SetData(firstNewParticle * stride, particles,
                                     particles.Length - firstNewParticle,
                                     stride, SetDataOptions.NoOverwrite);

                if (firstFreeParticle > 0)
                    vertexBuffer.SetData(0, particles,
                                         0, firstFreeParticle,
                                         stride, SetDataOptions.NoOverwrite);

            // Move the particles we just uploaded from the new to the active queue.
            firstNewParticle = firstFreeParticle;

        /// <summary>
        /// Helper for setting the renderstates used to draw particles.
        /// </summary>
        void SetParticleRenderStates(RenderState renderState)
            // Enable point sprites.
            renderState.PointSpriteEnable = true;
            renderState.PointSizeMax = 256;

            // Set the alpha blend mode.
            renderState.AlphaBlendEnable = true;
            renderState.AlphaBlendOperation = BlendFunction.Add;
            renderState.SourceBlend = settings.SourceBlend;
            renderState.DestinationBlend = settings.DestinationBlend;

            // Set the alpha test mode.
            renderState.AlphaTestEnable = true;
            renderState.AlphaFunction = CompareFunction.Greater;
            renderState.ReferenceAlpha = 0;

            // Enable the depth buffer (so particles will not be visible through
            // solid objects like the ground plane), but disable depth writes
            // (so particles will not obscure other particles).
            renderState.DepthBufferEnable = true;
            renderState.DepthBufferWriteEnable = false;


        #region Public Methods

        /// <summary>
        /// Sets the camera view and projection matrices
        /// that will be used to draw this particle system.
        /// </summary>
        public void SetCamera(Matrix view, Matrix projection)

        /// <summary>
        /// Adds a new particle to the system.
        /// </summary>
        public void AddParticle(Vector3 position, Vector3 velocity)
            // Figure out where in the circular queue to allocate the new particle.
            int nextFreeParticle = firstFreeParticle + 1;

            if (nextFreeParticle >= particles.Length)
                nextFreeParticle = 0;

            // If there are no free particles, we just have to give up.
            if (nextFreeParticle == firstRetiredParticle)

            // Adjust the input velocity based on how much
            // this particle system wants to be affected by it.
            velocity *= settings.EmitterVelocitySensitivity;

            // Add in some random amount of horizontal velocity.
            float horizontalVelocity = MathHelper.Lerp(settings.MinHorizontalVelocity,

            double horizontalAngle = random.NextDouble() * MathHelper.TwoPi;

            velocity.X += horizontalVelocity * (float)Math.Cos(horizontalAngle);
            velocity.Z += horizontalVelocity * (float)Math.Sin(horizontalAngle);

            // Add in some random amount of vertical velocity.
            velocity.Y += MathHelper.Lerp(settings.MinVerticalVelocity,

            // Choose four random control values. These will be used by the vertex
            // shader to give each particle a different size, rotation, and color.
            Color randomValues = new Color((byte)random.Next(255),

            // Fill in the particle vertex structure.
            particles[firstFreeParticle].Position = position;
            particles[firstFreeParticle].Velocity = velocity;
            particles[firstFreeParticle].Random = randomValues;
            particles[firstFreeParticle].Time = currentTime;

            firstFreeParticle = nextFreeParticle;


HLSL code

// Camera parameters.
float4x4 View;
float4x4 Projection;
float ViewportHeight;

// The current time, in seconds.
float CurrentTime;

// Parameters describing how the particles animate.
float Duration;
float DurationRandomness;
float3 Gravity;
float EndVelocity;
float4 MinColor;
float4 MaxColor;

// These float2 parameters describe the min and max of a range.
// The actual value is chosen differently for each particle,
// interpolating between x and y by some random amount.
float2 RotateSpeed;
float2 StartSize;
float2 EndSize;

// Particle texture and sampler.
texture Texture;

sampler Sampler = sampler_state
    Texture = (Texture);
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Point;
    AddressU = Clamp;
    AddressV = Clamp;

// Vertex shader input structure describes the start position and
// velocity of the particle, and the time at which it was created,
// along with some random values that affect its size and rotation.
struct VertexShaderInput
    float3 Position : POSITION0;
    float3 Velocity : NORMAL0;
    float4 Random : COLOR0;
    float Time : TEXCOORD0;

// Vertex shader output structure specifies the position, size, and
// color of the particle, plus a 2x2 rotation matrix (packed into
// a float4 value because we don't have enough color interpolators
// to send this directly as a float2x2).
struct VertexShaderOutput
    float4 Position : POSITION0;
    float Size : PSIZE0;
    float4 Color : COLOR0;
    float4 Rotation : COLOR1;

// Vertex shader helper for computing the position of a particle.
float4 ComputeParticlePosition(float3 position, float3 velocity,
                               float age, float normalizedAge)
    float startVelocity = length(velocity);

    // Work out how fast the particle should be moving at the end of its life,
    // by applying a constant scaling factor to its starting velocity.
    float endVelocity = startVelocity * EndVelocity;
    // Our particles have constant acceleration, so given a starting velocity
    // S and ending velocity E, at time T their velocity should be S + (E-S)*T.
    // The particle position is the sum of this velocity over the range 0 to T.
    // To compute the position directly, we must integrate the velocity
    // equation. Integrating S + (E-S)*T for T produces S*T + (E-S)*T*T/2.

    float velocityIntegral = startVelocity * normalizedAge +
                             (endVelocity - startVelocity) * normalizedAge *
                                                             normalizedAge / 2;
    position += normalize(velocity) * velocityIntegral * Duration;
    // Apply the gravitational force.
    position += Gravity * age * normalizedAge;
    // Apply the camera view and projection transforms.
    return mul(mul(float4(position, 1), View), Projection);

// Vertex shader helper for computing the size of a particle.
float ComputeParticleSize(float4 projectedPosition,
                          float randomValue, float normalizedAge)
    // Apply a random factor to make each particle a slightly different size.
    float startSize = lerp(StartSize.x, StartSize.y, randomValue);
    float endSize = lerp(EndSize.x, EndSize.y, randomValue);
    // Compute the actual size based on the age of the particle.
    float size = lerp(startSize, endSize, normalizedAge);
    // Project the size into screen coordinates.
    return size * Projection._m11 / projectedPosition.w * ViewportHeight / 2;

// Vertex shader helper for computing the color of a particle.
float4 ComputeParticleColor(float4 projectedPosition,
                            float randomValue, float normalizedAge)
    // Apply a random factor to make each particle a slightly different color.
    float4 color = lerp(MinColor, MaxColor, randomValue);
    // Fade the alpha based on the age of the particle. This curve is hard coded
    // to make the particle fade in fairly quickly, then fade out more slowly:
    // plot x*(1-x)*(1-x) for x=0:1 in a graphing program if you want to see what
    // this looks like. The 6.7 scaling factor normalizes the curve so the alpha
    // will reach all the way up to fully solid.
    color.a *= normalizedAge * (1-normalizedAge) * (1-normalizedAge) * 6.7;
    return color;

// Vertex shader helper for computing the rotation of a particle.
float4 ComputeParticleRotation(float randomValue, float age)
    // Apply a random factor to make each particle rotate at a different speed.
    float rotateSpeed = lerp(RotateSpeed.x, RotateSpeed.y, randomValue);
    float rotation = rotateSpeed * age;

    // Compute a 2x2 rotation matrix.
    float c = cos(rotation);
    float s = sin(rotation);
    float4 rotationMatrix = float4(c, -s, s, c);
    // Normally we would output this matrix using a texture coordinate interpolator,
    // but texture coordinates are generated directly by the hardware when drawing
    // point sprites. So we have to use a color interpolator instead. Only trouble
    // is, color interpolators are clamped to the range 0 to 1. Our rotation values
    // range from -1 to 1, so we have to scale them to avoid unwanted clamping.
    rotationMatrix *= 0.5;
    rotationMatrix += 0.5;
    return rotationMatrix;

// Custom vertex shader animates particles entirely on the GPU.
VertexShaderOutput VertexShader(VertexShaderInput input)
    VertexShaderOutput output;
    // Compute the age of the particle.
    float age = CurrentTime - input.Time;
    // Apply a random factor to make different particles age at different rates.
    age *= 1 + input.Random.x * DurationRandomness;
    // Normalize the age into the range zero to one.
    float normalizedAge = saturate(age / Duration);

    // Compute the particle position, size, color, and rotation.
    output.Position = ComputeParticlePosition(input.Position, input.Velocity,
                                              age, normalizedAge);
    output.Size = ComputeParticleSize(output.Position, input.Random.y, normalizedAge);
    output.Color = ComputeParticleColor(output.Position, input.Random.z, normalizedAge);
    output.Rotation = ComputeParticleRotation(input.Random.w, age);
    return output;

// Pixel shader input structure for particles that do not rotate.
struct NonRotatingPixelShaderInput
    float4 Color : COLOR0;
#ifdef XBOX
    float2 TextureCoordinate : SPRITETEXCOORD;
    float2 TextureCoordinate : TEXCOORD0;

// Pixel shader for drawing particles that do not rotate.
float4 NonRotatingPixelShader(NonRotatingPixelShaderInput input) : COLOR0
    return tex2D(Sampler, input.TextureCoordinate) * input.Color;

// Pixel shader input structure for particles that can rotate.
struct RotatingPixelShaderInput
    float4 Color : COLOR0;
    float4 Rotation : COLOR1;
#ifdef XBOX
    float2 TextureCoordinate : SPRITETEXCOORD;
    float2 TextureCoordinate : TEXCOORD0;

// Pixel shader for drawing particles that can rotate. It is not actually
// possible to rotate a point sprite, so instead we rotate our texture
// coordinates. Leaving the sprite the regular way up but rotating the
// texture has the exact same effect as if we were able to rotate the
// point sprite itself.
float4 RotatingPixelShader(RotatingPixelShaderInput input) : COLOR0
    float2 textureCoordinate = input.TextureCoordinate;

    // We want to rotate around the middle of the particle, not the origin,
    // so we offset the texture coordinate accordingly.
    textureCoordinate -= 0.5;
    // Apply the rotation matrix, after rescaling it back from the packed
    // color interpolator format into a full -1 to 1 range.
    float4 rotation = input.Rotation * 2 - 1;
    textureCoordinate = mul(textureCoordinate, float2x2(rotation));
    // Point sprites are squares. So are textures. When we rotate one square
    // inside another square, the corners of the texture will go past the
    // edge of the point sprite and get clipped. To avoid this, we scale
    // our texture coordinates to make sure the entire square can be rotated
    // inside the point sprite without any clipping.
    textureCoordinate *= sqrt(2);
    // Undo the offset used to control the rotation origin.
    textureCoordinate += 0.5;

    return tex2D(Sampler, textureCoordinate) * input.Color;

// Effect technique for drawing particles that do not rotate. Works with shader 1.1.
technique NonRotatingParticles
    pass P0
        VertexShader = compile vs_1_1 VertexShader();
        PixelShader = compile ps_1_1 NonRotatingPixelShader();

// Effect technique for drawing particles that can rotate. Requires shader 2.0.
technique RotatingParticles
    pass P0
        VertexShader = compile vs_1_1 VertexShader();
        PixelShader = compile ps_2_0 RotatingPixelShader();

Share this post

Link to post
Share on other sites

To start with, implementing the said world matrix (I assume it means the model-to-world matrix of the particle system?) in SetCamera is somewhat curious, because logically those matrix doesn't belong to the camera. There is already a "world" matrix for the camera, namely the inverse of the view matrix.


Technically, because the entire sequence of particle model's world matrix M, view matrix V, and projection matrix P is (using row vectors because being in D3D domain)

   M * V * P

one can see that the model matrix is just multiplied on the left of the view matrix. Hence, what you want to get can be done by giving a modified view matrix V'

   V' := M * V

when invoking effectViewParameter.SetValue(view). This can be done either by computing V' in advance and giving V' as "view" into SetCamera(...), or by using the approach mentioned in the OP and computing the product at first inside SetCamera(...).


However, the intention with particle systems is often to generate the particles already in world space. In such a case AddParticle(...) has to be altered so that the particles' initial position and velocity vectors are transformed (manually) before actually being added to the buffer. This may make things like collisions detection better integrated. Without such need, adapting the view matrix is certainly the easiest way of reaching the goal.

Edited by haegarr

Share this post

Link to post
Share on other sites

HI  haegarr


Thanks for responding, Ive also been experimenting with the Skinned Model Extensions Tutorial(link below) and wanted to attach

a particle to the bone of the model and thought that I had to add a world matrix in order to get it done Ive provided

some code below to show what Im working on







Skinned Model Extensions link




the code below draws the bat and positions it in the hand of the model

 /// <summary>
        /// Draws the baseball bat.
        /// </summary>
        void DrawBaseballBat(Matrix view, Matrix projection)
            int handIndex = skinningData.BoneIndices["L_Index1"];

            Matrix[] worldTransforms = animationPlayer.GetWorldTransforms();

            // Nudge the bat over so it appears between the left thumb and index finger.
            Matrix batWorldTransform = Matrix.CreateTranslation(-1.3f, 2.1f, 0.1f) * 

            foreach (ModelMesh mesh in baseballBat.Meshes)
                foreach (BasicEffect effect in mesh.Effects)
                    effect.World = batWorldTransform;
                    effect.View = view;
                    effect.Projection = projection;



[attachment=18208:10 4 2013 4 04 15 AM.jpeg]








I felt as though if I added a world effect(effect.World = batWorldTransform;) I could position

the particle effect in the models hand along with the bat model so that when I called the method

it would look something like the code below

 projectileTrailParticles.SetCamera(view , projection,  batWorldTransform);

but so far what I have in the update method is:


ParticleEmitter emit;

 emit = new ParticleEmitter(projectileTrailParticles,
                                             trailParticlesPerSecond, position);


            emit.Update(gameTime, position);

and the called method

projectileTrailParticles.SetCamera(view , projection);

visual of what I have so far

[attachment=18205:10 4 2013 3 11 34 AM.jpeg]


moving with the model left and right


[attachment=18206:10 4 2013 3 12 04 AM.jpeg]




and the closest Ive come  is the smoke particle moving with the camera instead of

staying  in one place giving the effect of a cloud hovering around

the model

projectileTrailParticles.SetCamera(view + batWorldTransform1, projection);

[attachment=18207:10 4 2013 3 14 09 AM.jpeg]

Share this post

Link to post
Share on other sites

So you beared out what I already mentioned in the 1st answer: Attaching the model matrix of the particle system to the camera isn't a valid solution. The camera is an independent object in the world, and any manipulation to it has an effect on the view of every object in the world.


What you IMHO need is to attach the particle emitter to the bone, and generate and update the particles itself in world space. Mathematically that means to compute the world matrix of the emitter as

    EW := EL * B1L * B2L * ... * BR * M


   EL is the placement of the emitter w.r.t. the bone,

   B1L * B2L * ... * BR is the sequence from the bone (where the emitter is attached) up to the root bone, and

   is the world matrix of the model.


projectileTrailParticles.SetCamera(view + batWorldTransform1, projection);

This is wrong. Concatenation of transformations is done by matrix multiplication but not matrix addition! Notice further that matrix multiplication is not commutative, so the order plays a role. E.g. the order "view * batWorldTransform1" would be wrong; see my first post above for the correct order.

Share this post

Link to post
Share on other sites

Hello there.

Make sure that all the math is(positions and that) all in the same space. Eg.All in bone space , or word space or some other space(Hippy world space munchout fest2.)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!