home *** CD-ROM | disk | FTP | other *** search
/ PC World 2006 July & August / PCWorld_2006-07-08_cd.bin / temacd / planearcade / planearcade.exe / Tank3.bmp / particlesystem.cpp < prev    next >
C/C++ Source or Header  |  2004-05-29  |  9KB  |  333 lines

  1.  
  2.  
  3. #include "main.h"
  4.  
  5. //------------------------------------------------------------------
  6. // Name: PARTICLESYSTEM()
  7. // Desc: deÜtruktor
  8. //------------------------------------------------------------------
  9. PARTICLESYSTEM::PARTICLESYSTEM()
  10. {
  11.     Point = NULL;
  12.     Particle = NULL;
  13.     NumParticles = 0;
  14.     ActParticle = 0;
  15.  
  16.  
  17.     //
  18.     //vÜetko nastavi na default
  19.     //
  20.  
  21.     //--------------
  22.     //CycleMode
  23.     //--------------
  24.     CycleLeght = 10000.0f; 
  25.     SpawnFreqency = 20.0f; 
  26.     ActTime = 0.0f;  
  27.  
  28.  
  29.     //-----------------
  30.     //Kolizie
  31.     //-----------------
  32.     Oct = NULL;
  33.     OctreeCollision = false;
  34.  
  35.     //---------------
  36.     //Vlastnosti
  37.     //---------------
  38.     Emitter.Position = Get3D(0.0f,0.0f,0.0f);
  39.     Emitter.Radius = 10.0f;
  40.     Emitter.SizeX = 10.0f;
  41.     Emitter.SizeY = 10.0f;
  42.     Emitter.SizeZ = 10.0f;
  43.     Emitter.Type = EMITTER_POINT;
  44.  
  45.     Direction = Get3D(0.0f,5.0f,0.0f);
  46.     Gravity = Get3D(0.0f,-2.0f,0.0f);
  47.     DirectionPower = 0.1f;
  48.     GravityPower = 0.2f;
  49.  
  50.     SpawnPower  = 10.0f;
  51.     MirrorFactor = 0.6f;
  52.     DirRandFactor = 1.0f;  
  53.  
  54.     Rotation = true;
  55.     RotationSpeed = 0.1f;
  56.  
  57.     //SRC   vlastnosti pociatocne
  58.     SRCSize = 0.7f;
  59.     SRCFrame = 0.0f;
  60.     SRCColor = GetColor(1.0f,1.0f,1.0f,0.0f);
  61.  
  62.     //DST  vlastnosti koneΦne
  63.     DSTSize = 0.7f;
  64.     DSTFrame = 0.0f;
  65.     DSTColor = GetColor(0.0f,1.0f,0.0f,0.0f);
  66.  
  67. }
  68. //------------------------------------------------------------------
  69. // Name: ~PARTICLESYSTEM()
  70. // Desc: konÜtruktor
  71. //------------------------------------------------------------------
  72. PARTICLESYSTEM::~PARTICLESYSTEM()
  73. {
  74.  
  75.     if (Point != NULL)
  76.         delete[]Point;
  77.     Point = NULL;
  78.  
  79. }
  80.  
  81. //------------------------------------------------------------------
  82. // Name: Initialize()
  83. // Desc: inicializuje system
  84. //------------------------------------------------------------------
  85. void PARTICLESYSTEM::Initialize(PARTICLE *P,int NParticles)
  86. {
  87.  
  88.     //nastavi pointer 
  89.     Particle = P;
  90.  
  91.     Point = new PARTICLEPOINT [NParticles];
  92.     NumParticles = NParticles;
  93.  
  94.     //nastavi vsetky particli na neaktivne
  95.     for (int i=0 ;i<NumParticles;i++)
  96.     {
  97.         Point[i].Active = false;
  98.     }
  99.  
  100. }
  101. //------------------------------------------------------------------
  102. // Name: 
  103. // Desc: Vyrenderuje aktivne particli
  104. //------------------------------------------------------------------
  105. void PARTICLESYSTEM::Render()
  106. {
  107.  
  108.     //vysledne
  109.     COLOR Color;
  110.     float Frame;
  111.     float Size;
  112.  
  113.     //rozdiely
  114.     COLOR DColor;
  115.     float DFrame;
  116.     float DSize;
  117.  
  118.     //rozdiely vypocita skor pre urychlenie
  119.     DColor.A = DSTColor.A - SRCColor.A;    
  120.     DColor.R = DSTColor.R - SRCColor.R;    
  121.     DColor.G = DSTColor.G - SRCColor.G;    
  122.     DColor.B = DSTColor.B - SRCColor.B;    
  123.     DFrame = DSTFrame - SRCFrame;
  124.     DSize = DSTSize - SRCSize;
  125.     
  126.     
  127.     //interpolant Φasu
  128.     float Int;
  129.  
  130.     //pre vÜetky particles
  131.     for (int i=0;i<NumParticles;i++)
  132.     {
  133.         //ak je aktviny
  134.         if (Point[i].Active == true)
  135.         {
  136.             //vypocet Interpolatu
  137.             Int = Point[i].Time / CycleLeght ;
  138.         
  139.             //interpoluj vlastnosti
  140.             Color.A = SRCColor.A + (Int * DColor.A);
  141.             Color.R = SRCColor.R + (Int * DColor.R);
  142.             Color.G = SRCColor.G + (Int * DColor.G);
  143.             Color.B = SRCColor.B + (Int * DColor.B);
  144.  
  145.             Frame = SRCFrame + (Int*DFrame);
  146.             Size = SRCSize + (Int*DSize);
  147.         
  148.             Particle->RenderParticle(Point[i].Position,Color,Size,Point[i].Rotation,Frame);
  149.         //    DebugDrawLine(Point[i].Position,Get3D(Point[i].Position.X + Point[i].Direction.X,Point[i].Position.Y + Point[i].Direction.Y,Point[i].Position.Z+ Point[i].Direction.Z));
  150.         }
  151.     }
  152.  
  153.     Engine.SetZwrite(false);
  154.         Particle->Render();
  155.     Engine.SetZwrite(true);
  156. }
  157. //------------------------------------------------------------------
  158. // Name: ProcessParticles()
  159. // Desc: VypoΦita poziciu particlov
  160. //------------------------------------------------------------------
  161. void PARTICLESYSTEM::ProcessParticles()
  162. {
  163.  
  164.     //pocitadlo
  165.     if(ActTime > SpawnFreqency)
  166.     {
  167.         ActTime = 0.0f;
  168.     }
  169.     ActTime += PowerTime(1.0f);
  170.  
  171.     for (int i=0;i<NumParticles;i++)
  172.     {
  173.         if (Point[i].Active == true)
  174.         {
  175.  
  176.             //casovac
  177.             Point[i].Time += PowerTime(1.0f);
  178.  
  179.             //zastavi particle ak uz dlho zije
  180.             if (Point[i].Time > CycleLeght)
  181.             {
  182.                 Point[i].Active = false;
  183.                 continue;
  184.             }
  185.                 
  186.             //vypocita smerovy vektor
  187.             Point[i].Direction.X += Gravity.X*Power(GravityPower);
  188.             Point[i].Direction.Y += Gravity.Y*Power(GravityPower);
  189.             Point[i].Direction.Z += Gravity.Z*Power(GravityPower);
  190.  
  191.             //kolizia
  192.             if (OctreeCollision == true)
  193.             {
  194.                 if(Oct->ColliseDistance(Point[i].Position,Get3D(Point[i].Position.X + Power(DirectionPower) *Point[i].Direction.X,
  195.                                                      Point[i].Position.Y + Power(DirectionPower) *Point[i].Direction.Y,
  196.                                                      Point[i].Position.Z + Power(DirectionPower) *Point[i].Direction.Z)))
  197.                 
  198.                 {
  199.                     float DirSize = CalcDistance(Point[i].Direction,Get3D(0.0f,0.0f,0.0f));
  200.  
  201.                     //upravi kolizny normalovy vektor na velkost direction vektoru
  202.                     VECTOR3D Pom;
  203.                     Pom = Oct->IntNormal;
  204.                     Pom.X = Pom.X * -DirSize;
  205.                     Pom.Y = Pom.Y * -DirSize;
  206.                     Pom.Z = Pom.Z * -DirSize;
  207.                     
  208.                     //sub
  209.                     Pom.X = Pom.X + Point[i].Direction.X;
  210.                     Pom.Y = Pom.Y + Point[i].Direction.Y;
  211.                     Pom.Z = Pom.Z + Point[i].Direction.Z;
  212.  
  213.                     //normalize
  214.                     Normalize(&Pom);
  215.  
  216.                     Point[i].Direction.X = -Pom.X * DirSize * MirrorFactor;
  217.                     Point[i].Direction.Y = -Pom.Y * DirSize * MirrorFactor;
  218.                     Point[i].Direction.Z = -Pom.Z * DirSize * MirrorFactor;
  219.                 }
  220.             
  221.             }
  222.             
  223.  
  224.             //vypocita poziciu
  225.             Point[i].Position.X += Power(DirectionPower) *  Point[i].Direction.X;
  226.             Point[i].Position.Y += Power(DirectionPower) *  Point[i].Direction.Y;
  227.             Point[i].Position.Z += Power(DirectionPower) *  Point[i].Direction.Z;
  228.             Point[i].Rotation += Power(RotationSpeed);
  229.  
  230.         }
  231.  
  232.     }
  233.  
  234. }
  235.  
  236.  
  237. //------------------------------------------------------------------
  238. // Name: SpawnParticles()
  239. // Desc: Vypusti naraz particli
  240. //------------------------------------------------------------------
  241. void PARTICLESYSTEM::SpawnParticles(int NParticles)
  242. {
  243.     for (int i=0;i<NParticles;i++)
  244.         Spawn();
  245. }
  246.  
  247.  
  248. //------------------------------------------------------------------
  249. // Name: SpawnParticlesTime()
  250. // Desc: Vypusta particli neustale podla frekvencie
  251. //------------------------------------------------------------------
  252. void PARTICLESYSTEM::SpawnParticlesTime(int NParticles)
  253. {
  254.     if(ActTime > SpawnFreqency)
  255.     {
  256.         SpawnParticles(NParticles);
  257.     }
  258. }
  259.  
  260. //------------------------------------------------------------------
  261. // Name: Spawn()
  262. // Desc: Vypusti jeden particle
  263. //------------------------------------------------------------------
  264. void PARTICLESYSTEM::Spawn()
  265.  
  266.     //vektor vygenerovany
  267.     VECTOR3D RnDir;
  268.  
  269.     //ak je eÜte stale aktviny nerenderuj
  270.     if (Point[ActParticle].Active == true)
  271.         return;
  272.  
  273.     //////////
  274.     //Random
  275.     RnDir = RandomVector();
  276.  
  277.     RnDir.X = (RnDir.X*DirRandFactor) + Direction.X;
  278.     RnDir.Y = (RnDir.Y*DirRandFactor) + Direction.Y;
  279.     RnDir.Z = (RnDir.Z*DirRandFactor) + Direction.Z;
  280.  
  281.     Normalize(&RnDir);
  282.  
  283.     /////////
  284.     //Point
  285.     if (Emitter.Type == EMITTER_POINT)
  286.     {
  287.         Point[ActParticle].Position = Emitter.Position;
  288.     }
  289.  
  290.     /////////
  291.     //Box
  292.     if (Emitter.Type == EMITTER_BOX)
  293.     {
  294.         Point[ActParticle].Position.X = Emitter.Position.X + RandomMinMax(-Emitter.SizeX,Emitter.SizeX);
  295.         Point[ActParticle].Position.Y = Emitter.Position.Y + RandomMinMax(-Emitter.SizeY,Emitter.SizeY);
  296.         Point[ActParticle].Position.Z = Emitter.Position.Z + RandomMinMax(-Emitter.SizeZ,Emitter.SizeZ);
  297.     }
  298.     /////////
  299.     //Sphere
  300.     if (Emitter.Type == EMITTER_SPHERE)
  301.     {
  302.         Point[ActParticle].Position.X = Emitter.Position.X + RandomMinMax(-Emitter.Radius,Emitter.Radius);
  303.         Point[ActParticle].Position.Y = Emitter.Position.Y + RandomMinMax(-Emitter.Radius,Emitter.Radius);
  304.         Point[ActParticle].Position.Z = Emitter.Position.Z + RandomMinMax(-Emitter.Radius,Emitter.Radius);
  305.     }
  306.  
  307.     Point[ActParticle].Active = true;
  308.     Point[ActParticle].Direction.X = RnDir.X * SpawnPower;
  309.     Point[ActParticle].Direction.Y = RnDir.Y * SpawnPower;
  310.     Point[ActParticle].Direction.Z = RnDir.Z * SpawnPower;
  311.     Point[ActParticle].Time = 0.0f;
  312.     Point[ActParticle].Rotation = RandomMinMax(-D3DX_PI,D3DX_PI);
  313.  
  314.     ActParticle++;
  315.  
  316.     if (ActParticle == NumParticles)
  317.         ActParticle = 0;
  318.  
  319. }
  320.  
  321.  
  322. //------------------------------------------------------------------
  323. // Name: SetOctreeCollision()
  324. // Desc: nastavi koliziu particlov zo octree
  325. //------------------------------------------------------------------
  326. void PARTICLESYSTEM::SetOctreeCollision(OCTREE *O)
  327. {
  328.     Oct = O;
  329.     OctreeCollision = true;
  330. }
  331.  
  332.