Shooting on models

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
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Shooting on models

Post by Katsankat »

Hi,

I wrote a class for NPC, that contains a pointer to the node.

When I shoot on NPCs, a simple getSceneNodeFromRayBB() returns a pointer to the
node that was hit. Is there a way to get a pointer to the class instead, to
be able to call its die() method?

Thanks in advance.

Code: Select all

class NPC
{
 NPC::NPC()
 {
  IAnimatedMesh* mesh = smgr->getMesh("models/bandit.b3d");
  node = smgr->addAnimatedMeshSceneNode(mesh);
  if (node)
  {
    node->setScale(vector3df(4,4,4));
    node->setPosition(ontheground(x,z));
    node->setRotation(vector3df(0,angle,0));
    ...
 }
 
 void NPC::die()
 {
  //play sound and animation
 }

 private:
  IAnimatedMeshSceneNode *node;
};


void shoot()
{
 ISceneNode* Victim = sm->getSceneCollisionManager()->getSceneNodeFromRayBB( 
        line3d<f32>(start,end),IS_ENEMY, false);
 if (Victim)
 {
  printf("** BB hit **\n");
  //NPC *x = Victim->getParent();
  //x->die();
 }
}
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

you'll need to store some reference to the NPC object into the node...
for example Newton objects have a void* to store datas you need, but Irrlicht doesn't provide such...

so I can think of some workarounds:
1st - add a void* to the ISceneNode class...
2nd - if you don't need the node's ID you can use it to store an index or maybe the pointer to the NPC object...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Lambda
Posts: 126
Joined: Wed Feb 27, 2008 3:00 pm
Location: Spain, Huelva
Contact:

Post by Lambda »

Acki wrote:you'll need to store some reference to the NPC object into the node...
for example Newton objects have a void* to store datas you need, but Irrlicht doesn't provide such...

so I can think of some workarounds:
1st - add a void* to the ISceneNode class...
2nd - if you don't need the node's ID you can use it to store an index or maybe the pointer to the NPC object...
for example:

Code: Select all

class NPCClass
{
public:
	void Die();
};

struct NPCNode
{
	ISceneNode* pNode;
	NPCClass* pNPCClass;
};

core::array< NPCNode > ActiveNPCList;

//When a NPC is hitted, you do a for in the ActiveNPCList to find the pNPCClass of the hitted mesh.

NPCClass* GetNPCClassOfNode( ISceneNode* pHittedMesh )
{
	for( int i = 0; i != ActiveNPCList.size(); i++ )
	{
		NPCNode Node = ActiveNPCList[i];

		if( Node.pNode == pHittedMesh )
		{
			//Found, return the NPC Class
			return &Node.pNPCClass;
		}
	}

	//NPC not found, return null
	return NULL;
}

void CheckNPCHit()
{
	ISceneNode* Victim = sm->getSceneCollisionManager()->getSceneNodeFromRayBB(
        line3d<f32>(start,end),IS_ENEMY, false); 

	if( Victim )
	{
		NPCClass* pNodeClass = GetNPCClassOfNode( Victim );

		if( pNodeClass )
		{
			//NPC Found
			pNodeClass->Die();
		}
	}
}
Image
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Post by Katsankat »

Thank you very much sirs, it works very well.
Post Reply