shoot

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.
GameGuru

shoot

Post by GameGuru »

I know this is a ( probably ) question that has been asked a thousand times, but i always find like tenthousands of topics when i search and none is useable, so i ask this: how can i implement shooting?
I know this is in the techdemo, but i don't get that code and i would really want to undersand my entire code. Please help me.
SARIN
Posts: 139
Joined: Fri Oct 29, 2004 3:53 am

Post by SARIN »

simple way to do shotting (im not posting any code, ill just explain0
-make a function shoot() that returns a vector3df
-in the function:
--create a line from the camera to the end of the camera veiw (target)
--getCollisionPoint, if there is a collision with the object then do damage (u will need an individual tri selector for each shootable object)
--return the position of the collision
-out of function:
--just call shoot() when the mouse is pressed
--if u have physics or particle systems, use the vector3df, otherwise u really dont need it unless u have some other use for the collision position

this is what the techdemo does, just follow the code for it, ignore the particle and sound systems for it to understand easier
ijo coim
Posts: 57
Joined: Fri Mar 25, 2005 1:29 pm
Location: indonesia, jogja-jogja

emmm..i still can't make it

Post by ijo coim »

i'd try this code to make shoot :

Code: Select all

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

using namespace irr;

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

class EventReciever:public IEventReceiver 
{ 
	public: 
   virtual bool OnEvent(SEvent Event) 
   { 
      

      if(Event.KeyInput.Key==KEY_SPACE) 
      { 
         shoot(); 
      } 
      
      return false; 
   } 
}; 

	video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
	EventReciever reciever; 

	IrrlichtDevice *device =createDevice(driverType, core::dimension2d<s32>(640,480),16,false,false);
	scene::ITriangleSelector* mapSelector;
	
	struct SParticleImpact
	{
		u32 when;
		core::vector3df pos;
		core::vector3df outVector;
	};

int main()
{

	
	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	
	device->getFileSystem()->addZipFileArchive("map-20kdm2.pk3");
	
	scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
	scene::ISceneNode* q3node = 0;
	
	if (q3levelmesh)
		q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));

	scene::ITriangleSelector* selector = 0;
	
	if (q3node)
	{		
		q3node->setPosition(core::vector3df(-1370,-130,-1400));

		selector = smgr->createOctTreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
		q3node->setTriangleSelector(selector);
		selector->drop();
	}
	
	scene::ICameraSceneNode* camera = 
		smgr->addCameraSceneNodeFPS(0,100.0f,300.0f);
	camera->setPosition(core::vector3df(-100,50,-150));

	scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
		selector, camera, core::vector3df(30,50,30),
		core::vector3df(0,0,0), 
		core::vector3df(0,50,0));
	camera->addAnimator(anim);
	anim->drop();
	
	device->getCursorControl()->setVisible(false);

	// add billboard

	scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
	bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
	bill->setMaterialTexture(0, driver->getTexture("particle.bmp"));
	bill->setMaterialFlag(video::EMF_LIGHTING, false);
	bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));

	// add 3 animated faeries.

	video::SMaterial material;
	material.Texture1 = driver->getTexture("faerie2.bmp");
	material.Lighting = true;

	scene::IAnimatedMeshSceneNode* node = 0;
	scene::IAnimatedMesh* faerie = smgr->getMesh("faerie.md2");

	if (faerie)
	{
		node = smgr->addAnimatedMeshSceneNode(faerie);
		node->setPosition(core::vector3df(-70,0,-70));
		node->setMD2Animation(scene::EMAT_RUN);
		node->getMaterial(0) = material;

		node = smgr->addAnimatedMeshSceneNode(faerie);
		node->setPosition(core::vector3df(-70,0,-50));
		node->setMD2Animation(scene::EMAT_SALUTE);
		node->getMaterial(0) = material;

		node = smgr->addAnimatedMeshSceneNode(faerie);
		node->setPosition(core::vector3df(-70,0,-60));
		node->setMD2Animation(scene::EMAT_JUMP);
		node->getMaterial(0) = material;		
	}

	material.Texture1 = 0;
	material.Lighting = false;

	// Add a light

	smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
		video::SColorf(1.0f,1.0f,1.0f,1.0f),
		600.0f);

	scene::ISceneNode* skyboxNode = 0;

		skyboxNode = smgr->addSkyBoxSceneNode(
		driver->getTexture("irrlicht2_up.jpg"),
		
		driver->getTexture("irrlicht2_dn.jpg"),
		
		driver->getTexture("irrlicht2_lf.jpg"),
		
		driver->getTexture("irrlicht2_rt.jpg"),
		
		driver->getTexture("irrlicht2_ft.jpg"),
		
		driver->getTexture("irrlicht2_bk.jpg"));


	scene::ISceneNode* selectedSceneNode = 0;
	scene::ISceneNode* lastSelectedSceneNode = 0;
	
	int lastFPS = -1;

	while(device->run())
	if (device->isWindowActive())
	{
		driver->beginScene(true, true, 0);

		smgr->drawAll();

		core::line3d<f32> line;
		line.start = camera->getPosition();
		line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;

		core::vector3df intersection;
		core::triangle3df tri;

		if (smgr->getSceneCollisionManager()->getCollisionPoint(
			line, selector, intersection, tri))
		{
			bill->setPosition(intersection);
				
			driver->setTransform(video::ETS_WORLD, core::matrix4());
			driver->setMaterial(material);
			driver->draw3DTriangle(tri, video::SColor(0,255,0,0));
		}		

		selectedSceneNode = smgr->getSceneCollisionManager()->getSceneNodeFromCameraBB(camera);

		if (lastSelectedSceneNode)
			lastSelectedSceneNode->setMaterialFlag(video::EMF_LIGHTING, true);

		if (selectedSceneNode == q3node || selectedSceneNode == bill)
			selectedSceneNode = 0;

		if (selectedSceneNode)
			selectedSceneNode->setMaterialFlag(video::EMF_LIGHTING, false);

		lastSelectedSceneNode = selectedSceneNode;

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
		  core::stringw str = L"Collision detection example - Irrlicht Engine [";
		  str += driver->getName();
		  str += "] FPS:";
		  str += fps;

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

	device->drop();
	
	return 0;
}


