Help with particle/beam

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
empirepro
Posts: 71
Joined: Thu Dec 27, 2007 5:41 am

Help with particle/beam

Post by empirepro »

I am trying to make a star ship thats able to shoot a laser beam. I have tried the beam scene node but it doesn't show up when I activate it. here is the code:

laser.h

Code: Select all

#include <irrlicht.h>
#include <iostream>
#include <ITexture.h>
#include <SMaterial.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

// Just a simple wrapper :D
struct IrrQuad {
   video::S3DVertex verts[4];
};

class CBeamNode : public scene::ISceneNode {
   private:
      // The beam material.
      video::SMaterial material;

      // Start/End Points
      core::vector3df vStart;
      core::vector3df vEnd;

      // Bounding Box
      core::aabbox3d<f32> Box;

      // Size of the beam
      float flScale;

      // Beam color
      video::SColor beamColor;

      void DrawQuad( IrrQuad& quad ) {
         u16 indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
         video::IVideoDriver* driver = SceneManager->getVideoDriver();
         driver->setMaterial(material);
         driver->drawIndexedTriangleList( &quad.verts[0], 4, &indices[0], 4 );
      }

      // Thanks to whoever wrote this little function :)
      core::vector3df getTargetAngle( core::vector3df v, core::vector3df r) {
         //v -current node position
         //r -target node position

         core::vector3df angle;
         float x,y,z;
         x = r.X - v.X;
         y = r.Y - v.Y;
         z = r.Z - v.Z;

         //angle in X-Z plane
         angle.Y = atan2 (x, z);
         angle.Y *= (180 / 3.14); //converting from rad to degrees

         //just making sure angle is somewhere between 0-360 degrees
         if(angle.Y < 0) angle.Y += 360;
         if(angle.Y >= 360) angle.Y -= 360;

         //angle in Y-Z plane while Z and X axes are already rotated around Y
         float z1 = sqrt(x*x + z*z);

         angle.X = atan2 (z1, y);
         angle.X *= (180 / 3.14); //converting from rad to degrees
         angle.X -= 90;

         //just making sure angle is somewhere between 0-360 degrees
         if(angle.X < 0) angle.X += 360;
         if(angle.X >= 360) angle.X -= 360;

         return angle;
      }

   public:

      CBeamNode( scene::ISceneNode* parent, scene::ISceneManager *mgr, s32 id, char* szBeam ) : scene::ISceneNode( parent, mgr, id ) {
         // Setup the beam material
         material.Wireframe = false;
         material.Lighting = false;
         material.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
		 video::ITexture* texture = mgr->getVideoDriver( )->getTexture( szBeam );
         material.setTexture(0, texture);
		 

         // Default to 32 units for the scale
         flScale = 32.0;

         // Default to white
         beamColor.set( 255, 255, 255, 255 );
      }

      virtual void OnPreRender( ) {
         if( IsVisible ) {
            SceneManager->registerNodeForRendering( this );
         }
      }

      virtual void render( ) {
        SceneManager->getVideoDriver()->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);

         // Figure out quads based on start/end points.
         core::matrix4 m;
         m.setRotationDegrees( getTargetAngle( vStart, vEnd ) );
         core::vector3df vUp( 0, 1, 0 );
         core::vector3df vRight( -1, 0, 0 );
         m.transformVect( vRight );
         m.transformVect( vUp );

         // Draw the first cross
         IrrQuad beam;
         beam.verts[0] = video::S3DVertex( vStart + vUp * flScale, core::vector3df( 1, 1, 0 ), beamColor, core::vector2df( 0, 1 ) );
         beam.verts[1] = video::S3DVertex( vStart + vUp * -flScale, core::vector3df( 1, 0, 0 ), beamColor, core::vector2df( 1, 1 ) );
         beam.verts[2] = video::S3DVertex( vEnd + vUp * -flScale, core::vector3df( 0, 1, 1 ), beamColor, core::vector2df( 1, 0 ) );
         beam.verts[3] = video::S3DVertex( vEnd + vUp * flScale, core::vector3df( 0, 0, 1 ), beamColor, core::vector2df( 0, 0 ) );
         DrawQuad( beam );

