Yet another 3d camera question, open space, no friction.

So, you’ve all played Elite Dangerous right? Or thought about it, I can recommend it if you like a relaxing game with a bit of pew thrown in.

Anyway, this is the kind of control I need to reproduce. The camera, basically the object you are sat in will be floating in a no friction medium. I need the usual forward and backwards stuff, slew, roll, etc.

I’ve done this in 2d space with top down views. I can handle all the pseudo physics for the object, but this damn camera object just boggles my mind. I’m confused as to why there isn’t a simple operation to copy the position and rotation from another object. My ‘ship’ (for want of a better word) is pointing where it should be, behaving how it should be, all I want to do is bolt the camera on top, or in the cockpit I suppose. But then I need to complicate things further by adding head look to the existing vectors, this shouldn’t be too difficult?

Maybe I’m missing something.

I’ve been looking for flight sim type examples, very few and far between. What I can find is either 3rd person camera positioning, or so vast pulling apart the code will take ages.

Is there anyone who could point me towards a small example or tutorial?

Many thanks.

You need to effectively make the ship object the parent world matrix for the camera, you will need to do something like:

cameraWorldMAtrix *= ShipWorldMatrix;

This will place the camera in the same world space as the ship, I think :smiley:

You don’t typically set the view from the camera world directly or you can mess up the projection matrix multiplications CreateWorld is not the same as CreateLookAt even though they seem very similar.

Maybe reading this will help a bit…

Your camera objects matrix is used to create the view matrix.
The view is created typically based on any world object you choose using Matrix.CreateLookAt(…)

To say if your ship is the camera …
You use its world matrix vectors (position forward up) that represents its orientation in the world or worldspace to input to the create look at function in order to create the view matrix which represent camera operation space or view space.

If you want to offset the position of the camera.
Ccopy the object that represents your camera in this case your ship.
Matrix temp = myShip;.
Take the up vector and add it to the position which is the translation vector by some amount.
temp.Translation += myShip.Up * 5f; // by say 5 units.

now you have a world orientation that is just above your ship with the same orientations as your ship.
build the view matrix from it.
Matrix view = Matrix.CreateLookAt(temp.Translation, temp.Translation + temp.Forward, temp.Up);

In order to have your ship and camera roll you need to also allow the up vector of the ship to be able to rotate around the forward axis, this can be done simply with create from axis angle using your current ships forward vector, as the axis to rotate upon. This would be done before the translation previously described.

Maybe this older camera is easier to understand, if the other camera was not. This was a older one that separates the ideas of, the camera world object itself, the controls and the view matrix manipulations, into 3 distinct classes.

But the other class i linked to in your last post actually has a method to accept a target matrix to just use as a camera which is pretty trivial.