Sign in to follow this  
Evil Booger

3d Space Shooter camera system How to keep the camera aligned to the ship(c++,darkgd

Recommended Posts

I need help making the camera turn and move when the ship turns. I wish I could show you a movie of what I am talking about but I only only try and describe it to you. Imagine a player-controlled spaceship from the top-down view. The camera that the player sees out of is behind the spaceship. When the player turns right, the camera swings to the left in a semi-circle motion. How do I achieve that with code? Is that what that DarkGDK camera function is for if used correctly)?

Share this post

Link to post
Share on other sites
I would create a camera class, it isnt to difficult. From my bookmarks...

I have been told not sure by who that it is best to always keep the character always at (0,0,0). I agree because if you move a long way you will be stretching the float precision and best of all your view matrix wont have to be updated!

But still use a camera class; its OOP; worship it.

Edit: ~ I always proof-read after posting :p

It might make it a bit clearer if i were to say that you apply the inverse translation(or in this case rotation) when drawing everything but your character.

Share this post

Link to post
Share on other sites
Original post by Evil Booger
But would't a camera class be redundant because of all the DarkGDK camera functions?

And how would I rotate ships other than the player's ship? Wouldn't that create the same problem that I had with the camera before?
I don't know anything about DarkGDK, but I can tell you a little about how this would be done in general.

Most graphics APIs have a pipeline through which all geometry is processed. Part of that pipeline is a 4x4 matrix transform that is applied to the input geometry (vertices and so forth). Different APIs handle this transform differently, but in general you can think of it as being split up into a model or world part, a view part, and a projection part.

We're not concerned here with the projection part, so we'll set that aside. With respect to the model and view parts, different APIs handle them differently (for example, OpenGL combines them into a single 'modelview' matrix, while D3D keeps them separate), but regardless, the transforms are applied in the order model->view.

The purpose of the model transform is to get the geometry from local space (where the model is defined) into world space. The purpose of the view transform is to get the geometry from world space into local camera space. As such, the view transform is generally the inverse of an existing model transform (if you know a bit about linear algebra, you'll understand that if a model transform transforms geometry from local to world space, then the inverse of that will transform geometry from world to local space, which is what we want a view transform to do).

Usually, we want to render multiple models, each with their own model transforms, but at the same time we want to keep the same camera transform. This is where a matrix stack can come into play (how this is handled depends on the API).

Now, what you're asking about is how to set up the view transform for a 3rd-person view of your ship. As I said earlier, the view transform is generally just the inverse of a model transform, so what we need to do is figure out how to compute a world transform that is 'above and behind' that player ship.

To do this, we start by creating a local transform, in the local space of the ship, that meets these requirements. I don't know which direction your ship faces in local space, but presumably you'd want to position the camera some number of units in the negative direction along the forward vector, and some number of units along the up vector. Then, you'll probably want to pitch the camera forward so that it's directed at the ship.

Once you've constructed this transform, you:

1. Concatenate it with the ship's world transform to yield a camera world transform
2. Invert the resulting transform to yield the view transform

Now, a lot of APIs have a 'look at' convenience function that takes care of some of these details for you. If you have such a function available, you can just compute the camera position in world space and then feed it, along with the player position and up vector, into the 'look at' function.

Note that this just describes a fixed camera; incorporating acceleration, damping, and so on is more complicated.

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