Page 1 of 1

Use a special SceneNode type for cars?

Posted: Thu Apr 01, 2004 10:49 am
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)?

Posted: Fri Apr 02, 2004 11:22 am
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.

Posted: Fri Apr 02, 2004 11:39 am
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

Posted: Fri Apr 02, 2004 11:51 am
by Homer
Thank you very very much! That works great!

Posted: Fri Apr 02, 2004 3:34 pm
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

Posted: Sat Apr 03, 2004 3:12 am
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)

Posted: Sat Apr 03, 2004 8:31 am
by Homer
Thanks Domarius, that's surely a possible solution. I'll try this out when I don't find an other way.