shooting!

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
LOL

shooting!

Post by LOL »

Hi,
I am making an FPS game using IrrWizard. I want projectiles to be displayed when a gun is fired.
I have tried looking at the Irrlicht Demo Example since it has a "visible shot" and have tried to incorporate it into the coding which is generated by IrrWizard, but with no success.:cry:

I would very grateful if someone can help me on this issue. :wink:

thx
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

What specific problems are you having? Any error messages, got any short bits of code that you can show where you've made the changes?
Image Image Image
LOL

shooting

Post by LOL »

I've tried to incorporate the shoot() function(in the CDemo.cpp) from the Irrlicht Demo into GamePlayState.cpp (This is a file generated by IrrWizard).
The thing runs fine but as soon as i fire the game crashes.!

void CGamePlayState::shoot()
{
IrrlichtDevice *device;

scene::ISceneManager* sm = device->getSceneManager();
scene::ICameraSceneNode* camera = sm->getActiveCamera();

if (!camera || !m_Selector)
return;

SParticleImpact imp;
imp.when = 0;

// get line of camera

core::vector3df start = camera->getPosition();
core::vector3df end = (camera->getTarget() - start);
end.normalize();
start += end*5.0f;
end = start + (end * camera->getFarValue());


core::triangle3df triangle;

core::line3d<f32> line(start, end);

// get intersection point with map

if (sm->getSceneCollisionManager()->getCollisionPoint(
line, m_Selector, end, triangle))
{
// collides with wall
core::vector3df out = triangle.getNormal();
out.setLength(0.03f);

imp.when = 1;
imp.outVector = out;
imp.pos = end;
}
else
{
// doesnt collide with wall
end = (camera->getTarget() - start);
end.normalize();
end = start + (end * 1000);
}

// create fire ball
scene::ISceneNode* node = 0;
node = sm->addBillboardSceneNode(0,
core::dimension2d<f32>(25,25), start);

node->setMaterialFlag(video::EMF_LIGHTING, false);
node->setMaterialTexture(0, device->getVideoDriver()->getTexture("media/particle.bmp"));
node->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);


f32 length = (f32)(end - start).getLength();
const f32 speed = 0.6f;
u32 time = (u32)(length / speed);

scene::ISceneNodeAnimator* anim = 0;

// set flight line

anim = sm->createFlyStraightAnimator(start, end, time);
node->addAnimator(anim);
anim->drop();

anim = sm->createDeleteAnimator(time);
node->addAnimator(anim);
anim->drop();

if (imp.when)
{
// create impact note
imp.when = device->getTimer()->getTime() + (time - 100);
Impacts.push_back(imp);
}
}


///////////////////////////////////

void CGamePlayState::MouseEvent(CGameManager* pManager)
{

scene::ITriangleSelector* mapSelector;

// left mouse pressed, FIRE!!!
if (pManager->getMouse() == EMIE_LMOUSE_PRESSED_DOWN )
{
if (pManager->getPlayer()->getAmmo() > 0)
{
shoot(); // this is what i have added!
pManager->getPlayer()->setAmmo(pManager->getPlayer()->getAmmo() - 4);
// get the scene node
scene::ISceneNode* node = pManager->getSceneManager()->getSceneCollisionManager()->
getSceneNodeFromCameraBB(pManager->getSceneManager()->getActiveCamera(), ID_ENEMY);

if (node->getID() == ID_ENEMY )
{
// hit an enemy, not a wall
CGameEnemy* enemy = pManager->getEnemyManager()->GetEntityFor(ENTITY_MERC);
enemy->setHealth(enemy->getHealth() - 20);
pManager->createParticleEffect(pManager, getTargetsPosition(pManager), "media/blood.bmp");
} else
pManager->createParticleEffect(pManager, getTargetsPosition(pManager), "media/smoke.bmp");
}
}
// right mouse pressed, RELOAD!!!
if (pManager->getMouse() == EMIE_RMOUSE_PRESSED_DOWN )
{
pManager->getPlayer()->setAmmo(100);
/* if (pManager->getPlayer()->getAmmo() <= 0)
pManager->getPlayer()->setAmmo(100); // 100% full */

}

}
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

What's the error you get?
Image Image Image
area51
Posts: 338
Joined: Thu Mar 18, 2004 10:20 pm
Location: UK
Contact:

Post by area51 »

Most of the code in the shoot() function will be redundant as all you are really trying to do is create a FlyStraightAnimator with the correct parameters.

All the parameters you need will be in the calling function, CGamePlayState::MouseEvent().

In your shoot fuction you just need to do the //create fireball and //set flight line bit to start with.

You might want to pass in a pointer to the GameManager, pManager. You can then get a pointer to Irrlicht subsytems like the SceneManager (sm) in the code you have like this :

Code: Select all

    pManager->getSceneManager();
to create a FlyStraightAnimator you can do this:

Code: Select all

  pManager->getSceneManager()->createFlystraightAnimator(camPos,targetPos, 5000);
to get the first parameter, camPos:

Code: Select all

   pManager->getSceneManager()->getActiveCamera()->getPosition()
and the second, the target position, wall or enemy (getTargetsPosition() is a member function of CGamePlayState() by the looks)

