Problem with object rotation

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
siny
Posts: 10
Joined: Fri Apr 20, 2007 11:59 am

Problem with object rotation

Post by siny »

Hi,

I'am writing a simple simulation of cannon using irrlicht and newton dynamics. The problem is, i can't rotate it (lift up or down) I can make the gun to move with the cat:

Code: Select all

void _cdecl PhysicsCarSetTransform(const NewtonBody *body, const dFloat *matrix)
{
    matrix4 mat;
    memcpy(mat.pointer(), matrix, sizeof(float)*16);

    CPhysicsCar *car = (CPhysicsCar *)NewtonBodyGetUserData(body);
    car->node->setPosition(mat.getTranslation());
    car->node->setRotation(mat.getRotationDegrees());
    ////////////////////////////////////////////////////////////////////////////
    if(cannonnode){         
        vector3df RelativeToCar(-41.7,24.5,0); 
        ((ISceneNode*)carnode)->getAbsoluteTransformation().transformVect(RelativeToCar);         
        cannonnode->setPosition(RelativeToCar);  

        // !! this is the place where i set gun rotation
        cannonnode->setRotation(mat.getRotationDegrees());
        
                  
    }
   	if(attachCamera){
        vector3df RelativeToCar(200,100,0); 
        ((ISceneNode*)carnode)->getAbsoluteTransformation().transformVect(RelativeToCar); 
        actualCamera->setPosition(RelativeToCar); 
        actualCamera->setTarget(carnode->getAbsolutePosition());             
    }
    ////////////////////////////////////////////////////////////////////////////
    
    
    // also need to set transform for the tyres
    void* wheelid = 0;
    for(wheelid = NewtonVehicleGetFirstTireID(car->vehicle); wheelid; wheelid = NewtonVehicleGetNextTireID(car->vehicle, wheelid))
    {
        CPhysicsCarWheel *wheel = (CPhysicsCarWheel *)NewtonVehicleGetTireUserData(car->vehicle, wheelid);
        NewtonVehicleGetTireMatrix(car->vehicle, wheelid, &mat.pointer()[0]);
        wheel->node->setPosition(mat.getTranslation());
        wheel->node->setRotation(mat.getRotationDegrees());
    }
	if (reset)
	{
		NewtonVehicleReset(car->vehicle);
		dMatrix matrix (GetIdentityMatrix());
		vector3df terrPos= terrain->getTerrainCenter();
	    matrix.m_posit.m_x = terrPos.X;
	    matrix.m_posit.m_y = terrPos.Y+900;
	    matrix.m_posit.m_z = terrPos.Z;
	
		NewtonBodySetMatrixRecursive(car->carbody,&matrix[0][0]);
	}
}
But this only makes the gun to move with the cart. When I try to rotate it in Y axis e.g. like this:

Code: Select all

    
        vector3df RelativeToCar(0,30,0); 
        ((ISceneNode*)carnode)->getAbsoluteTransformation().rotateVect (RelativeToCar);         
        cannonnode->setRotation(RelativeToCar); 
It starts to move in different directions. I understand that i miss something in math with this but i don't know what.

Thanks for advices. And sory for my english:D
Catprog
Posts: 164
Joined: Wed Jan 31, 2007 9:07 am
Contact:

Post by Catprog »

Are you using 1 or 2 models for the cannon?
siny
Posts: 10
Joined: Fri Apr 20, 2007 11:59 am

Post by siny »

2 models: cart and cannon gun. I treat cart as Newton Dynamics Car, and cannon gun is only a Irrlicht Mesh
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

Have i got this right - you want your gun node follow you car, and be able to rotate it relative to the car?

Then you could make the gun a child of the car, and set the guns rotation to the relative value. A parents rotation, translation and scale is transfered down to the childs, and the childs can have their own values, which gets summed up, sortof.[/code]
If you don't have anything nice to say, don't say anything at all.
siny
Posts: 10
Joined: Fri Apr 20, 2007 11:59 am

Post by siny »

Ok that sounds nice, but could you show me some code that makes child object?? becouse I'am new to irrlicht and i don't know how to do that.

Only question is, isn't it the same as:

Code: Select all

        vector3df RelativeToCar(0,30,0); 
        ((ISceneNode*)carnode)->getAbsoluteTransformation().rotateVect (RelativeToCar);          
        cannonnode->setRotation(RelativeToCar);
becose it won't work. And it also counts vector relative to car.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

ISceneNode::setParent(parentSceneNode);
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

Code: Select all

ISceneNode* TheCarNode;
IAnimatedMesh* TheGunMesh;

//Create the gun node as a child to the car node
ISceneNode* TheGunNode = SceneManager->addAnimatedMeshSceneNode(TheGunMesh, TheCarNode);
or create as you have done already, but after creation add

Code: Select all

TheCarNode->addChild(TheGunNode);
Either way, to rotate your gun, do this

Code: Select all

TheGunNode->setRotation(45,0,0); //Rotate 45 degrees around the x axis
and it will rotate relative to the car.
If you don't have anything nice to say, don't say anything at all.
siny
Posts: 10
Joined: Fri Apr 20, 2007 11:59 am

Post by siny »

Thankks. That works just fine.
Post Reply