Sign in to follow this  
bvs33

Moving Direction Vector in 3d Space

Recommended Posts

I'm working on a camera/movement right now and I seem to have forgotten too much trig to figure this out. I'm trying to program a 3rd person arcade type flight sim camera in directx 9. The camera sits behind the plane and stays pretty steady, the controls move the model and allow it to change direction and roll, etc... The camera simply follows it. I have two main problems: 1) How can I move the direction vector in 3d space? For example say my direction vector is (x,y,z) and when certain key is pressed I want to move that vector in the direction of the right vector of my model? 2) How can I rotate around the direction vector. Rotating around the x,y or z axis is easy using the D3DXMatrixRotataion functions, but if I want to realistically roll the plane I'm going to need to rotate around the direction vector. Thanks!

Share this post


Link to post
Share on other sites
I am not sure about what DirectX supports, but to rotate around the direction vector you require an axis-angle rotation. This typically involves a quaternion (but doesnt have to). See if DirectX has a quaternion rotation class or something.

I am not sure what you mean by moving the direction vector. You cannot
translate vectors because they are position-less. You can rotate them,
deform them etc.

Share this post


Link to post
Share on other sites
Should be able to do something like

D3DXMatrixRotationAxis(&mat, &shipForwardAxis, D3DXToRadian(15) * _deltatime);

to roll 15 degrees per second. Multiply this matrix into the ships matrix(I always forget the order)

Regarding rotating the direction vector, I commonly use D3DXVec3TransformNormal.

Share this post


Link to post
Share on other sites
To extract the direction vector from a matrix, multiply the RHW vector (0,0,1,0) by this matrix: the result vector is your direction vector (normalized).

Store Rotation, Position and (if you want) Scaling matrices to determine at all times the position of your object.

To find out the full transformation matrix of your object (the one you give to the renderer), apply the matrices in the following order to the identity matrix:

Scaling, Rotation, Position

Now, the answers to your questions:

1) Multiply your Position matrix by a translation matrix you computed using the direction vector.

2) Multiply your Rotation matrix by a rotation around axis matrix you computed using the direction vector.

Share this post


Link to post
Share on other sites
Ok, I think I see how to perform the rotation around the direction vector, but I'm still confused about how to change the direction vector.

I know what the current direction vector is (obviously) but I sitll don't see how to move it in response to input and track it's new coordinates.

Maybe some code would help...

Share this post


Link to post
Share on other sites
Quote:
To extract the direction vector from a matrix, multiply the RHW vector (0,0,1,0) by this matrix: the result vector is your direction vector (normalized).
Just wanted to point out to the OP that this step isn't strictly necessary. Geometrically, the rows or columns of the rotation matrix are already the basis vectors of the local frame and can be extracted directly. Or you can look at it mathematically, in that multiplying a cardinal basis vector by a rotation matrix will simply return the corresponding row or column. In any case, no actual multiplication is necessary.

Share this post


Link to post
Share on other sites
Ok, I'm going bloody crazy here. I can get the direction vector changed ok now, but I can't seem to rotate the model to line up with it.


Here is my code. What I THINK this code should do is place my model in the scene and put my camera 50 units behind and 15 units up. The W/S keys should move my model forward/backwards (they do) and the camera should follow (it does). The D/A keys should rotate my direction vector and along with it my camera around the model's up vector (they do), but they should also rotate the model to keep it facing the direction vector and this does NOT occur. The model doesn't appear to rotate. I can't figure out why.


if (m_pInput->KeyPressed(DIK_W)) // Key W
{
model.Velocity=2);
}
// move back
if (m_pInput->KeyPressed(DIK_S)) // Key S
{
model.Velocity=-2;
}
if (m_pInput->KeyPressed(DIK_D)) // Key D
{
D3DXMatrixRotationAxis(&MatRot, &model_Up, .4);

D3DXVec3TransformCoord(&model_Right,&model_Right, &MatRot);
D3DXVec3TransformCoord(&model_Direction,&model_Direction, &MatRot);
}
if (m_pInput->KeyPressed(DIK_A)) // Key A
{
D3DXMatrixRotationAxis(&MatRot, &model_Up, -.4);

D3DXVec3TransformCoord(&model_Right,&model_Right, &MatRot);
D3DXVec3TransformCoord(&model_Direction,&model_Direction, &MatRot);
}

//move model
D3DXVECTOR3 newpos(model.Direction() * modelVelocity()) + model.Position());
model.Position=&newpos;

// define camera vectors
D3DXVECTOR3 vCameraLook=model.Position; // look at model
D3DXVECTOR3 vCameraUp=model.Up;
D3DXVECTOR3 vCameraRight=model.Right;
D3DXVECTOR3 vCameraPos=model.Position + (model.Direction* -50)); // put camera 50 units behind model
vCameraPos.y += 15; // raise camera 15 units

// find model world matrix
D3DXMatrixTranslation(
&Trans,
model_Position.x,
model_Position.y,
model_Position.z);

D3DXMatrixMultiply(&World,&MatRot,&Trans);
device->SetTransform(D3DTS_WORLD, &World);

// find view matrix
D3DXMATRIX View;
D3DXMatrixLookAtLH(
&View,
&vCameraPos,
&vCameraLook,
&vCameraUp);
device->SetTransform(D3DTS_VIEW, &View);

//render scene......

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