Those darn cameras! Camera 1 Part 1

I know I am not the only one that feels this way, but cameras are a huge pain! I have seen so many people quitting projects just because they couldn’t get those darn camera’s right! It’s fine in 2D games, not much math just follow the dude. 3D is a completely different story!

I have decided to write a couple of articles dedicated to cameras! The cameras I will be detailing:
First Person
Third Person
Strategy type (If I have the time).

Incidentally, my current game uses a third person camera, so that is what I will start with!

A third person camera is, in my opinion, the best sort of camera, and is also the most popular. The problem is just the math involved.

Co-ordinate system
Typically games use the Cartesian style for any sort of programming. Games are no different, the only difference is 3D games use a 3D Cartesian co-ordinate system.

If you have no idea what the Cartesian co-ordinate system is, I would recommend you stop reading right now and read up on the Cartesian co-ordinate system!

So next up, we have the polar co-ordinate system.
The Cartesian system is known for it’s simplicity of moving objects. The polar co-ordinates is known for its simplicity in rotations. It’s position is defined by theta and r, as seen below.

Thanks to trigonometry primitives, we can convert the polar system to the Cartesian system via these formulas:
Once can also convert the Cartesian system to the polar system via these formulas:

A further advancement of the polar co-ordinate system we use is the spherical system.
The spherical system of course has 3 arguments. (r, a, a2). Where the 2 a’s are angles. This is basically how it looks:

Now let me be the first on this blog to say, that the spherical co-ordinate system is the most logic choice for a 3D camera because of it’s rotation in a 3D space.

And yes, lucky for you, one can convert the spherical system to a 3D Cartesian system.

Or as others prefer:

You can also convert from the Cartesian system to the spherical system like so:

Why did I teach you this?
In order to understand the math used, you have to understand what system is used. You have to have some basic knowledge on the topic otherwise you wouldn’t understand anything, that would be a waste.

I know what some of you are thinking. “I get the concept, now why on earth would I need to know this junk?”.

Simple really, I assume you are familiar with the Cartesian system which moves a 3D object. The spherical system is used on a 3D camera, for obvious reasons.

See that little dot in the centre? Pretend that that is the character, observe how the camera can easily travel around the character without that much math.

Camera target would be the character’s X,Y and Z co-ordinates.
The position of the camera would be something as explained above.
There you have it!
In the next lesson I will go around changing it’s height and it’s rotation etc.

Or if you want, you can use the current formula I am using in my project, which is a little more complex, more on this later:
X = -1*cos(-1*YRotation*PI/180) + XPosition
Y = YPosition
Z = -1*sin(-z*YRotation*PI/180) + ZPosition

Note: YRotation and XPosition, YPosition, ZPosition, refers to the character, not the camera itself.

In part 2 we will be looking at a downloadable example, as well as manipulating the camera such as Zoom, height, rotation etc. I shall also introduce the spring system. In part 3 we will be looking at other considerations, such as boundaries. I hope you learnt something from this article, thank you so much for reading.

Mathematics for GameDevelopers
Programming Gems 4
Math teacher :P

No comments:

Post a Comment