Grotius

I'm trying to implement a camera that follows a spaceship that turns in three dimensions. My ship itself turns pretty well now, thanks to the magic of Quaternions. I had thought it would be a simple task to grab the ship's position and rotation and use it for a follow-cam, but this code doesn't quite work. The camera does follow, but it doesn't properly rotate with the ship:

private void UpdateCamera()

{

Matrix rot = Matrix.CreateFromQuaternion(shipRotationQuat); // grab the ship's rotation from its Quaternion

Vector3 transformedCameraPosition = Vector3.Transform(new Vector3(0f, 800f, 800f), rot);

cameraPosition = shipPosition + transformedCameraPosition; // transform cam position and add to ship positon

cameraTarget = shipPosition; // aim camera at ship

view = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up); // create view matrix

}

Later, in Draw(),

{ ...

effect.View = view; // etc.

}

Any suggestions



Re: XNA Game Studio Express Third-person camera not quite right

Catalin Zima

I would say your problem is the Vector.Up in the CreateLookAt function. you should get the Transformed UP vector (I think rot.Up should do the trick)



Re: XNA Game Studio Express Third-person camera not quite right

Grotius

You, sir, are a genius. rot.Up worked perfectly! Thanks so much. I'm going to rate your post "helpful".

Gotta love this whole quaternion thing. There's not a line of trig in my code now. :)





Re: XNA Game Studio Express Third-person camera not quite right

Catalin Zima

Glad I could be of help