Use a special SceneNode type for cars?

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
Homer
Posts: 58
Joined: Tue Nov 18, 2003 7:11 pm
Location: Germany

Use a special SceneNode type for cars?

Post by Homer »

Hi,

I want to implement cars in my game and I think the best way to do this is to make a new SceneNode-type. This new SceneNode should be a child of IAnimatedMeshSceneNode, extended by some variables and some functions for moving and rotating the car etc. I think.
I've already tried to write such a new SceneNode, according to the CustomSceneNode-Tutorial. Here is the code (nothing special yet):

Code: Select all

class CCarSceneNode : public IAnimatedMeshSceneNode
{
	private:
		core::vector3df direction;
		float speed;
	public:
		virtual void setDirection(core::vector3df newDirection)
		{
			direction = newDirection;
		}
		virtual void setSpeed(float newSpeed)
		{
			speed = newSpeed;
		}
		virtual void render()
		{
			IAnimatedMeshSceneNode::render();
			// Do the moving/rotating/... stuff here
		}
};
Now, I wanted to create such a SceneNode with

Code: Select all

CCarSceneNode *carNode = sm->addAnimatedMeshSceneNode(carmesh);
which didn't work, because "addAnimatedMeshSceneNode" returns an IAnimatedMeshSceneNode and not a CarSceneNode. But CarSceneNode is a child of AnimatedMeshSceneNode, so it should be possible to create one out of an AnimatedMeshSceneNode, shouldn't it? I'm a C++ noob and don't know how to do this, please help me.

I've got another question relating the CarSceneNode: Later I want to rotate the cars wheels when it moves. Is it somehow possible to rotate just a part of a SceneNode or do I have to create an animation for this in my modelling program (Milkshape)?
Homer
Posts: 58
Joined: Tue Nov 18, 2003 7:11 pm
Location: Germany

Post by Homer »

OK, I've found out that I need to do a type-cast to get this line of code working:

Code: Select all

CCarSceneNode *carNode = (CCarSceneNode *) sm->addAnimatedMeshSceneNode(carmesh);
But it seems that this doesn't work correctly, because I get an exception when I want to use my CarSceneNodes functions (e.g. carNode->setSpeed(0.5f)). So how do I have to create my node correctly? Any help is highly appreciated.
rincewind
Posts: 35
Joined: Thu Mar 25, 2004 5:28 pm
Location: Germany --> Bonn

Post by rincewind »

hm, i don't think, that this approach will work, because what you get from
addAnimatedMeshSceneNode() is an AnimatedMeshSceneNode and no other type of SceneNode.
You should write your class in this way:

Code: Select all

class CCar {
  private:
  ISceneNode* myNode;
  //what else you need

  public:
  CCar(ISceneNode* pNode, /*other stuff you need to initialize your class*/)  {
  myNode = pNode;
  // do more initializing here

  }
  //other important methods, eg.
  ISceneNode* getMyNode(){
      return myNode;
  }

};
now you can use it:

Code: Select all

CCar myCar(sm->addAnimatedMeshSceneNode(carmesh),/*other stuff*/);
and you can access the SceneNode with

Code: Select all

myCar.getMyNode()
greetings, rincewind
Homer
Posts: 58
Joined: Tue Nov 18, 2003 7:11 pm
Location: Germany

Post by Homer »

Thank you very very much! That works great!
Homer
Posts: 58
Joined: Tue Nov 18, 2003 7:11 pm
Location: Germany

Post by Homer »

The class code works great, but I still don't know how to realise the animation of the wheels. My car is a .ms3d model and I've created a joint for each wheel. Now I tried to get a pointer to each wheel with

Code: Select all

wheelFrontLeft = SceneNode->getMS3DJointNode("WheelFL");
(SceneNode is the IAnimatedMeshSceneNode of the car) and to set the rotation of the wheel each time the car gets rendered with

Code: Select all

wheelFrontLeft->setRotation(wheelFrontLeft->getRotation().X + 1.0f, wheelFrontLeft->getRotation().Y + 1.0f, 0.0f));
but nothing happens. I know that the rotation code isn't correct, I just wanted to see if this is the right way. Any suggestions? Thanks in advance
Domarius
Posts: 178
Joined: Thu Mar 11, 2004 9:51 am
Location: Brisbane, QLD, Australia

Post by Domarius »

I have no experience with MS3D objects, but the way I'd tackle this is to make the wheels seperate objects. (Although that's probably not what you wanted to hear)
Homer
Posts: 58
Joined: Tue Nov 18, 2003 7:11 pm
Location: Germany

Post by Homer »

Thanks Domarius, that's surely a possible solution. I'll try this out when I don't find an other way.
Post Reply