Thread: How to find relative translation...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    How to find relative translation...

    My question is about the math involved in scene graphs and how they determine relative translations..

    Say I have a node in my tree that is the terrain, positioned at 0, 0, 0.

    What mathematical equation do I use to find the transformation variables for a dwarf which is located relatively to the above node...

    I know you have to take the terrains transformation matrix, which should read out 0, 0, 0 to gltranslate... but do I add the dwarf's transformation matrix to find his relative location, or multiply it?

    I havn't taken any math classes in over 2 years and I admit I'm a bit rusty with my algebra.

    I'll show you the code I have...

    Code:
    class CDOFNode : public CSceneNode
    {
    public:
    
    	CDOFNode() 
    	{
    	}
    
    	~CDOFNode() { }
    
    	void Update()
    	{
    
    		glPushMatrix();
    		this->FinalMatrix = LocalMatrix * ParentNode->FinalMatrix;
    		glMultMatrixf(this->FinalMatrix.readArray());
    
    		CSceneNode::Update();
    		glPopMatrix();
    	}
    
    	void Initialize( Vector4 translation) // Updates local matrix
    	{
    		LocalMatrix.Translation(translation);
    	}
    
    private:
    
    	Matrix4 LocalMatrix;
    };
    This doesn't work at all, seeing as how when my program runs I get polygons all over the screen, kinda like a kaleidoscope. I ran the debug and it's obvious that my FinalMatrix is not what it should be.

    Any help?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think you are confusing matrix chains for use in bone and skeletal systems with transformation matrices used for positioning objects. If you want to position an object at x,y,z then you create a translation matrix that will translate the object to x,y,z.

    For D3D:
    1,0,0,x
    0,1,0,y
    0,0,1,z
    0,0,0,1

    Code:
    void CGameObject::TransformObject(D3DXVECTOR3 vecWorldPos,
                                                               D3DXVECTOR3 vecScale,
                                                               D3DXVECTOR3 vecLocalRot)
                                                              
    {
    
      //Setup matrix variables
      D3DXMATRIX matWorld,matTrans,matLocalRot,matScale; 
    
      //Setup model space or local rotation matrix
      D3DXMatrixRotationYawPitchRoll(&matLocalRot,vecLocalRot.x,vecLocalRot.y,vecLocalRot.z);
    
      //Setup scaling matrix
      D3DXMatrixScale(&matScale,vecScale.x,vecScale.y,vecScale.z);
    
      //Setup translation matrix  (moves object to its world position)
      D3DXMatrixTranslation(&matTrans,vecWorldPos.x,vecWorldPos.y,vecWorldPos.z);
    
    
      //Create final world matrix
      matWorld=matScale*matLocalRot*matTrans);
    
      //Use shared D3D device pointer to set world transform
      m_spDevice->SetTransform(D3DTS_WORLD,&matWorld);
    }
    This method is not foolproof as it does not take into account gimbal lock. A more correct approach would either be to use axis-angle rotations or quaternions to avoid the gimbal lock issue. In my system I have a special class called COrient3D which is responsible for all 3D orientations and representations. Every object has it's own COrient3D object which makes handling rotations centralized to one particular class.

  3. #3
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    For D3D:
    1,0,0,x
    0,1,0,y
    0,0,1,z
    0,0,0,1
    And for GL, remember to transpose that matrix.

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Yeah um, I know what a translation matrix looks like, but what I want to know is.

    How do I find another matrices relative location to a matrix?

    Like lets say we have our orc model, he is carrying a battle axe that is relative in translation to the orcs hand. Is this an addition operation? Or a multiplication operation?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Like lets say we have our orc model, he is carrying a battle axe that is relative in translation to the orcs hand. Is this an addition operation? Or a multiplication operation?
    Multiplication. Not a whole lot can be done by adding matricies.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You would find the location of the orcs hand in model space relative to 0,0,0. Translate the axe by that amount in model space, transform anything needed in model space, and then transform to world space. If you translate the axe to the center of the character in world space, or the character's position, the axe will be in the correct position.

    How to implement this is up to you. If you can throw the axe or the weapons then doing it this way does not provide a good way to unchain objects from their parents. So I would create a matrix stack. The local translation matrix for the objects would be the location of the orcs hand in model space relative to 0,0,0. Once you transform the character then all you need to do is transform the axe using the model space matrix and concatenate it with the world matrix of the character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  3. ld.exe: cannot find -l-lstdc++
    By Tonto in forum Tech Board
    Replies: 3
    Last Post: 04-10-2007, 11:20 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM