Rotating a sceneNode...

Discussion about everything. New games, 3d math, development tips...
Post Reply
jordi
Posts: 2
Joined: Tue Jun 12, 2007 10:53 am

Rotating a sceneNode...

Post by jordi »

Hello to the community, I need your help:
I have programmed with IrrLicht about few weeks and I am satisfied with the 3d engine. Now I have a problem:

I have to compute three angles alfa,beta,gamma for make an a rotacion:

I have 2 vectors: (0,1,0) and (x,y,z).

My desire is rotate vector (0,1,0) and transform it to normalized vector (x,y,z). I will use this code for rotate a mesh of an sceneNode.

ISceneNode have a function <setRotacion>:
virtual void irr::scene::ISceneNode::setRotation ( const core::vector3df &rotationdegrees)

I need to pass the rotation <rotationdegrees> of the three euler angles for rotating (0,1,0) and accomplish the vector (x,y,z).
I tried to calculate with the atan2 function of the three planes and call <setRotation>, but I didn't accomplish my objective in many cases.
Here are my code:

Code: Select all

		v3d0=vector3df(0,1,0);	// v3d0=vector (0,0,0)
		float alfa0,beta0,gamma0;

		if (v3d0.Y >= 0) alfa0=atan2f(v3d0.Y, v3d0.X);
		else alfa0=6.28+atan2f(v3d0.Y, v3d0.X);

		if (v3d0.X >= 0) beta0=atan2f(v3d0.X, v3d0.Z);
		else beta0=6.28+atan2f(v3d0.X, v3d0.Z);

		if (v3d0.Y >= 0) gamma0=atan2f(v3d0.Y, v3d0.Z);
		else gamma0=6.28+atan2f(v3d0.Y, v3d0.Z );

		//---------------------------------------------------------------------
		float alfa,beta,gamma;

		v3d=posEsf[i+1]-posEsf[i]; // v3d=vector (x,y,z)
		if (v3d!=vector3df(0,0,0)) v3d.normalize(); 

		if (v3d.Y >= 0) alfa=atan2f(v3d.Y, v3d.X);
		else alfa=6.28+atan2f(v3d.Y, v3d.X);

		if (v3d.X >= 0) beta=atan2f(v3d.X, v3d.Z);
		else beta=6.28+atan2f(v3d.X, v3d.Z);

		if (v3d.Y >= 0) gamma=atan2f(v3d.Y, v3d.Z);
		else gamma=6.28+atan2f(v3d.Y, v3d.Z );

		// make angles
 		v3dAngles=vector3df(gamma-gamma0,beta0-beta,alfa0-alfa);	
		v3dAngles *= 360/6.28; // radians to degrees
		node->setRotation(v3dAngles); // but the mesh rotate different that I hope

Can somebody explain to me how to make this?.

Thanks to all users for reading this message.
vi-wer
Posts: 93
Joined: Sun May 20, 2007 7:15 pm
Location: Germany
Contact:

Post by vi-wer »

Irrlicht has matrix4 that can be used for nearly all transformations.

Here's some code that could be useful. Try it to see how it works.

Code: Select all

        matrix4 mat;
        vector3df vec = vector3df(0.0f,0.0f,1.0f);

        mat.makeIdentity();
        mat.buildCameraLookAtMatrixLH( vector3df(0,0,0), // position
                                        vector3df( 0,1,0), // target
                                        vector3df(0,0,1)); // upvector

        mat.rotateVect( vec );
        mat.getRotationDegrees(); // get matrix rotation

        vec.normalize();

        printf( "\n Vector: x=%f y=%f z=%f\n",vec.X,vec.Y,vec.Z);
Target and UpVector mustn't be equal.
You can call getRotationDegrees() to get the rotation.
If setting up LookAt-matrix use vector3df(0,0,0) as position and

Code: Select all

v3d=posEsf[i+1]-posEsf[i];
as target.

Hope that helps. :wink:
Post Reply