void shoot()
{
	scene::ISceneManager* smgrShoot = device->getSceneManager();
	scene::ICameraSceneNode* camShoot = smgrShoot->getActiveCamera();
	
	if(!camShoot||!mapSelector)
		return;

	SParticleImpact imp;
	imp.when=0;

	//get line of camera

	core::vector3df start=camShoot->getPosition();
	core::vector3df end = (camShoot->getTarget()-start);
	end.normalize();
	start+=end*5.0f;
	end=start + (end * camShoot->getFarValue());
	
	core::triangle3df triangle;
	core::line3d<f32>line(start,end);

	//get intersection point with map
	if(smgrShoot->getSceneCollisionManager()->getCollisionPoint(line, mapSelector,end,triangle))
	{
		//collides with wall
		core::vector3df out = triangle.getNormal();
		out.setLength(0.03f);

		imp.when = 1;
		imp.outVector = out;
		imp.pos = end;
	}
	else
	{
		//doens't collision with wall
		end=(camShoot->getTarget()-start);
		end.normalize();
		end=start+(end*1000);
	}

	//create fire ball
	scene::ISceneNode* nodeShoot=0;
	nodeShoot=smgrShoot->addBillboardSceneNode(0,core::dimension2d<f32>(25,25),start);
	nodeShoot->setMaterialFlag(video::EMF_LIGHTING,false);
	nodeShoot->setMaterialTexture(0,device->getVideoDriver()->getTexture("fireball.bmp"));
	nodeShoot->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);	

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


}


i got this error :

Code: Select all

Compiling...
shoot_1.cpp
G:\3 dimensin\SEMUA IRRLICHT\irlicht hancurrr\irlicht asli neeehhh\irrlicht-0.8\examples\LATIHANKU\Latihan\shoot_1.cpp(18) : error C2065: 'shoot' : undeclared identifier
G:\3 dimensin\SEMUA IRRLICHT\irlicht hancurrr\irlicht asli neeehhh\irrlicht-0.8\examples\LATIHANKU\Latihan\shoot_1.cpp(229) : error C2373: 'shoot' : redefinition; different type modifiers
Error executing cl.exe.
shoot_1.obj - 2 error(s), 0 warning(s)

in begineer forum, there are 73 pages, every page there are 50 threads, but i only got 6 threads that talk about shoot.

is there any solutions for me? tech demo didn't work for me.

thanks for your attention

regards,

ijo coim
hybrid

Post by hybrid »

You should move your shoot method to the top of your file in order to let the EventReceiver know about this method before it gets called. or just put a

Code: Select all

void shoot();
forward declaration to the top of your file. Then it's ok to have tihe implementation at then end.
ijo coim
Posts: 57
Joined: Fri Mar 25, 2005 1:29 pm
Location: indonesia, jogja-jogja

YiiiiiiiiHaaaaaaa

Post by ijo coim »

yiiihaaaa.....it work.

thanks.
i am 0 in cpp.

but, when i press space key, nothing is happen. there isn't shoot. (nothing is happen)

maybe i wrong with my code.

