Sign in to follow this  
cozzie

Combining 2 world matrices

Recommended Posts

cozzie    5029

Hi,

 

I've ran into a challenge with the following:

- my weapon is rendered/ positioned using the viewIdentityMatrix (*projection matrix)

- to do this I have a local space World matrix for the weapon

 

- my point lights are in worldspace and therefor not affecting my weapon, since the world matrix used/ sent to the shader is in local space

 

My question;

- I have a local space World matrix / orientation of the gun

- I have a world space position of the camera, and yaw/roll/pitch angles (rotation)

 

How do I combine both to get a resulting World Matrix?

I've tried the following, which goes in the direction but is definately not OK:

D3DXMATRIXA16 WorldWorldspace;
D3DXVECTOR3 CamRot = pCam.GetRot();
D3DXVECTOR3 MeshRot = pD3dscene.mWeaponMeshInst[mInst].GetRot();
float scale = pD3dscene.mWeaponMeshInst[mInst].GetScale();
ComposeD3DXWorldMatrix(&WorldWorldspace, D3DXVECTOR3(scale, scale, scale), CamRot.x - MeshRot.x, CamRot.y - MeshRot.y, CamRot.z - MeshRot.z, pCam.GetPosition() + pD3dscene.mWeaponMeshInst[mInst].GetWorldPos());

I know my compose matrix function is working fine, it's used for positioning all mesh instances in the scene.

 

(the goal is to sent both the local space world matrix to the shader, for positioning, as the world space world matrix, for lighting in the pixel shader).

 

I also tried this:

D3DXMATRIXA16 WorldWorldSpace = pCam.GetViewMatrix() * pD3dscene.mWeaponMeshInst[mInst].GetWorldMatrix();

Any ideas/ hints?

Edited by cozzie

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665

I'm not sure if I read you correctly. Do you want to want a first person type weapon view? or a weapon parented to the payers mesh?

Share this post


Link to post
Share on other sites
cozzie    5029

Jep, a first person weapon view.

I've actually achieved that already, but I did it by sending the identity view * projection matrix to my shader, and a local space world matrix of the weapon.

So rendering / position on screen is fine.

 

But my point lights are in world space, and therefor never affecting/ lighting up the weapon )-;

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665

rather than sending the identity view matrix, send the same view as you would normally, but send the inverse view matrix as the 1stperson  world transform. This is the same as your cameras world transform. It should keep the weapon at the same spot on the screen, but your gun will be in the correct location for lighting.

Share this post


Link to post
Share on other sites
Norman Barrows    7179

I'm working on the 3rd version of 1st person female attack animations in CAVEMAN right now. I'm on weapon number 37 of 65 or so.

 

i draw the arms and weapon just like any other object. 

 

i apply rotation to get it pointing the right way, then i use translation to move it to where it should be relative to the camera's location in the world.

 

for example, the camera is located at the player's eyes, 5 feet above the ground at the players location.

 

for a right forearm, i might move it's local origin (the elbow joint) down one foot, right one foot, and forward one foot relative to the camera's position (the player's eyes).

 

then i simply apply  the player's rotation and translation, which moves it out into the world in front of the camera, and rotates it the same way the player is facing.

 

since its drawn like any other object, lighting is automatic.

 

and no need to change view matrix, which is a performance hit.

 

not sure, but i think its the highest performance hit of the three (world, view, and projection).

 

world changes for each unique object transformation. so it changes all the time, obviously it has to (and does) run fast.

 

i change the projection matrix 4 times per frame with no noticeable performance hit.

 

but i have read that changing the view a lot can be a performance hit. just twice per frame (once to draw weapon, then once to set it back again) is not an issue though.

Edited by Norman Barrows

Share this post


Link to post
Share on other sites
cozzie    5029

@burnt_fyr: cool, It's working rolleyes.gif

 

Now only to figure out how to combine the inverse view matrix of the camera with the local space weapon world matrix

(to be able to rotate/ position for viewer etc, also for animations).

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665

@burnt_fyr: cool, It's working rolleyes.gif

 

Now only to figure out how to combine the inverse view matrix of the camera with the local space weapon world matrix

(to be able to rotate/ position for viewer etc, also for animations).

 

Your terminology is somewhat confusing. rather than talking about matrices and spaces, talk in spaces alone(or frames of reference), and think of your matrices as objects you use to transform a vector from 1 space to another.

 

ie: local space weapon world matrix: i'm not sure what this is. There is no local space. Local to who? If it's a matrix that transforms from an object's local space, to that of the world, then it is simply a world matrix. Many times however you don't want a model to go directly to world space. You want it relative to something else. So if you want your weapon to move a bit right in the screen, simply give it a world matrix that concatenates both the translation you want, as well as the camera's inverse view transform. Depending on your handedness this could be:

object.world = translation * camera.inversetransform;
 
or
 
object.world = camera.inversetransform * translation;

If you would prefer to have it attach to a bone(for animation) then simply use the bones inverse world matrix rather than the camera's.

 

I think looking into animation should be high upon your list of priorities right now, as it will force you to learn how transforms work in a heirarchy, and you would have been able to solve this, and your future problems yourself quite easily. I myself worked on a simple car made of a heirarchy of boxes and spheroids, and that was more than enough to clarify the concept for me.

Edited by Burnt_Fyr

Share this post


Link to post
Share on other sites
cozzie    5029

Thanks, sorry for the confusion.

Reading your answer, says me that we're talking about the same goal.

 

I've tried it like this:

- create world matrix for the weapon, i.e. translate(0.1, 0.0, 0.2), rotate(0.0, 10.0, 0.0), scale (1.0, 1.0, 1.0)

- this world watrix is multiplied with the inverse view matrix of the camera

 

But then the weapon is 'gone' (not the sum of the 2 translations/rotations).

Which if I read correct, should be the way to go.

 

I'll give it another try and post the sourcecode of what I tried.

Share this post


Link to post
Share on other sites
cozzie    5029

Got it. Don't know what went wrong before, I think I tried to add them up instead of multiply.

The old version, no transformations/rotations of the weapon multiplied by inverse view matrix:

D3DXMATRIXA16 World;
D3DXMatrixInverse(&World, NULL, &pCam.GetViewMatrix());
if(FAILED(pD3dscene.mShaders[fxIndex].mEffect->SetMatrix(pD3dscene.mShaders[fxIndex].mHWorldMatrix, &World))) return false;	

And the new improved version smile.png

D3DXMATRIXA16 MatCamViewInverse;
D3DXMatrixInverse(&MatCamViewInverse, NULL, &pCam.GetViewMatrix());
if(FAILED(pD3dscene.mShaders[fxIndex].mEffect->SetMatrix(pD3dscene.mShaders[fxIndex].mHWorldMatrix, &(pD3dscene.mWeaponMeshInst[mInst].GetWorldMatrix() * MatCamViewInverse)))) return false;	

Thanks again, this really helps me saving a lot of time going to 3ds max over and over again to position my weapons rolleyes.gif

 

A sample 'in action':

 

weaponmat-ok.jpg

Edited by cozzie

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665

looking good, drop it 1/4 the screen(so you don't see the empty grip(or attach a hand mesh, parenting the gun to the hand) and add a second so you can really get the lead out :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this