         // Draw the second cross.
         beam.verts[0] = video::S3DVertex( vStart + vRight * flScale, core::vector3df( 1, 1, 0 ), beamColor, core::vector2df( 0, 1 ) );
         beam.verts[1] = video::S3DVertex( vStart + vRight * -flScale, core::vector3df( 1, 0, 0 ), beamColor, core::vector2df( 1, 1 ) );
         beam.verts[2] = video::S3DVertex( vEnd + vRight * -flScale, core::vector3df( 0, 1, 1 ), beamColor, core::vector2df( 1, 0 ) );
         beam.verts[3] = video::S3DVertex( vEnd + vRight * flScale, core::vector3df( 0, 0, 1 ), beamColor, core::vector2df( 0, 0 ) );
         DrawQuad( beam );
      }

      virtual const core::aabbox3d<f32>& getBoundingBox() const {
         return Box;
      }

      virtual s32 getMaterialCount() {
         return 1;
      }

      virtual video::SMaterial& getMaterial(s32 i) {
         return material;
      }

      void SetStartPoint( core::vector3df pos ) {
         vStart = pos;
      }

      void SetEndPoint( core::vector3df pos ) {
         vEnd = pos;
      }

      void SetBeamScale( float size ) {
         flScale = size;
      }

      void SetBeamColor( video::SColor color ) {
         beamColor = color;
      }
};
main.cpp

Code: Select all

/*
This Tutorial shows how to move and animate SceneNodes. The
basic concept of SceneNodeAnimators is shown as well as manual
movement of nodes using the keyboard.

As always, I include the header files, use the irr namespace,
and tell the linker to link with the .lib file.
*/
#include <irrlicht.h>
[b]#include "laser.h"[/b]
#include "flightfunctions.h"
#include "shipcontrols.h"
#include <iostream>

using namespace irr;

double playerspeed = 0.0;

#pragma comment(lib, "Irrlicht.lib")




int main()
{
	// let user select driver type

	video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;

	printf("Please select the driver you want for this example:\n"\
		" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
		" (d) Software Renderer\n (e) Burning's Software Renderer\n"\
		" (f) NullDevice\n (otherKey) exit\n\n");

	char i;
	std::cin >> i;

	switch(i)
	{
		case 'a': driverType = video::EDT_DIRECT3D9;break;
		case 'b': driverType = video::EDT_DIRECT3D8;break;
		case 'c': driverType = video::EDT_OPENGL;   break;
		case 'd': driverType = video::EDT_SOFTWARE; break;
		case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
		case 'f': driverType = video::EDT_NULL;     break;
		default: return 0;
	}	

	// create device
	MyEventReceiver receiver;

	IrrlichtDevice* device = createDevice( driverType, core::dimension2d<s32>(1000, 800),
		16, false, false, false, &receiver);

	if (device == 0)
		return 1; // could not create selected driver.


	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
    smgr->addSkyDomeSceneNode(driver->getTexture("media/Textures/sky.bmp"),32,32,1.0f,2.0f);
    smgr->loadScene("Media/Levels/space.irr");
	scene::ISceneNode* node1 = smgr->addSphereSceneNode(); //replace with what ever you want
	if (node1)
	{
		node1->setMaterialFlag(EMF_LIGHTING, false);
		node1->setMaterialTexture( 0, driver->getTexture("media/Textures/borg.psd") );
		node1->setPosition(core::vector3df(-550,0,0));
	}
  
	scene::ISceneNode* node2 = smgr->addCubeSceneNode(); //just for reference
   if (node2)
      node2->setMaterialFlag(video::EMF_LIGHTING, false);
   node2->setPosition(core::vector3df(500,0,100));

   scene::ICameraSceneNode *camera = device->getSceneManager()->addCameraSceneNode();
   [b]CBeamNode* beam = new CBeamNode( smgr->getRootSceneNode( ), smgr, 200, "laserbeam.bmp" );[/b]

   int lastFPS = -1;

   while(device->run())
   { 
	   
	   driver->beginScene(true, true, SColor(255,100,101,140));
        smgr->drawAll();
       
        // direction control
        if(keys[irr::KEY_LEFT])
        {
            turn(node1, 0.5);
        }
        if(keys[irr::KEY_RIGHT])
        {
            turn(node1, -0.5);
        }
        if(keys[irr::KEY_UP])
        {
            pitch(node1, 0.5);
        }
        if(keys[irr::KEY_DOWN])
        {
            pitch(node1, -0.5);
        }
        if(keys[irr::KEY_COMMA])
        {
            roll(node1, 0.01);
        }
        if(keys[irr::KEY_PERIOD])
        {
            roll(node1, -0.01);
        }
       
        // movement control
        if(keys[irr::KEY_KEY_W])
        {
             playerspeed = playerspeed + 0.5;
        }
        if(keys[irr::KEY_KEY_S])
        {
             playerspeed = playerspeed - 0.0001;
             if (playerspeed = -0.01)
                    playerspeed = -.009;               
            move(node1, core::vector3df(0,0,playerspeed));
        }
        if(keys[irr::KEY_KEY_Q])
        {
             playerspeed = 0;
        }
		[b]if(keys[irr::KEY_KEY_F])
        {
			
beam->SetStartPoint( core::vector3df( -500, 0, 0 ) );
beam->SetEndPoint( core::vector3df( 500, 0, 0 ) );
beam->SetBeamColor( video::SColor( 200, 0, 255, 0 ) );
beam->SetBeamScale(1.5);
		}[/b]
		
       
        move(node1, core::vector3df(0,0,playerspeed));
       
        makeCockpit(camera, node1, core::vector3df(0,7,-30));
       
        driver->endScene();
		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
			core::stringw tmp(L"Movement Example - Irrlicht Engine [");
			tmp += driver->getName();
			tmp += L"] fps: "; 
			tmp += fps;

			device->setWindowCaption(tmp.c_str());
			lastFPS = fps;
		}
    }
     
   device->drop();

   return 1;
} 
The code compiles just fine but when I press the F button I am not able to see anything.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Presumably you want to pass the result of node1->getAbsolutePosition() to SetStartPoint().