Code: Select all

  getTargetsPosition(pManager)
and so on.

Sorry it's only high level help, but I think in this case it's better to step back from the code and think what you want to achieve from it. Then start from a blank and build it up rather than cut and paste a large amount of code and then whittle it back.
________
Avatar: the last airbender forum
Last edited by area51 on Thu Feb 24, 2011 11:59 pm, edited 1 time in total.
LOL

Post by LOL »

Thank you area51, I finally got it to work! :D

But i've got one problem - How do I actually create the fireball? because at the moment i've only got a white cube being fired!

when i put in this piece of code the game crashes when i fire the gun.

Code: Select all

node2->setMaterialTexture(0, m_pDevice->getVideoDriver()->getTexture("media/particle.bmp"));
Ive got rid of the shoot() function and just put the coding in CGamePlayState::MouseEvent().

Here is the CGamePlayState::MouseEvent() so far:

Code: Select all

void CGamePlayState::MouseEvent(CGameManager* pManager)
{
    
		scene::ITriangleSelector* mapSelector;

     // left mouse pressed, FIRE!!!
	if (pManager->getMouse() == EMIE_LMOUSE_PRESSED_DOWN ) 
    {
        if (pManager->getPlayer()->getAmmo() > 0)
        {                   

           pManager->getSceneManager(); 
           core::vector3df camPos =      pManager->getSceneManager()->getActiveCamera()->getPosition(); 
           core::vector3df targetPos = getTargetsPosition(pManager);
            scene::ICameraSceneNode* camera = pManager->getSceneManager()->getActiveCamera();	
            	
           pManager->getSceneManager()->createFlyStraightAnimator(camPos,targetPos, 5000); 
          	   
              
              ///////////////////
           pManager->getPlayer()->setAmmo(pManager->getPlayer()->getAmmo() - 4);
           // get the scene node
           scene::ISceneNode* node = pManager->getSceneManager()->getSceneCollisionManager()->
                 getSceneNodeFromCameraBB(pManager->getSceneManager()->getActiveCamera(), ID_ENEMY);
   
   
   core::triangle3df triangle;
   core::line3d<f32> line(camPos, targetPos);
   	SParticleImpact imp; 
	imp.when = 0;

	// get intersection point with map

	if (pManager->getSceneManager()->getSceneCollisionManager()->getCollisionPoint(
		line, m_Selector, targetPos, triangle))
	{
		// collides with wall
		core::vector3df out = triangle.getNormal();
		out.setLength(0.03f);

		imp.when = 1;
		imp.outVector = out;
		imp.pos = targetPos;
	}
	else
	{
		// doesnt collide with wall
		targetPos = (camera->getTarget() - camPos);
		targetPos.normalize();
		targetPos = camPos + (targetPos * 1000);
	}
   
   scene::ISceneNode* node2 = 0;
	node2 = pManager->getSceneManager()->addBillboardSceneNode(0,
		core::dimension2d<f32>(25,25), camPos);

	node2->setMaterialFlag(video::EMF_LIGHTING, false);

**********************************************************
//This next line is giving me problems. If i put it in then the game crashes on firing, otherwise i get a white cube being fired!
**********************************************************

	This one -> //node2->setMaterialTexture(0, m_pDevice->getVideoDriver()->getTexture("media/particle.bmp"));


	node2->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);

   
   	scene::ISceneNodeAnimator* anim = 0;
   	anim = pManager->getSceneManager()->createFlyStraightAnimator(camPos, targetPos, 500);
	node2->addAnimator(anim);	
	anim->drop();

	anim = pManager->getSceneManager()->createDeleteAnimator(500);
	node2->addAnimator(anim);
	anim->drop();
area51
Posts: 338
Joined: Thu Mar 18, 2004 10:20 pm
Location: UK
Contact:

Post by area51 »

I can't see where you've initialised m_pDevice, so use this instead:

Code: Select all

node2->setMaterialTexture(0, pManager->getDevice()->getVideoDriver()->getTexture("media/particle.bmp")); 

Also this code below:

Code: Select all

core::triangle3df triangle; 
   core::line3d<f32> line(camPos, targetPos); 
      SParticleImpact imp; 
   imp.when = 0; 

   // get intersection point with map 

   if (pManager->getSceneManager()->getSceneCollisionManager()->getCollisionPoint( 
      line, m_Selector, targetPos, triangle)) 
   { 
      // collides with wall 
      core::vector3df out = triangle.getNormal(); 
      out.setLength(0.03f); 

      imp.when = 1; 
      imp.outVector = out; 
      imp.pos = targetPos; 
   } 
   else 
   { 
      // doesnt collide with wall 
      targetPos = (camera->getTarget() - camPos); 
      targetPos.normalize(); 
      targetPos = camPos + (targetPos * 1000); 
   } 
    
isn't doing that much more than this:

Code: Select all

    core::vector3df targetPos = getTargetsPosition(pManager); 
________
Yamaha fj600
Last edited by area51 on Thu Feb 24, 2011 11:59 pm, edited 1 time in total.
LOL
Posts: 4
Joined: Wed Apr 19, 2006 12:47 pm

Post by LOL »

Thanks area51, youre a star! :wink:

it works like a charm! :D
Post Reply