Jump to content
  • Advertisement
Sign in to follow this  
VitaliBR

Control Of Bone

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font="arial, sans-serif"]I'm trying to control your character's arm, and make it point to where your cursor is on the screen (so it aim the gun where the mouse)[/font]
[font="arial, sans-serif"] [/font]


[font="arial, sans-serif"]So:[/font]
[font="arial, sans-serif"][/font]void SKINNEDMESH::UpdateMatrices(bool bones, BONE* bone, D3DXMATRIX *parentMatrix)
{
if(bones)
{
if(parentMatrix != NULL)
{
if(bone->Name != NULL && strcmp(bone->Name,"Bip01_L_UpperArm")==0)
{
D3DXMATRIX ident;
D3DXMatrixIdentity(&ident);
D3DXMATRIX final = bone->TransformationMatrix*(*parentMatrix);
D3DXVECTOR3 boneLocation(final._41,final._42,final._43);
D3DXVec3Project(&boneLocation,&boneLocation,&m_vp,&m_pm,&m_vm,&ident); // use YOUR viewport, projection and view matrices in this call
D3DXVECTOR3 dir = mouseLocation - boneLocation;
if(mouseLocation.x > m_Width/2)
{
float offset = D3DX_PI/2.0f;
float angle = offset - atan2(dir.y,dir.x); // change this when you rotate the model
D3DXMATRIX rot;
D3DXMatrixRotationY(&rot,angle);
bone->CombinedTransformationMatrix = rot*final;
}
else
{
float offset = -D3DX_PI/2.0f;
float angle = offset + atan2(dir.y,dir.x); // change this when you rotate the model
D3DXMATRIX rot;
D3DXMatrixRotationY(&rot,angle);
bone->CombinedTransformationMatrix = rot*final;
}
}
else
{
D3DXMatrixMultiply(&bone->CombinedTransformationMatrix,
&bone->TransformationMatrix,
parentMatrix);
}
}
else
bone->CombinedTransformationMatrix = bone->TransformationMatrix;


if(bone->pFrameSibling)
{
UpdateMatrices(bones, (BONE*)bone->pFrameSibling, parentMatrix);
}
if(bone->pFrameFirstChild)
{
UpdateMatrices(bones, (BONE*)bone->pFrameFirstChild, &bone->CombinedTransformationMatrix);
}
}
else
{
if(bone != NULL)
D3DXMatrixMultiply(&bone->CombinedTransformationMatrix,
&bone->TransformationMatrix,
parentMatrix);
else
bone->CombinedTransformationMatrix = bone->TransformationMatrix;


if(bone->pFrameSibling)
UpdateMatrices(bones, (BONE*)bone->pFrameSibling, parentMatrix);
if(bone->pFrameFirstChild)
UpdateMatrices(bones, (BONE*)bone->pFrameFirstChild, &bone->CombinedTransformationMatrix);
}}

