Mesh won't rotate on a certain axis??

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
joe1234
Posts: 8
Joined: Mon Jun 22, 2009 6:09 pm

Mesh won't rotate on a certain axis??

Post by joe1234 »

There seems to be some issue with rotating a child mesh... Specifically a first person gun mesh. The way I have my FPS camera set up right now is like this: there's a playerHead node which parents the camera, cameraTarget, and playerWeapon mesh. Now looking around and everything works fine; all of playerHead's children follow its rotation and position just fine. But when I try to set a specific rotation for my gun mesh, I can only change its Y and Z rotation. If I try setting its X rotation... it just modifies the Z rotation. If I set both X and Z rotation, it actually adds the values together to rotate on the Z axis! What am I doing wrong? I've set my camera up a few different ways and its always the same. Rotations work fine when a mesh isn't a child, but as soon as I make it a child, its just unable to rotate on the X axis for some reason.

Help :(
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

If I`m not mistaken, when using a child all its transformations (position, rotation, scale) are relative to its parent`s, which in 99% is what we need, but probably this is causing you trouble. You can always get your head node`s position, rotation etc. and use them to position and rotate your weapon or whatever without using child&parent relationship. Just apply the transformations you need to your weapon after you`re done with the head node. Eventually (after you tried doing this yourself ) you can post some code, because there`s a plenty of things you potentially may be doing wrong. Sometimes it`s a good idea to stop investigating using the trial-and-failure method and try to figure out what you are doing. :wink:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
joe1234
Posts: 8
Joined: Mon Jun 22, 2009 6:09 pm

Post by joe1234 »

shadowslair wrote:If I`m not mistaken, when using a child all its transformations (position, rotation, scale) are relative to its parent`s, which in 99% is what we need, but probably this is causing you trouble.
Nooo no no, that's what I'm wanting. What I'm trying to do is make the weapon aim just a bit downward instead of straight forward from the head/camera's point of view. But it seems that I can only roll the weapon, or rotate it to the left or right. Up and down just doesn't seem to work.

Code: Select all

	// Init variables
	IAnimatedMesh* mesh = smgr->getMesh("stupidgun (2).3ds");
	IAnimatedMesh* mesh2 = smgr->getMesh("cube.3ds");
	ICameraSceneNode* cam = smgr->addCameraSceneNode(0, vector3df(0,0,-20), vector3df(0,0,0));
	IAnimatedMeshSceneNode* weapon = smgr->addAnimatedMeshSceneNode(mesh);
	IAnimatedMeshSceneNode* playerHead = smgr->addAnimatedMeshSceneNode(mesh2);
	IAnimatedMeshSceneNode* camTarget = smgr->addAnimatedMeshSceneNode(mesh2);

	// Set parents
	cam->setParent(playerHead);
	weapon->setParent(playerHead);
	camTarget->setParent(playerHead);

	// Set some other stuff
	if (weapon) weapon->setMaterialTexture(0, driver->getTexture("derp.bmp"));
	playerHead->setPosition(vector3df(10,10,10));
	playerHead->setRotation(vector3df(0,0,0));
	camTarget->setPosition(vector3df(0,0,100));
	cam->setPosition(vector3df(0,0,5));
	cam->setTarget(camTarget->getAbsolutePosition());
	weapon->setPosition(vector3df(6,-3,20));
	weapon->setRotation(vector3df(0,-90,0)); // <--- ***This is the problem***
	smgr->addLightSceneNode(0, vector3df(0,0,0), SColor(0,1,0,0), 800);
	float mouse_sens = 0.1f;

	// Game loop *****
	while(device->run())
	{
		vector3df position = playerHead->getPosition();
		vector3df rotation = playerHead->getRotation();
		if (keyboard.IsKeyDown(KEY_KEY_W)) position.Z += 0.02f;
		if (keyboard.IsKeyDown(KEY_KEY_S)) position.Z -= 0.02f;
		if (keyboard.IsKeyDown(KEY_KEY_D)) position.X += 0.02f;
		if (keyboard.IsKeyDown(KEY_KEY_A)) position.X -= 0.02f;
		if (keyboard.IsKeyDown(KEY_UP)) rotation.X -= mouse_sens;
		if (keyboard.IsKeyDown(KEY_DOWN)) rotation.X += mouse_sens;
		if (keyboard.IsKeyDown(KEY_RIGHT)) rotation.Y += mouse_sens;
		if (keyboard.IsKeyDown(KEY_LEFT)) rotation.Y -= mouse_sens;
		if (rotation.X < -89.9f) rotation.X = -89.9f;
		if (rotation.X > 89.9f) rotation.X = 89.9f;
		playerHead->setPosition(position);
		playerHead->setRotation(rotation);
		cam->setTarget(camTarget->getAbsolutePosition());
		driver->beginScene(true, true, SColor(255,140,255,255));
		smgr->drawAll();
		guienv->drawAll();
		driver->endScene();
	}
So my problem is if I change weapon's X or Z rotation value (after making it a child of playerHead), it just rolls, and the Y value rotates it left and right. But I want up and down. (And this is all based off the camera's viewpoint)

:?::?::?::?::?:
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

Too lazy reading and thinking, but here are a few quick guesses:

1) If all you need is to position and rotate your weapon in the beginning and don`t apply any local transformations any further, why don`t you simply position and rotate your weapon model from within your modelling program?

2) Try rotating your weapon before making it child of the head.

3) If you use "weapon->setRotation(vector3df(0,-90,0));" just to make your weapon point forward, it`s much better to do this in the modelling program, because you`re changing its orientation axes. Keep in mind that Irrlicht and every other engine rotates a node first on X, second on Y, and last on Z. Probably this is causing you trouble.

Hope this helps.
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

IIRC, Irrlicht rotates in the order Z, Y, X.
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

Oops, my mistake. I kinda figured this out 6 months ago when I had to change the rotation axes` sequence getting proper realtime rotations using some 3ds Max biped bone coordinate systems.
All I tried to point out was that rotating a mesh 90 degrees this way affects it`s rotations around the other axes. I think this is what I get for trying way too many eninges for the last years. :)
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
joe1234
Posts: 8
Joined: Mon Jun 22, 2009 6:09 pm

Post by joe1234 »

Well, that appears to have fixed it. I changed its rotation in Blender and re-exported it; it seems to be working now. Though I'm still confused as to why I had a problem in the first place. I mean why would spinning it -90 degrees in one direction essentially break the other two directions? Its as if one direction was relative and the other was absolute... :?!

Oh well, it works for now. Thanks for the help guys! Appreciate it! :D
Post Reply