# Combining 2 world matrices

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

## Recommended Posts

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 on other sites

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 on other sites

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 on other sites

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 on other sites

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 on other sites

@burnt_fyr: cool, It's working

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 on other sites

@burnt_fyr: cool, It's working

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 on other sites

Thanks, sorry for the confusion.

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 on other sites

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());



And the new improved version

D3DXMATRIXA16 MatCamViewInverse;
D3DXMatrixInverse(&MatCamViewInverse, NULL, &pCam.GetViewMatrix());



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

A sample 'in action':

Edited by cozzie

##### Share on other sites

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 on other sites
:) thanks, I've lowered it, the screenshot was to demonstrate the local positioning.

2nd gun should be fun, but first... Mesh animations

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628653
• Total Posts
2984054

• 10
• 9
• 9
• 10
• 21