• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2154
  • Last Modified:

C++ & DirectX: How to render lasers particles and explosions

I have a top down space game that uses DirectX and generic bitmaps.
When I want to draw a basic laser I do something like this:

dxbb->drawLaser(x1,y1, x2,y2, width, color1|0xFF000000);

Where lasermode:
                  case (Render::laserMode):
                        device->SetRenderState(D3DRS_ALPHATESTENABLE, 1);
                        device->SetRenderState(D3DRS_ALPHABLENDENABLE, 1);
                        device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
                        device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

And drawlaser:

void DxCanvas::drawLaser(int ix1, int iy1, int ix2,int iy2,int iwidth, D3DCOLOR color){
      int r = ((color & 0xFF0000) >> 16),
            g = ((color & 0xFF00) >> 8), b = (color & 0xFF);
      r = r/4 + 0xFF*3/4;
      g = g/4 + 0xFF*3/4;
      b = b/4 + 0xFF*3/4;
      if (r > 0xFF)
            r = 0xFF;
      if (g > 0xFF)
            g = 0xFF;
      if (b > 0xFF)
            b = 0xFF;
      D3DCOLOR outerColor = (color & 0xFFFFFF)|0x00000000;
      D3DCOLOR innerColor = (r << 16) | (g << 8) | (b) | (color & 0xFF000000);//color |0xFF000000;
      float x1 = (float)ix1;
      float x2 = (float)ix2;
      float y1 = (float)iy1;
      float y2 = (float)iy2;
      float width = (float)iwidth;
      float dx = x2-x1;
      float dy = y2-y1;
      float len = sqrt(dx*dx + dy*dy);
            dx /= len;
            dy /= len;
      dx *= width*0.5f;
      dy *= width*0.5f;
      float px = -dy;//perpendicular
      float py = dx;



      GLDraw::vertex2f(x1 - dx, y1 - dy);

      GLDraw::vertex2f(x1, y1);

      GLDraw::vertex2f(x1 - px, y1 - py);

      GLDraw::vertex2f(x1, y1);

      GLDraw::vertex2f(x2 - px, y2 - py);

      GLDraw::vertex2f(x2, y2);

      GLDraw::vertex2f(x2 + dx, y2 + dy);

      GLDraw::vertex2f(x2, y2);

      GLDraw::vertex2f(x2 + px, y2 + py);

      GLDraw::vertex2f(x2, y2);

      GLDraw::vertex2f(x1 + px, y1 + py);

      GLDraw::vertex2f(x1, y1);

      GLDraw::vertex2f(x1 - dx, y1 - dy);

      GLDraw::vertex2f(x1, y1);



This draws a 'star wars' type laser, which overall does look good. However, it's basic. It's just shooting a line from your ship to the enemy ship, no particles around the laser or damage where the laser hits. So this was my first question, if theres a simple way to do this so it looks more 'pretty'

Another issue is explosions. I was wondering if there is a tutorial on how to design/add explosions using directx rendering instead of bitmaps as it currently is. I want it to look a bit more fancier since we added DirectX recently and bitmaps are pretty generic and eat up alot of resources when tons are being used at the same time.

Would anyone have any ideas/tutorials that I could read up, mainly to make explosions and use some sort of particle system for laser looks? Thanks!
2 Solutions
>> Another issue is explosions. I was wondering if there is a tutorial on how to design/add explosions using directx

Perhaps using particle effects you can get a nice explosion. You can download a DirectX Particle Demo with source code (so it says):

Also, see Tutorial 11 to learn how to use dynamic buffers to create a nice particle effect.
Looks like many other useful tutorials with source code as well.
George TokasCommented:
The code you provided is using 2Dimentional rendering..
In this case explosion or particle rendering is just a matter of sprites rendered in the screen...
For an explotion use an image with frames of the explotion and render the frames when needed...
For particles its another story...
In this case you need some (more than one - maybe 3-4) buffers (sprites) and display them in a center based incremental distance...
In case of just explotion that is not required since you can made it with the frames of the explotion....
Take care because those functions consumes memory video or system...
Have in mind that in whatever format you use to store and load your pictures, when loaded there are converted in DIBs and that is a kind of bitmap...

George Tokas.
VallerianiAuthor Commented:
Thanks both!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now