Is your "laserbeam.bmp" texture in your working directory rather than your "media/Textures" directory? I notice that you haven't qualified the path, and that CBeamNode doesn't validate the texture.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
cdrwolfe
Posts: 100
Joined: Thu Nov 15, 2007 5:38 pm
Location: Cranfield University

Post by cdrwolfe »

I use a similar one though it is in C# i'am afraid, what exactly are you trying to create with regards to ship firing?

Regards Wolfe
empirepro
Posts: 71
Joined: Thu Dec 27, 2007 5:41 am

Post by empirepro »

rogerborg wrote:Presumably you want to pass the result of node1->getAbsolutePosition() to SetStartPoint().

Is your "laserbeam.bmp" texture in your working directory rather than your "media/Textures" directory? I notice that you haven't qualified the path, and that CBeamNode doesn't validate the texture.
The texture is in the top directory. When I start it up it loads the texture just fine. It just doesn't show anything when I press the button to fire it. right now the only thing I am worried about is getting it to show up on the screen once it shows up then I will worry about trying to get it aligned to the ship and everything.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Oh, got it.

OnPreRender() should be OnRegisterSceneNode(). That's changed since Irrlicht 1.3.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
empirepro
Posts: 71
Joined: Thu Dec 27, 2007 5:41 am

Post by empirepro »

I changed that and its still not showing up
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Presumably you've got some other code that actually moves the camera to a position where it could see the beam node? I moved it to (0, 0, -50) in order to see the node.

It occurs to me that the node isn't setting up its bounding box, but that's not causing a problem on my local version.

Other than that, all that I can suggest is that you break out your debugger and check that the node is actually registering itself for rendering, and being rendered.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
empirepro
Posts: 71
Joined: Thu Dec 27, 2007 5:41 am

Post by empirepro »

I am now using a different tactic for lasers that is now working. Now all that I need to figure out is collision detection and particle system for explosions.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

On a Satisfaction Scale of Kathy Bates to Alyson Hannigan, I rate this thread a Tony Collette: Borderline Self Recriminations, Would Not Do Again.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
cdrwolfe
Posts: 100
Joined: Thu Nov 15, 2007 5:38 pm
Location: Cranfield University

Post by cdrwolfe »

LOL,

On a side note, what type of collision detection you looking to implement.

Regards Wolfe
Post Reply