# Problem trying to set basic camera angle

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

## Recommended Posts

Hi Before I have use function D3DXMatrixLookAtLH like this
   D3DXMatrixLookAtLH
(
&view,
&D3DXVECTOR3(X, Y, Z),    // the camera position
&D3DXVECTOR3(0, 0, 0),    // the look-at position
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)  // the up direction
);


to make my camera look at word orign. But this is bad solution if I want to move camera along x y or z axis couse it always looks at the same point. So I tryed to set cameras position and angle(Pitch,Yaw,Roll) the same way as I am doing for vertices like this
   D3DXMatrixRotationYawPitchRoll(&rotation,D3DXToRadian(Yaw),D3DXToRadian(Pitch),D3DXToRadian(Roll));
D3DXMatrixTranslation(&position,X,Y,Z);
view = rotation * position;
d3dDevice->SetTransform(D3DTS_VIEW, &view);


but something mathematicly is wrong couse camera interpretates it's position invers. I say go to position (0,0,-2) but it goes to 0,0,2. The same thing with all axes Can anyone suggest me how to make look camera to some direction depend on it's pitch,yaw,roll angles

##### Share on other sites
You can still use the D3DX function to build your view matrix, just rotate the view direction vector before you pass it in rather than rotate the entire matrix.

##### Share on other sites
Sorry for no replay

How do I exactly rotate vector in 3 dimension. Is there some DX function for this? I misted up a bit.
I found this formula on NET. It calculates vector (1,0) from given angle on 2D plane

NewX = Cos(Angle)*X - Sin(Angle)*YNewY = Cos(Angle)*Y + Sin(Angle)*X

Example if I want to rotate vector 45degrees i will get (0.707,0.707) it's correct but this formula is only for 2D. How can I do this for 3D (I have three angle values not one).

I gues I should use matrice here but I don't understand them very well
and I have no idea which matrice element represent vector direction(position)in final matrix like
D3DXMatrixRotationYawPitchRoll(&final_rotation_matrix,D3DXToRadian(Yaw),D3DXToRadian(Pitch),D3DXToRadian(Roll));.

is it _11,_12,_13 or _11 _21 _31...

[Edited by - redas on March 22, 2007 1:24:44 PM]

##### Share on other sites
Can anyone at least say which picture is correct.
I am not sure 100% in which direction DX calculates angles (I use "Left-handed system")

Oh God if the "A" is the right one than it looks I finaly got my camera to work :)

##### Share on other sites
i'm sory but its unfortunately "Picture B"
you see when you chose "Left Hand System" (directx coor system) rotations have to be Counter ClockWise (CCW) based

##### Share on other sites
Quote:
 its unfortunately "Picture B"
Why I am not surprised :)

Damn camera still deosn't work corectly...

##### Share on other sites
Quote:
 I have no idea which matrice element represent vector direction(position)in final matrixis it _11,_12,_13 or _11 _21 _31..

its like that :

_11 : Right.x
_12 : Right.y
_13 : Right.z
_14 : 0

_21 : Up.x
_22 : Up.y
_23 : Up.z
_24 : 0

_31 : Direction.x
_32 : Direction.y
_33 : Direction.z
_34 : 0

_41 : Position.x
_42 : Position.y
_43 : Position.z
_44 : 1

anyway i didnt handle my camera object which your way, you dont have to think deeply like that
here is my solution :

your cameras are like other objects right? i mean they have own positon, rotation values if so

procedure TCamera.PrepareCamera;begin  if CameraLocked then    ObjectAim(CameraLockedObject.AbsolutePosition,CameraUpVector);{ this part isnt so important but if like learn "Aiming" i also send that method}  FD3DDevice.SetTransform(D3DTS_VIEW,MatrixView);  FD3DDevice.SetTransform(D3DTS_PROJECTION,MatrixPerspective);end;function TCamera.MatrixView: TD3DMatrix;var  look:TD3DVector;begin  D3DXVec3Add(look,AbsolutePosition,AbsoluteDirection);  D3DXMatrixLookAtLH(Result,AbsolutePosition,look,AbsoluteUp);end;function TCamera.MatrixPerspective: TD3DMatrix;var  width,height:integer;begin    with RealScreenRect do {this is for multi camera games or some thing else you dont have to add that code you can just use screen width and height}    begin      width:=Right-Left;      height:=Bottom-Top;    end;    if Perspective then {this is for perspective and ortho cameras}      D3DXMatrixPerspectiveFovLH(Result,PointOfView,width/height,ZNear,ZFar)    else      D3DXMatrixOrthoLH(Result,width/(PointOfView*100),height/(PointOfView*100),        ZNear,ZFar);end;

##### Share on other sites
if you want to learn values of variables (like PointOfView, ZNear, ZFar) or something else i can explain

