Jump to content
  • Advertisement
Sign in to follow this  
Jimmy Valavanis

PLEASE HELP - navigation problem (SOLVED)

This topic is 4623 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

I have the following code in my program:

  dev = D3DDevice;


  if (dev)
    GetRotationMatrix(mat, Rotation.X, Rotation.Y, Rotation.Z, 0.75);
    mat._44 = NearClippingPlane; // Near clipping plane
    dev->SetTransform(D3DTRANSFORMSTATE_VIEW, mat);

    matPosition._11 =  1.0;
    matPosition._22 =  1.0;
    matPosition._33 =  1.0;
    matPosition._44 =  1.0;
    matPosition._41 =  -Position.X;
    matPosition._42 =  -Position.Y;
    matPosition._43 =  -Position.Z;
    dev->SetTransform(D3DTRANSFORMSTATE_WORLD, matPosition);

that setup the rotation & position matrixes. Position -> position vector (D3DVector) Rotation -> rotation vector (D3DVector) THE PROBLEM: I'm want to "navigate" (i.e. change Position vector) to the direction that Rotation vector shows. (let's say flying a spacecraft). What D3DVector value must add to Position vector in a time unit (time - oldtime: time: current render time, oldtime: last render time) so that I 'll manage to move in that direction???? [Edited by - Jimmy Valavanis on October 21, 2005 1:49:40 PM]

Share this post

Link to post
Share on other sites
I am going to try and explain what I did to solve a similar problem. But it's not really complete and lacks some really slick control.

I have a camera class that will later be inherited by a FreeForm or 3rd Person class. In the camera class I have a basic layout.
I have 4 vectors that define the camera.
1) a Positional vector
2) a LookAt vector
3) a Up vector
4) a Right vector

The camera class has a method for building the view matrix
All you do is use the D3DXMatrixLookAtLH() and plug in the vectors defined in your class and then set the view matrix.

public virtual void BuildViewMatrix(Device device)
matView = Matrix.LookAtLH(position, lookAt, up);
device.Transform.View = matView;

From this class I inherit a freeform class and most of the work is done here.
The Pitch Method

private void Pitch(float angle)
Matrix matPitch = Matrix.RotationAxis(right, angle);
up = Vector3.TransformCoordinate(up, matPitch);
lookAt = Vector3.TransformCoordinate(lookAt, matPitch);

The Yaw Method

private void Yaw(float angle)
Matrix matYaw = Matrix.RotationAxis(up, angle);
right = Vector3.TransformCoordinate(right, matYaw);
lookAt = Vector3.TransformCoordinate(lookAt, matYaw);

The RollMethod

private void Roll(float angle)
Matrix matRoll = Matrix.RotationAxis(lookAt, angle);
right = Vector3.TransformCoordinate(right, matRoll);
up = Vector3.TransformCoordinate(up, matRoll);

The Walk Method

private void Walk(float units)
position += lookAt * units;

The Strafe Method

private void Strafe(float units)
position += right * units;

The Fly Method

private void Fly(float units)
position += up * units;

The last method is the method that really does the work and it's where you are building the view matrix.
Building the View Matrix

matView = Matrix.Identity;
//keep the camera's axes orthogonal to each other
lookAt = Vector3.Normalize(lookAt);
up = Vector3.Cross(lookAt, right);
up = Vector3.Normalize(up);
right = Vector3.Cross(up, lookAt);
right = Vector3.Normalize(right);
//build view matrix
float x = -Vector3.Dot(right, position);
float y = -Vector3.Dot(up, position);
float z = -Vector3.Dot(lookAt, position);
//First Matrix row in the form
/* rightX, upX, lookX, 0
* rightY, upY, lookY, 0
* rightZ, upZ, lookZ, 0
* posX ,posY, posZ, 1

matView.M11 = right.X;
matView.M12 = up.X;
matView.M13 = lookAt.X;
matView.M14 = 0.0f;

matView.M21 = right.Y;
matView.M22 = up.Y;
matView.M23 = lookAt.Y;
matView.M24 = 0.0f;

matView.M31 = right.Z;
matView.M32 = up.Z;
matView.M33 = lookAt.Z;
matView.M34 = 0.0f;

matView.M41 = x;
matView.M42 = y;
matView.M43 = z;
matView.M44 = 1.0f;
device.Transform.View = matView;

If you have any questions about the camera etc.. before you ask them look at the Sample Framework's camera code. It should be a pretty good reference as i've used the camera in the sample framework in the SDK.
If you have any other questions I would be happy to answer them and sorry for the lack of explanation.

I hope this helps.
Take care.

Share this post

Link to post
Share on other sites
Thanks for the reply, but it does not help me much.
Unfortunatelly I'm totally unfamiliar with C++ classes...

I'm trying to do the following:

r = NegativeVector(Rotation); // Invert view rotation
GetRotationMatrix(mat, r.x, r.y, r.z); // Get rotation matrix

// dist -> distance I want to move
// v1 -> get a vector that corresponts to Rotation = (0.0, 0.0, 0.0)
// i.e. -> I add v1 to Position if Rotation = (0.0, 0.0, 0.0)
// IS THAT RIGHT ?????
v1 = MakeD3DVector(0.0, 0.0, dist);
D3DMath_VectorMatrixMultiply(v, v1, mat); // Rotate v1 vector to invert rotation view

v = VectorAdd(Position, v);

The above code does not work. It only works when 2 of the 3 coordinates of
Rotation vector are = 0.0

Share this post

Link to post
Share on other sites
I've made it!!!!!

r = Rotation;
GetRotationMatrix(mat, r.x, r.y, r.z);
// RIGHT: Invert the rotation matrix!!!
// WRONG: Take negative rotation vector
D3DMath_MatrixInvert(mat2, mat);
v1 = MakeD3DVector(0.0, 0.0, dist);
D3DMath_VectorMatrixMultiply(v, v1, mat2);

v = VectorAdd(Position, v);

I'm getting old...

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!