# Moving head bone with mouse, need to create a view matrix

This topic is 2207 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am controlling the skinned meshs head with the mouse, what I want todo next is press a key and switch to the view the head is looking at and build the look,up,right vectors to work with D3DXMatrixLookAtLH

The below code is not mine,its from Character Animation with Direct3D which is a great book , I just want to make a mod so when I move the head with mouse I am controlling what he is looking at

Here is a video for demonstration purposes

My guess would be I need to covert this matrix , rotation * m_pHeadBone->CombinedTransformationMatrix; to the 3 vector paramaters for the Lookat function,thanks for reading..

#include "InverseKinematics.h"
#include "skinnedMesh.h"
// Setup forward vectors and get bone pointers for our example
InverseKinematics::InverseKinematics(SkinnedMesh* pSkinnedMesh)
{
m_pSkinnedMesh = pSkinnedMesh;
// Exit if there is no head bone
{
// Calculate the local forward vector for the head bone
// Remove translation
return;
// Our model is looking towards -z in the content
D3DXVECTOR4 vec;
}
}
{
// Exit if there is no head bone
return;
//Get look at point from cursor location
POINT p;
GetCursorPos(&p);
p.x = max(min(p.x, 800), 0);
p.y = max(min(p.y, 600), 0);
float x = (p.x - 400) / 800.0f;
float y = (p.y - 300) / 600.0f;
D3DXVECTOR3 lookAt(x, 1.5f - y, -1.0f);
// Restrict head movement to 60 degrees
}
// Uses shortest arc rotation algorithm to turn the head bone towards a position
void InverseKinematics::ApplyLookAtIK(D3DXVECTOR3 &lookAtTarget, float maxAngle)
{
// Start by transforming to local space
D3DXMATRIX mtxToLocal;
// Footnote: the matrix inverse could be avoided if we would ignore to update the local transform
D3DXVECTOR3 localLookAt;
D3DXVec3TransformCoord(&localLookAt, &lookAtTarget, &mtxToLocal );
// Normalize local look at target
D3DXVec3Normalize(&localLookAt, &localLookAt);
// Get rotation angle and angle
D3DXVECTOR3 localRotationAxis;
D3DXVec3Normalize(&localRotationAxis, &localRotationAxis);
// Limit angle
localAngle = min( localAngle, maxAngle );
// Apply the transformation to the bone
D3DXMATRIX rotation;
D3DXMatrixRotationAxis(&rotation, &localRotationAxis, localAngle);
// Update changes to child bones
{
}
}

Edited by Anddos

##### Share on other sites

You can use the inverse of the m_pHeadBone matrix as a view matrix. This matrix lets you transform a scene into the local space of the head(-camera), as you effectively always do when multiplying by a view matrix.

##### Share on other sites

but what about the 3 params for D3DXMatrixLookAtLH

##### Share on other sites

"Character Animation with Direct3D" is a great book as introduction, I have it.

I said introduction because it uses D3DX so he doesn't go deep of how it is done.

Inverse of a world matrix is always a view matrix, so just inverse the world matrix of the bone, the combined is what you want (local * parentWorld).

You don't need to make a "look at" matrix, you only need to inverse the combined and use it as view matrix.

Edited by Alundra

##### Share on other sites

g_pDevice->SetTransform(D3DTS_VIEW, &m);

so it would be something like this ? , I cant think how I am going to switch the view matrix now , like enalble and disable, I know its with bools , dosent the settransform have to go in the rendering loop?

##### Share on other sites

You can use boolean to switch the view or you can store an array of view and a number who says the current view.

You have to check the input key used for the switch. When it's pressed, adds one or subtracts one.

You have to check the number limits (0 -> MaxView). "SetTransform" must be set in the rendering.

• 13
• 18
• 29
• 11