##### Share on other sites
ibr_ozdemir thank you for trying to help.

Yes my camera is like other objects, it has X,Y,Z and Pitch,Yaw,Roll properties.
So let's say now I want to set my camera. What will I do? afcourse I will use
D3DXMatrixLookAtLH() couse all tutorials shows it;
   D3DXMatrixLookAtLH   (      &view,      &D3DXVECTOR3(X, Y, Z),    // the camera position <= wow it's cool I can just simply pass object's position properties      &lookat,    // the look-at position <= this is my nightmare      &D3DXVECTOR3(0.0f, 1.0f, 0.0f)  // the up direction   );

All parameters of this function are good just how they are except "look-at".
Ye it's good if you want have static camera or you want make camera to fallow
for example a ball, afcourse camera will not change it's position but it will change it's angles.

Exactly "look-at"parameter and is my PROBLEM. What I wish to do is get some kind of direction vector calculated from given angles (Pitch,Yaw,Row).
Let's say I wish to make camera be rotated 45 degrees around Y axis,
then my direction(look-at) vector should become (0.707, 0, 0.707) let's take another example "Rotate camera 30 degrees around Y axis" than look-at vector should be (0.866, 0, 0.5).

Current camera render function code looks like that
   D3DXMatrixRotationX(&mat_pitch,D3DXToRadian(Pitch));   D3DXMatrixRotationY(&mat_yaw,D3DXToRadian(Yaw));   D3DXMatrixRotationZ(&mat_roll,D3DXToRadian(Roll));   mat_rotation = mat_yaw * mat_pitch * mat_roll;   lookat.x = mat_rotation._31;   lookat.y = mat_rotation._32;   lookat.z = mat_rotation._33;   D3DXVec3Add(&lookat,&D3DXVECTOR3(X, Y, Z),&lookat);   D3DXMatrixLookAtLH   (      &mat_view,      &D3DXVECTOR3(X, Y, Z),    // the camera position      &lookat,    // the look-at position      &D3DXVECTOR3(0.0f, 1.0f, 0.0f)  // the up direction   );   d3dDevice->SetTransform(D3DTS_VIEW, &mat_view);// set the view transform   D3DXMatrixPerspectiveFovLH   (      &mat_projection,      D3DXToRadian(60),    // the horizontal field of view      Aspect,    // aspect ratio      1.0f,    // the near view-plane      Range   );   d3dDevice->SetTransform(D3DTS_PROJECTION, &mat_projection); // set the projection transform

this code version is allmost good becouse:
1. It doesn't interpretate camera position inversly
2. It looks like it rotates to correct direction along X and Y axis, BUT it has no effect on Z axis.
What am I doing wrong?

What is the right order to multiply transformation matrices. Is this one correct?
"scale * rotationY * rotationX * rotationZ * position"

Now here's how I understand PointOfView, ZNear, ZFar:
PointOfView - This is point(vector) where camera looks for example 0,0,0 it will looks at word orign
ZNear - it's the distance from camera position at which camera actulay starts to see something. I set it to 1.0f
ZFar - it's distance from camera position where camera stops to see. I set it to big number 1024.0f. So ZFar - ZNear = camera view range

[Edited by - redas on March 23, 2007 9:08:24 AM]

##### Share on other sites
first i have to say that "PointOfView" is very incapable word, its not some kind of Point-Of-View, sory

its "flovy", actually you can see flovy in D3DXMatrixPerspectiveFovLH method...anyway

i make my camera matrix like this:
(there is no scale factor for cameras but i'm goingto add that code to)

function Object.Matrix:TD3DMatrix;var  m:TD3DMatrix;begin  //following code make matrix for every child objects, i mean "local matrix"  D3DXMatrixTranslation(Result,Axis.x,Axis.y,Axis.z);{this is for axis you dont have to do that but its good for rotating something like legs, arms etc. you got the idea}  D3DXMatrixIdentity(m);  with m do  begin    _11:=Right.x*Scale.x;      _12:=Right.y*Scale.x;      _13:=Right.z*Scale.x;    _21:=Up.x*Scale.y;         _22:=Up.y*Scale.y;         _23:=Up.z*Scale.y;    _31:=Direction.x*Scale.z;  _32:=Direction.y*Scale.z;  _33:=Direction.z*Scale.z;  end;  D3DXMatrixMultiply(Result,Result,m);  D3DXMatrixTranslation(m,Positon.x,Positon.y,Positon.z);  D3DXMatrixMultiply(Result,Result,m);  //following code make "world matrix" for object  if Parent<>nil then    D3DXMatrixMultiply(Reslut,Result,Parent.Matrix);end;

if you want learn how am i filling Right, Up, Direction vector i can show them

1. 1
Rutin
54
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633412
• Total Posts
3011729
• ### Who's Online (See full list)

There are no registered users currently online

×