[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"]But he does not move properly in the direction that the mouse cursor is, why?[/font]
[font="arial, sans-serif"]as, I move the mouse, it moves very little, but in reverse (back)[/font]
[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"]Thanks[/font]
[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"]I'm sending a video to youtube with my problem, wait [/font]rolleyes.gif

Share this post


Link to post
Share on other sites
Advertisement

In your "world" (as shown in the video), which direction is the Y axis? You're adding a rotation about the Y axis to "aim" the bone. Is that the correct axis?


[font=arial, sans-serif]The Y-Axis is "up" and "down",[/font]
[font=arial, sans-serif]I used another class for SkinnedMesh, and it worked perfectly, but I had to create a new class, because there was conflict with my octree and now this new class as it is going wrong in the video :([/font]

Share this post


Link to post
Share on other sites
You add a rotation about the Y axis to move the arm. As asked above but not answered: is that the correct axis for your model's local arm? You might try changing to D3DXMatrixRotationX or Z.

EDIT: The method you're using assumes the local Y axis for the bone is into/out-of the screen.

Share this post


Link to post
Share on other sites

You add a rotation about the Y axis to move the arm. As asked above but not answered: is that the correct axis for your model's local arm? You might try changing to D3DXMatrixRotationX or Z.

EDIT: The method you're using assumes the local Y axis for the bone is into/out-of the screen.


[font="arial, sans-serif"]I tried using the axis X or Z, but the results were wrong, if you watch the video (720p in fullscreen), you will see that the arm moves in the right direction, but it moves very little,[/font]
[font="arial, sans-serif"]and when it is facing to the left, [/font][font="arial, sans-serif"]the arm is back [/font]blink.gif

Share this post


Link to post
Share on other sites
First, solve one problem at a time. You're apparently changing the facing direction of the entire model based on the location of the mouse. Until you can get the arm to point to the correct location, don't do that!.

Second, double-check that the mouse location you're using is in client (not screen) coordinates.

Then, you need to determine the local axis for the bone which, when multiplied by the bone's parent's matrices, points into/out-of the screen. That's the axis about which you want to add the rotation based on the mouse position. You can experiment a bit by guessing at various axes - something like:

D3DXVECTOR3 guessAxis(0,0,1);
D3DXVec3TransformNormal(&guessAxis,&guessAxis,&final);
// Now check if guessAxis is the axis for your system that points into/out-of the screen

When you find the correct axis, use that for your D3DXRotation function. Then you can play around with changing the direction of the model.

EDIT: More simply, determine the axis that points into/out-of your screen. Then:

D3DXVECTOR3 screenAxis(...); // set this to the axis in/out of screen - USE A UNIT VECTOR
D3DXMATRIX invFinal;
D3DXMatrixInverse(&invFinal,NULL,&final);
D3DXVec3TransformNormal(&screenAxis,&screenAxis,&invFinal);

After the conversion, screenAxis will be the axis about which you need to rotate with respect to the mouse position.

Share this post


Link to post
Share on other sites
[font="arial, sans-serif"]I'm not rotating the model as the mouse position [/font][font="arial, sans-serif"](as you told me not to do)[/font]
[font="arial, sans-serif"]See how it's the code:[/font]
[font="arial, sans-serif"]if(bone->Name != NULL && strcmp(bone->Name,"Bip01_L_UpperArm")==0)
{
D3DXMATRIX ident;
D3DXMatrixIdentity(&ident);
D3DXMATRIX final = bone->TransformationMatrix*(*parentMatrix);
D3DXVECTOR3 boneLocation(final._41,final._42,final._43);
D3DXVec3Project(&boneLocation,&boneLocation,&m_vp,&m_pm,&m_vm,&ident); // use YOUR viewport, projection and view matrices in this cal
D3DXVECTOR3 dir = mouseLocation - boneLocation;

float offset = D3DX_PI/2.0f;
float angle = offset - atan2(dir.y,dir.x); // change this when you rotate the model
D3DXMATRIX rot;
D3DXMatrixRotationY(&rot,angle);
bone->CombinedTransformationMatrix = rot*final;]
}

[/font]

[font="arial, sans-serif"]But still the same problem, and now I realized that as I move the player up or down, I'mcontrolling the bone moves alone, see the video when the player starts to fly upwards[/font]Look:
http://www.youtube.c...h?v=vm6W1SjZsag

[font="arial, sans-serif"]And I'm passing the Coordinators of the mouse as follows:[/font]
[font="arial, sans-serif"]GetCursorPos(&absCursor);
///////////////////////////////////////////////////////
// Get matrices and the viewport
///////////////////////////////////////////////////////
D3DXMATRIX pm; // projection matrix
D3DXMATRIX vm; // view matrix
D3DVIEWPORT9 vp; //viewport

m_pDevice->GetTransform(D3DTS_PROJECTION, &pm);
m_pDevice->GetTransform(D3DTS_VIEW, &vm);
m_pDevice->GetViewport(&vp);

D3DXVECTOR3 mouseLocation((float)absCursor.x,(float)absCursor.y,0);
player->mouselocal(mouseLocation, WIDTH, pm, vm,vp);
[/font]

Share this post


Link to post
Share on other sites
You're trying to solve too many problems at once. Work on one problem at a time. You apparently haven't gotten the arm rotation fixed, so you shouldn't be worrying about "flying." When you introduce new functions and variables, it's too difficult to help you, particularly since you don't answer questions that're necessary to understand what you're doing.

For instance, I don't know what player->mouselocal(...) does. I would suggest, for clarity:

GetCursorPos(&absCursor);
ScreenToClient(hWnd,&absCursor); // hWnd = the handle to your DirectX window
D3DXVECTOR3 mouseLocation( absCursor.x, absCursor.y, 0 ); // mouse in client coordinates

Also, you're still rotating the arm about the Y axis. Did you determine that's the correct axis to rotate about?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!