Control Of Bone

Started by
37 comments, last by VitaliBR 13 years, 3 months ago
[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="arial, sans-serif"]


[font="arial, sans-serif"]So:
[font="arial, sans-serif"]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="arial, sans-serif"]But he does not move properly in the direction that the mouse cursor is, why?
[font="arial, sans-serif"]as, I move the mouse, it moves very little, but in reverse (back)
[font="arial, sans-serif"]
[font="arial, sans-serif"]Thanks
[font="arial, sans-serif"]
[font="arial, sans-serif"]I'm sending a video to youtube with my problem, wait rolleyes.gif
http://mateusvitali.wordpress.com/
Advertisement
The video with problem:



Thanks smile.gif
http://mateusvitali.wordpress.com/

The video with problem:

http://www.youtube.c...h?v=nYjT5iXXXaE

Thanks smile.gif


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?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


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=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 :(
http://mateusvitali.wordpress.com/
Anyone? :(
http://mateusvitali.wordpress.com/
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


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="arial, sans-serif"]and when it is facing to the left, [font="arial, sans-serif"]the arm is back blink.gif
http://mateusvitali.wordpress.com/
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

[font="arial, sans-serif"]I'm not rotating the model as the mouse position [font="arial, sans-serif"](as you told me not to do)
[font="arial, sans-serif"]See how it's the code:
[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="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 upwardsLook:
http://www.youtube.c...h?v=vm6W1SjZsag

[font="arial, sans-serif"]And I'm passing the Coordinators of the mouse as follows:
[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);
http://mateusvitali.wordpress.com/
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?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement