Archived

This topic is now archived and is closed to further replies.

Terence Mok

How wear weapon on 3d model ?

Recommended Posts

Terence Mok    122
Hi everybody, I watched Jim Adams "advanced animation" and RPG book. I want character (e.g.tiny or warrior model) can wear weapon (e.g. sword) with new book engine? But How to do ? Finally, i want know which people migrate RPG ''Tower'' code with new book engine and when Jim will release this code on web site? Thank a lot. It is part of my source code (DoFrame function) Character and weapon can be render, but weapon cannot put on correct position.
void DoFrame()
{
  static DWORD StartTime = timeGetTime();
  DWORD ThisTime = timeGetTime();

  // Update the animation (convert to 30 fps)

  g_Anim.Update(m_AnimName, (ThisTime-StartTime)*3, TRUE);

  // Rebuild the frame hierarchy transformations  

  if(g_Frame)
    g_Frame->UpdateHierarchy();

  // Build the skinned mesh

  UpdateMesh(g_Mesh);

  //Get Character Bone Frame (carry weapon) 

  D3DXFRAME_EX *pFrameAttachWeapon;
  if (g_Frame)
     pFrameAttachWeapon=g_Frame->Find("WeaponHand");
  if (pFrameAttachWeapon!=NULL)
     m_pBoneWeaponMatrices = &pFrameAttachWeapon->matCombined ;

  // Setup world matrix

  D3DXMATRIXA16 matWorld;
  D3DXMatrixTranslation( &matWorld, -m_vObjectCenter.x,
			-m_vObjectCenter.y,
			-m_vObjectCenter.z );
  D3DXMatrixMultiply(&matWorld, &matWorld, m_ArcBall.GetRotationMatrix() );
  D3DXMatrixMultiply( &matWorld,  &matWorld, m_ArcBall.GetTranslationMatrix() );
  g_pD3DDevice->SetTransform( D3DTS_WORLD, &matWorld );

  D3DXVECTOR3 vEye( 0, 0, -2*g_MeshRadius );
  D3DXVECTOR3 vAt( 0, 0, 0 );
  D3DXVECTOR3 vUp( 0, 1, 0 );
  D3DXMatrixLookAtLH( &m_matView, &vEye, &vAt, &vUp);

  // Clear the device and start drawing the scene

  g_pD3DDevice->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0,0,200,255), 1.0f, 0);
  if(SUCCEEDED(g_pD3DDevice->BeginScene())) {

	// Enable lighting

	if (g_Light==TRUE)
		g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
	// Render skinned mesh

	DrawMeshes(g_Mesh);

	// Render Weapon and position it

	if (m_pBoneWeaponMatrices)
	{
		g_pD3DDevice->SetTransform( D3DTS_WORLD,  m_pBoneWeaponMatrices  );
		DrawMeshes(g_WeaponMesh);
	}

	// Enable lighting

	if (g_Light==TRUE)
		g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    // End the scene

    g_pD3DDevice->EndScene();
  }

  // Present the scene to the user

  g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

Share this post


Link to post
Share on other sites
Etnu    880
Normally, you specify (in the model, or possibly procedurally) where the weapon should ''attach'' to the character, and then render the weapon relative to that position (usually you define a weapon''s ''attach point'' as well, since most weapons aren''t held at the center or end).

Sometimes (depending on the complexity of the weapon / item you want the character to hold), you have to specify a unique location for each item.

Share this post


Link to post
Share on other sites
Terence Mok    122
Hi Etnu,

Thank your reply.
I got basic idea. I try got bone frame matrix (e.g weaponhand) and set world position and render it.
Which steps are mistake ? I have no idea now....
But i will work hard to solve this problem.

By the way, I want know which people migrate RPG ''Tower'' code with new book engine and when Jim release this code on web site? hahaha..



Share this post


Link to post
Share on other sites
Terence Mok    122
I solved this problem. Thanks

work case
rendermatrix=bone combined matrix * world transform matrix

no work case ( I don''t know why..)
rendermatrix=world transform matrix * bone combined matrix

work sample code
 
void DoFrame()
{
static DWORD StartTime = timeGetTime();
DWORD ThisTime = timeGetTime();
char text1[256];
// Update the animation (convert to 30 fps)

// comment :update frame(bone) offset transformation matrix

g_Anim.Update(m_AnimName, (ThisTime-StartTime)*3, TRUE);

// Rebuild the frame hierarchy transformations

// comment :update frame(bone) updated matrix=origin*offset transformation matrix

if(g_Frame)
g_Frame->UpdateHierarchy();

// Build the skinned mesh

// comment :update vertex(mesh) with updated transformation matrix

UpdateMesh(g_Mesh);


// Setup world matrix

D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation( &matWorld, -m_vObjectCenter.x,
-m_vObjectCenter.y,
-m_vObjectCenter.z );
D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetRotationMatrix() );
D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetTranslationMatrix() );

D3DXVECTOR3 vEye( 0, 0, -2*g_MeshRadius );
D3DXVECTOR3 vAt( 0, 0, 0 );
D3DXVECTOR3 vUp( 0, 1, 0 );
D3DXMatrixLookAtLH( &m_matView, &vEye, &vAt, &vUp);

D3DXMATRIX matBoneRWorld;
if (m_pBoneWeaponRMatrices)
{ //cannot use matworld*m_pBoneWeaponMatrices

D3DXMatrixMultiply(&matBoneRWorld,m_pBoneWeaponRMatrices,&matWorld );
}
D3DXMATRIX matBoneLWorld;
if (m_pBoneWeaponLMatrices)
{ //cannot use matworld*m_pBoneWeaponMatrices

D3DXMatrixMultiply(&matBoneLWorld,m_pBoneWeaponLMatrices,&matWorld );
}

// Clear the device and start drawing the scene

g_pD3DDevice->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0,0,200,255), 1.0f, 0);
if(SUCCEEDED(g_pD3DDevice->BeginScene())) {

// Enable lighting

if (g_Light==TRUE)
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);

if (m_pBoneWeaponRMatrices)
{

g_pD3DDevice->SetTransform( D3DTS_WORLD, &matBoneRWorld );
DrawMeshes(g_WeaponRMesh);
}
if (m_pBoneWeaponLMatrices)
{
g_pD3DDevice->SetTransform( D3DTS_WORLD, &matBoneLWorld );
DrawMeshes(g_WeaponLMesh);
}


// Render skinned mesh

g_pD3DDevice->SetTransform( D3DTS_WORLD, &matWorld );
DrawMeshes(g_Mesh);

// Enable lighting

if (g_Light==TRUE)
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
// End the scene

g_pD3DDevice->EndScene();
}

// Present the scene to the user

g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:

no work case ( I don''t know why..)
rendermatrix=world transform matrix * bone combined matrix



Matrix multiplication is not commutative. So, as you learned the order of multiplication is important.

Share this post


Link to post
Share on other sites