anyway, thanks fory your quick reply.

regards,

ijo coim
ijo coim
Posts: 57
Joined: Fri Mar 25, 2005 1:29 pm
Location: indonesia, jogja-jogja

why ?

Post by ijo coim »

emmm... i'd put all the code for void shoot() (i got it from tech demo). but when i execute it, nothing is happen.

is there any suggests?

thanks b4.

regards,

ijo coim
ijo coim
Posts: 57
Joined: Fri Mar 25, 2005 1:29 pm
Location: indonesia, jogja-jogja

why ?

Post by ijo coim »

emmm... i'd put all the code for void shoot() (i got it from tech demo). but when i execute it, nothing is happen.

is this because these codes?

Code: Select all

scene::ISceneManager* smgrShoot = device->getSceneManager(); 
   scene::ICameraSceneNode* camShoot = smgrShoot->getActiveCamera(); 
is there any suggests?

thanks b4.

regards,

ijo coim
ijo coim
Posts: 57
Joined: Fri Mar 25, 2005 1:29 pm
Location: indonesia, jogja-jogja

Post by ijo coim »

hello forum,

emmm...is there any help?
SARIN
Posts: 139
Joined: Fri Oct 29, 2004 3:53 am

Post by SARIN »

hey, someone revived this thing? here might be ur problem, ur not passing the event reciever into the device

Code: Select all

   IrrlichtDevice *device =createDevice(driverType, core::dimension2d<s32>(640,480),16,false,false);
thats wat u did, this is wat u should do

Code: Select all

   IrrlichtDevice *device =createDevice(driverType, core::dimension2d<s32>(640,480),16,false,false,false,this);
that might explain it
ijo coim
Posts: 57
Joined: Fri Mar 25, 2005 1:29 pm
Location: indonesia, jogja-jogja

Post by ijo coim »

when i write the code, it give an error:

Code: Select all

G:\3 dimensin\SEMUA IRRLICHT\irlicht hancurrr\irlicht asli neeehhh\irrlicht-0.8\examples\LATIHANKU\Latihan\shoot_1.cpp(31) : error C2355: 'this' : can only be referenced inside non-static member functions
then i write the code like this :

Code: Select all

IrrlichtDevice *device =createDevice(driverType, core::dimension2d<s32>(640,480),16,false,false,false, &reciever);
it still wrong.

SARIN, thanks for your reply, any sugestions?
SARIN
Posts: 139
Joined: Fri Oct 29, 2004 3:53 am

Post by SARIN »

try putting it with the console window in the background. next, instead of the function shoot() when a key is pressed, addto it with "printf("shoot");". this way u can at least see if its getting the keypress
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

Hello Mr. noob that doesn't understand the techdemo but calls himself a gameguru...

you can't expect us to wave a magic wand and instantly teach you how to program.

do like I did spend months deciphering the code taking bit by bit and understanding it fully and combining what you've learned to create your own project.

try downloading guice maybe my source will explain a few things.

whats next on this route? once you can shoot what will you ask the community to code for you next?

I'm sorry if this sounds shrewd but it's the only way you are going to learn anything.
Sol
Posts: 8
Joined: Wed Apr 13, 2005 1:13 am
Location: Louisiana, USA
Contact:

Post by Sol »

He's right. Asking people to do it for you won't teach you anything, and will surely give yourself a bad name.
People here help other people-keyword help. Keep in mind that these last few posts are to give advice. Take it.
ijo coim
Posts: 57
Joined: Fri Mar 25, 2005 1:29 pm
Location: indonesia, jogja-jogja

thanks guys

Post by ijo coim »

Midnight and Sol, thanks for your reply. dou you know, your sentences give me more spirit to learn more and more.

last night, i only sleep for 2 hours, then i go to my college, i learned irrlicht.

now, i'll try to solve this by my self. give me some day, and i'll try to find where is the mistake. if i didn't find the way, i'll find the other guide, like in afecelis site:
http://www.danielpatton.com/afecelis/

i'd download much file (except the big, maybe than 8 mb).

any way, thanks for these. but, all of you guys, keep promise to help us (the newbie) in irrlicht.

to all irrlicht newbie, this is could be your suggestion.

::.keep programming.::

regards,

ijo coim
ijo coim
Posts: 57
Joined: Fri Mar 25, 2005 1:29 pm
Location: indonesia, jogja-jogja

Post by ijo coim »

oopppss..i almost forget, i am not GameGuru, i don't know who is GameGuru, as long as i know, GameGuru is the first person who start this thread (shoot).

hey, is there any link for a noob? (maybe there are some files that can help us (the noob) to increase our knowledge?)

thanks for your attention

regards,

ijo coim
Post Reply