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.
I would very grateful if someone can help me on this issue.
thx
shooting!
shooting
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 */
}
}
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 */
}
}
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 :
to create a FlyStraightAnimator you can do this:
to get the first parameter, camPos:
and the second, the target position, wall or enemy (getTargetsPosition() is a member function of CGamePlayState() by the looks)
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
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();
Code: Select all
pManager->getSceneManager()->createFlystraightAnimator(camPos,targetPos, 5000);
Code: Select all
pManager->getSceneManager()->getActiveCamera()->getPosition()
Code: Select all
getTargetsPosition(pManager)
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.
Thank you area51, I finally got it to work!
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.
Ive got rid of the shoot() function and just put the coding in CGamePlayState::MouseEvent().
Here is the CGamePlayState::MouseEvent() so far:
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"));
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();
I can't see where you've initialised m_pDevice, so use this instead:
Also this code below:
isn't doing that much more than this:
________
Yamaha fj600
Code: Select all
node2->setMaterialTexture(0, pManager->getDevice()->getVideoDriver()->getTexture("media/particle.bmp"));
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);
}
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.