Dahud
Dahud

Reputation: 181

(DirectX) Generating a rotation matrix to match a vector

How can I take a vector and generate a matrix that will rotate a mesh to face along that vector?

Specifics:

I want the model for my player to rotate to face the direction that the camera is looking. Right now, it always points in the direction it started in, and turning just makes the camera look at the player's side.

I have experimented with D3DXMatrixYawPitchRoll, with partial success. However, I noticed some drift in time from perfect alignment, and it had some strange wobbly issues when I tried pitching while yawing.

Upvotes: 2

Views: 3975

Answers (1)

Goz
Goz

Reputation: 62333

It will work in almost exactly the same way as this answer I've posted:

Rotate a sphere so that its pole heads towards the camera

The main change is that instead of "camera position" - "object origin" you do "object origin" - "camera position".

Upvotes: 2

Related Questions