Archived

This topic is now archived and is closed to further replies.

How do i Create a Camera that follows a Object(player)

This topic is 6111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am not veyr good at all this camera stuff, what i am looking for is to have a Object lets say for now a Cube this will be my player, and i want the camera sitting just above it, and when i move the object the camera will follow it, how should i go about doing this? Any advice would be great Cheers Gachuk

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hiya,
Camera''s can become a pain to write, depending on how ''intelligent'' you want them. For a simple ''follow the object no matter what'' you can do the following...

1. The cameras position is local to the targets position. So for a camera that is ''above and behind'' you could set it''s local position as x = 0, y = 50, z = -50 (scales depend on your world coordinate system, this also assumes positive y is up).
2. When it comes to render your view, add the targets position to the camera, this gives you your camera position.
3. Calculate the cameras orientation by generating a ''lookat'' matrix to the target object.

This will give you an extremely simple camera system, that you''ll want to add to. You''ll most likely want to do the following as well...

1. When calculating the cameras orientation, instead of using the objects position as the target, use a position slightly in front, or slightly above the object, otherwise the views orientation will probably be slightly too ''extreme'' and make it hard for the player to see where they''re going.

2. The above camera does not rotate with the target, so you''ll also need to rotate the camera so that it stays behind the target. A little ''give'' on the rotation will make stop it seeming ''stiff''.

After all that, you might like to add collision to stop it passing through other objects.

A very basic description, but hopefully that''ll set you off the the right direction

n!

Share this post


Link to post
Share on other sites
I thought you did it like make the background move according to what controls your using heh but then again im not very good

---------------------------
X to da Z!

Share this post


Link to post
Share on other sites
Right hmm ok, thanks right err i think i err got it err heh
not too sure about code err but i think err i can get it heh
or not oh well i give ago
Thanks for help

Gachuk

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Nonono! Don''t give up If there''s more you would like to know then just ask

Let''s elaborate slightly.
It is true that you can move the objects around the camera, but this becomes very cumbersome and the more objects you have in your scene the more extra transforms you have to calculate.

But let''s see what happens when you set your camera matrix, you have the camera''s orientation matrix, and you use the ''inverse'' transform of that matrix. The ''inverse'' is just means ''the opposite''.

Now let''s follow a mesh as it passes through our transforms.

A model starts off it''s life in it''s own coordinate space. This is called ''model space''. You then apply the ''world'' transform which applies local rotation and then moves it local to the world coordinate system.
Next the view (or camera) transform is applied, this transforms the object into camera space (so the camera is located at 0,0,0).
As you can see, the objects do move ''around'' the camera. But you''ve moved this transform into the view matrix. This transform needs to be applied anyway, so it''s essentially moving the objects around the camera ''for free'' (sorta ).

To calculate the orientation matrix for a camera you need to do the following:

vectTarget = object position
vectCamera = camera position
vectUp = world up orientation (usually 0, 1, 0).

vectDirection = vectTarget - vectCamera;
this gives you a vector between your camera and it''s target.
Normalise this and put it into your cameras third column (or row, depending on which matrix format you''re using).

vectRight = vectUp CrossProduct vectDirection.

This gives you your cameras ''right'' axis. Make sure this is normalised and place it into your cameras first column (or row as above).

vectActualUp = vectDirection CrossProduct vectRight;
This returns you your cameras ''up'' axis, which you can place into your cameras second column (or row).

This is the orientation matrix for your cameras view. (I''m assuming x,y,z vectors here) make sure the rest of your 4x4matrix is as an identity matrix.

Invert this (as it is an orthogonal matrix you can just flip around the major diagonal) and you have your camera''s orientation to look at a specific point.

Quick note, using the above there are some incorrect calculations when directly above the target object. You can avoid this by supplying a correct ''Up'' vector.

Looking back at that I''m not sure if I made it better or worse for you Hope it''s of help to someone.

n!

Share this post


Link to post
Share on other sites
- Translate to cube''s position
- Rotate to cube''s orientation
- Rotate into camera''s orientation
- Translate along camera''s Z axis to "zoom" out or in.

Share this post


Link to post
Share on other sites
Actually, I''m in the process of writing some code for a simple camera tutorial, as there are unbelievable amounts of posts about it. It would follow an object, not avoiding walls or anything, but stay oriented right in any case. I use the code in my game, so it works fine... Any interest in a GameDev article? Or is that too basic?



~BenDilts( void );

Share this post


Link to post
Share on other sites
As part of my current project, I have had to write a fixed camera (camera has fixed position and follows a specified object), chase camera (camera trails a predetermined distance behind a specified object) and first-person free look (fly around using Quake-style controls). Cameras are the bane of my programming existence, especially the chase camera.

My approach to the chase camera was to take the object''s forward unit vector, negate it so it is facing backwards and rotate it up a specified angle using the object''s right vector as an axis. Scale the resulting vector by the distance required, add it to the object''s position and that is your camera position. Now tell the camera to look at the object''s position and you have your chase camera. To make it look better, use smoothing from the camera''s previous position to get rid of any bumps and jerks the object may encounter.

Steve ''Sly'' Williams  Code Monkey  Krome Studios

Share this post


Link to post
Share on other sites
Well im not sure on all the matrix code and shit, i have been told i need to cretae my own matrix for the camera really, this is hard i need more books on 3d maths me thinks.

Oh and a camera tut would be great, no tut is too basic, anything helps

Thanks for the extra input i understand some of it

Cheers

Gachuk

Share this post


Link to post
Share on other sites