node movement in classes

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
Aliendreams
Posts: 3
Joined: Tue Jun 27, 2006 1:09 am
Location: EU

node movement in classes

Post by Aliendreams »

If anyone can at least give me a clue on what I'm missing here, it would be great. I'm trying to learn how to work with c++ classes in Irrlicht and here's my yet half-baked problem:

Code: Select all

class creature  
{
	ISceneNode* creature_node;  //the node 
	vector3df c_pos;	//position of the node

	public:

	  static int c_total;  //num de creatures
	  int state, speed, gravity, BI; //not all are in use yet
	  const static int slot = 1;
	
      // class constructor 
      creature(ISceneNode* creature_node,ISceneManager* smgr,vector3df c_pos,int gravity = 0, int speed = 10,int slot =1,s32 id = -1);
      // class destructor
	  ~creature();
	  void setTarget (vector3df new_target);
	  static int getSlot (); //returns slot creature number
	  bool Update();

};

// class constructor
creature::creature(ISceneNode* creature_node,ISceneManager* smgr,vector3df c_pos,int gravity, int speed,int slot,s32 id)
{
	IAnimatedMesh* mesh = smgr->getMesh("media/robo2.obj");
	creature_node = smgr->addAnimatedMeshSceneNode( mesh );
	
	if (creature_node)
	{
		creature_node->setMaterialFlag(EMF_LIGHTING, true);
		creature_node->setMaterialTexture( 0, driver->getTexture("media/robo2UV.jpg") );
		creature_node->setScale(vector3df(50,50,50));
		creature_node->setMaterialType(EMT_SOLID);
	
		creature_node->setPosition(c_pos);
	
		scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(selector, creature_node,
		vector3df(100,100,100),
		core::vector3df(0,gravity,0),
		core::vector3df(0,0,0));
		creature_node->addAnimator(anim);
		anim->drop();
	}
};

//class destructor
creature::~creature()
{
};

void creature::setTarget (vector3df new_target)  //move the node to a new position
{
	creature_node->setPosition(new_target);  //this crashes everything
};

int creature::getSlot () //returns ID for this creature/node
{
	return slot;
};

bool creature::Update()
{
	stringw str = L"Updating creature in slot ";
	str +=slot;
	device->setWindowCaption(str.c_str());

	return 0;
};
 
Within this function, I'm trying to implement a function for the node movement. The problem is that whenever I try to move the node (with or without animators) from a function in this class, the whole thing falls apart and crashes. I've simplified it to the point of trying a simple change in the position of the node (creature_node->setPosition(new_target);).

I've searched for examples but can't seem to find anything... am I thinking the whole thing wrong?
RapchikProgrammer
Posts: 279
Joined: Fri Dec 24, 2004 6:37 pm

Post by RapchikProgrammer »

I think you are using the class wrongly! Use the debug utility and see if u can find the problem!
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The creature constructor has a parameter named creature_node and then there is the member variable creature_node. When you create the mesh scene node, you save the pointer to it into the temporary that is passed as a function parameter.

Try making this change...

Code: Select all

creature::creature(ISceneNode* parent, ISceneManager* smgr,vector3df c_pos,int gravity, int speed,int slot,s32 id) 
The right thing to do is to use some naming convention for member variables so that you don't accidentally do this kind of thing. The convention used by Irrlicht is to name class member variables with a camel-caps like CreatureNode. Other guys use a leading or trailing underscore for member variables like _creature_node or creature_node_. Yet another solution is to put an m at the beginning of all member variable names like m_creature_node.

Travis
Aliendreams
Posts: 3
Joined: Tue Jun 27, 2006 1:09 am
Location: EU

Post by Aliendreams »

Thx vitek!

I've eliminated all local (unnecessary) nodes and the class now will take an array of global nodes. It works now and it seems simple. Now I'll try getting this node to do some decision-making...


edit: and i'll try sticking to code conventions, but its hard getting into the habit... :? but you're right.
Post Reply