Realization of camera class in game--directx game programming

Source: Internet
Author: User

We know that in DX, the transformation of coordinates is achieved by multiplying it with a matrix, and a new coordinate obtained by a transformation matrix is the converted coordinate (translation, rotation, scaled coordinates).

object's screen coordinate = itself coordinate system coordinate * world coordinate system transformation matrix * Observe coordinate system transformation matrix * Projection Matrix * Viewport transformation matrix

coordinate system coordinates * world coordinate system transformation matrix can be the object in the 3D world position, equivalent to you put an object in the 3D world

Then you can see the 3D world in different places and angles by just setting up a different coordinate system transformation matrix (you're moving in the 3D world)

And then multiply the projection matrix * Viewport transformation matrix is to convert the 3D world to a flat screen display on the 2D screen


To complete a camera class, it is necessary to observe the acquisition of the coordinate system transformation matrix, we call Idirect3ddevice9::settransfrom (d3dts_view,& observation coordinate system transformation matrix) before rendering, and tell DX the observed coordinate system transformation matrix , this matrix is obtained from our camera class. Through it, we can realize the roaming in the game scene.

Observing the coordinate system is to use our eyes as the origin, the right hand direction is the x-axis positive direction, the head direction is the y-axis positive direction, forward is the z-axis positive direction to describe the coordinates of the object coordinate system (note that this coordinate system is left-hand system), see the following diagram:


And our eyes are in the world coordinate system, so we can use the coordinates of the world coordinate system to represent the three axes of the coordinate system and the position of the eye. Look at the following figure:



(Haha, notice, we are not necessarily attention to the ground, but also may be crooked body or even lie down ...) )

So let's see how we get the transformation matrix that transforms an object from a world coordinate system to an observation coordinate system:

If it is really from the transformation of an object in the two coordinate system to consider, the problem will become very complex, so we change the idea to consider.

If our eyes are exactly at the origin of the world coordinate system, the right-hand direction is exactly the x-axis of the world coordinate system, the head direction is exactly in the y-axis positive direction of the world coordinate system, and just in the positive direction of the z axis of the world coordinate system, then the observing coordinate system is not coincident with the world coordinate system. No, you don't have to convert it? (It's too easy ...)

Unfortunately, it backfired, so many coincidences are difficult to meet, and we are too good to move, always walk around, that this hypothesis is even more unscientific.

But even if the position of the eye is random, we can move it to where we want it.

Look, if we let all the objects move with us, when we go to the origin of the world ourselves, and then stand at attention, then we can do it. (The whole world follows you to the feeling of movement ... It's really awesome. But in fact, since each object is multiplied by the transformation matrix you provide, which is the equivalent of moving along with you, this assumption is very graphic. )

your original position.

Go to the world's point of Origin

Turn in the right direction

(Forgive me for only drawing the top view-,-)

so we just need to do two steps:

1. Go to the origin of the world first

In DX, movement is achieved by multiplying it with a matrix (if you don't understand it, it's better to fill in the basics)

Assuming our position is in [PosX PosY Posz], we only need to go to the X negative direction PosX distance, toward y negative direction posY distance, to Z negative direction Posz distance.

So we can use this transformation matrix to go to the point where we get to the world:


To verify, look at our position coordinates to multiply it:


(in order to realize the translation transformation operation in DX, coordinates are the four-dimensional coordinates of [x,y,z,w] form, we just need to know that the XYZ coordinates are the XYZ coordinates in the three-dimensional world, W let him be equal to 1.) Of course, if you represent a vector, you have to let W is 0, because the beginning of the set vector in the DX in the origin, so the vector can not be translated, not to mention the vector refers to the direction, you can rotate and contraction is enough. )

2. Stand at attention!

Although we have reached the origin of the world, but may be crawling past, you face the earth ....

Come, let's assume our right hand direction was originally [Rx Ry Rz], the head direction was originally [Ux Uy Uz], originally we face [Lx Ly Lz], and they are all flat

We have to find a way to stand up, that is, let our right hand point to [1 0 0], head [0 1 0], face [0 0 1]

The language translated into mathematics is multiplied by a transformation matrix:




Put them together:


All we have to do is find the inverse matrix of the matrix.

Due to [Rx Ry Rz], [Ux Uy Uz],[Lx Ly Lz] are all flat (length 1), in fact it is the unit orthogonal array, so its inverse is its transpose matrix

(This is the linear algebra of things, class sleep and truancy students go to the book it)

So you can get:


Of course, the matrix in DX is 4 times 4, so let's extend it (this extension is really not good enough to explain it, just remember, it's not too hard)


Okay, we'll go through two steps together:


-pos R =-posx * Rx-posy * Ry-posz * Rz

-pos U =-posx * Ux-posy * Uy-posz *uz

-pos L =-posx * Lx-posy * Ly-posz * Lz

(That is, multiply the two matrices, note the order, go first to the origin, and then turn.)

Okay, so we get a look at the coordinate system transformation matrix.

Through this long piece of stuff, we find that as long as we know:

1. The position of the eye in the world coordinate system

2. Vector representation of the right-hand direction in the world coordinate system

3. Vector representation of the head direction in the world coordinate system

4. Vector representation of the forward direction in the world coordinate system

We can find the coordinate system transformation matrix.

At this point, the most important part of the theory is finished. In the next article, I'll talk about the code implementation of this camera class.

PS: Reference book-"DirectX 9.0 3D game development Programming Basics"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.