Advertisement
Guest User

XNA 3D Particle

a guest
Feb 4th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. //About:
  2. //      This is basically for particles but also has rotation because it started of as a class
  3. //          that was only for holding some data that would be shared between almost everything (position + rotation)
  4. //          it was easier to just convert this into particles than to write another one entirely.
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Audio;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.GamerServices;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Input;
  16. using Microsoft.Xna.Framework.Media;
  17.  
  18. namespace gphysics
  19. {
  20.     public class Particle
  21.     {
  22.         protected Vector3 mPosition;
  23.         protected Vector3 mVelocity;
  24.         protected Vector3 mAcceleration;
  25.         protected Vector3 mForceAccum;
  26.  
  27.         //Added variable to allow us to not use damping because it makes space physics not work.  
  28.         public static bool mUseDamping = true;
  29.         protected double mDamping;
  30.  
  31.         protected double mInverseMass;
  32.  
  33.         protected Vector3 mRotation;
  34.  
  35.         public Particle()
  36.         {
  37.             //I'm lazy so I want mDamping to have a default value.
  38.             mDamping = 0.9999999999999999999999999999999999f;
  39.         }
  40.  
  41.         public Vector3 GetPosition()
  42.         {
  43.             return mPosition;
  44.         }
  45.  
  46.         public void SetPosition(Vector3 Position)
  47.         {
  48.             mPosition = Position;
  49.         }
  50.  
  51.         public void Move(Vector3 Movement)
  52.         {
  53.             mPosition += Movement;
  54.         }
  55.  
  56.         public Vector3 GetVelocity()
  57.         {
  58.             return mVelocity;
  59.         }
  60.  
  61.         public void SetVelocity(Vector3 Velocity)
  62.         {
  63.             mVelocity = Velocity;
  64.         }
  65.  
  66.         public void AddVelocity(Vector3 AddedVel)
  67.         {
  68.             mVelocity += AddedVel;
  69.         }
  70.  
  71.         public Vector3 GetAcceleration()
  72.         {
  73.             return mAcceleration;
  74.         }
  75.  
  76.         public void SetAcceleration(Vector3 Acceleration)
  77.         {
  78.             mAcceleration = Acceleration;
  79.         }
  80.  
  81.         public void AddAcceleration(Vector3 AddedAccel)
  82.         {
  83.             mAcceleration += AddedAccel;
  84.         }
  85.  
  86.         public Vector3 GetForceAccum()
  87.         {
  88.             return mForceAccum;
  89.         }
  90.  
  91.         public void SetForceAccum(Vector3 ForceAccum)
  92.         {
  93.             mForceAccum = ForceAccum;
  94.         }
  95.  
  96.         public void AddForce(Vector3 Force)
  97.         {
  98.             mForceAccum += Force;
  99.         }
  100.  
  101.         private void ClearAccumulator()
  102.         {
  103.             mForceAccum = Vector3.Zero;
  104.         }
  105.  
  106.         public double GetDamping()
  107.         {
  108.             return mDamping;
  109.         }
  110.  
  111.         public void SetDamping(double Damping)
  112.         {
  113.             mDamping = Damping;
  114.         }
  115.  
  116.         public bool HasFiniteMass()
  117.         {
  118.             if (mInverseMass <= 0.0f)
  119.                 return false;
  120.  
  121.             return true;
  122.         }
  123.  
  124.         public double GetInverseMass()
  125.         {
  126.             return mInverseMass;
  127.         }
  128.  
  129.         public double GetMass()
  130.         {
  131.             return 1.0f / mInverseMass;
  132.         }
  133.  
  134.         public void SetInverseMass(double InverseMass)
  135.         {
  136.             mInverseMass = InverseMass;
  137.         }
  138.  
  139.         public void SetMass(double Mass)
  140.         {
  141.             mInverseMass = 1.0f / Mass;
  142.         }
  143.  
  144.         public Vector3 GetRotation()
  145.         {
  146.             return mRotation;
  147.         }
  148.  
  149.         public void SetRotation(Vector3 Rotation)
  150.         {
  151.             mRotation = Rotation;
  152.         }
  153.  
  154.         public void Rotate(Vector3 Rotation)
  155.         {
  156.             mRotation += Rotation;
  157.         }
  158.  
  159.         public void Integrate(GameTime elapsedTime)
  160.         {
  161.             if (mInverseMass <= 0.0f) return;
  162.  
  163.             float elapsedSeconds = (float)elapsedTime.ElapsedGameTime.Milliseconds/1000.0f;
  164.  
  165.             elapsedSeconds *= 100.0f;
  166.  
  167.             if (elapsedSeconds <= 0.0f) return;
  168.  
  169.             Vector3 resultingAcc = mAcceleration;
  170.             resultingAcc += mForceAccum * (float)mInverseMass;
  171.  
  172.             mPosition += mVelocity * elapsedSeconds;
  173.             if(!mUseDamping)
  174.                 mVelocity = (mVelocity) + resultingAcc * elapsedSeconds;
  175.             else
  176.                 mVelocity = (mVelocity * (float)mDamping) + resultingAcc * elapsedSeconds;
  177.  
  178.             ClearAccumulator();
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement