Use of 3D cameras in Cocos2d-x

Source: Internet
Author: User
This tutorial is about the use of cameras in Cocos2d-x, added the camera class in the Cocos2d-x3.2 version, this class is essential in 3D games, because 3D games are a three-dimensional world, the camera in 3D games is like our eyes. Through it, we can observe the whole game world. A camera is equivalent to a human eye.

This tutorial is about the use of cameras in Cocos2d-x, added to the camera class in Cocos2d-x 3.2, this class is essential in 3D games, because 3D games are a three-dimensional world, the camera in 3D games is like our eyes. Through it, we can observe the whole game world. A camera is equivalent to a human eye.

This tutorial is about the use of cameras in Cocos2d-x, added to the camera class in Cocos2d-x 3.2, this class is essential in 3D games, because 3D games are a three-dimensional world, camera in 3D games is equivalent to our eyes. Through it, we can observe the whole game world. A camera is equivalent to a person's eyes. When a person looks at an object with his eyes, he can theoretically observe the object from any angle, therefore, the shape of an object is affected by the camera position and rotation angle.

It is the principle of using cameras in 3D games. Of course, this is a perspective projection camera. Our camera in the game is implemented based on the principle in the figure. There are usually two types of cameras in the game: the perspective camera introduced in, which is very common in 3D games. Another is an orthogonal projection camera, which does not have the same effect as the Perspective Projection, but is the same as the size of an object at any position in the camera, generally, this type of camera uses the role model displayed on the role attribute panel. After talking about the two types of cameras, let's look at how we created the camera in the Cocos2d-x:

The above Code creates a Perspective Projection camera. The following describes the significance of the parameter: the first parameter is FOV, that is, the field of view ), it can be understood as how wide the line of sight can be viewed from the left and right (angle-based). The second is all the width-to-height ratios mentioned above. The last two are the camera's near-cut and far-cut surfaces. This is also easy to understand, the distance from the camera is closer than that of the near-cut surface, and the distance from the far-cut surface will not be rendered.

Is the principle of orthogonal cameras.

The above Code creates an orthogonal projection camera. The following describes the significance of the parameter: the first parameter is the camera width, and the second parameter is the camera height, the last two are the camera's near-cut and far-cut planes. This is also very understandable. It is closer to the camera than the near-cut plane, and farther to the far-cut plane, will not be rendered. This is the same as the fluoroscopy camera. Next, we need to set a FLAG for the camera so that the camera can be differentiated from other cameras-in some game applications, there is usually not only one camera, if there are multiple cameras, we need to mark an object to be "seen" by which camera. At this time, we need to set its CameraMask to match the Flag of the camera:

If there are multiple cameras at the same time, how can we mark an object as seen by those cameras?


Note that the _ cameraFlag attribute in Camera is of the enumeration type and is defined as follows:

1

2

3

4

5

6

7

8

9

10

11

12

enum class CameraFlag

{

DEFAULT = 1,

USER1 = 1 << 1,

USER2 = 1 << 2,

USER3 = 1 << 3,

USER4 = 1 << 4,

USER5 = 1 << 5,

USER6 = 1 << 6,

USER7 = 1 << 7,

USER8 = 1 << 8,

};

Node has an attribute of _ cameraMask. If the value of _ cameraFlag & _ cameraMask of the camera is true, the Node can be viewed by the camera. Therefore, in the above-mentioned camera creation code, the CameraFlag of camera is set to CameraFlag: USER1 and the CameraMask of this layer is 2, which means that this layer can only be seen by the CameraFlag: USER1 camera. If cameraMask is set to 3, it can also be seen by cameras with cameraFlag as CameraFlag: USER1 and CameraFlag: user2. We also need to note that if your genie is added after the cameraMask is set for the layer, it will not be seen, and you need to manually set the cameraMask for the genie. Don't think we can do this. In the end, we need to add the camera to the scene. Otherwise, we still cannot see the effect. Remember to add the camera to the scene code:

In this way, even if we have created a camera, this is only the first part. Next I will explain how the camera is used in the game.


In general, there are three ways to use a camera in 3D games:

  • The first is a free camera.

  • The second is the first-person camera.

  • Third, third-person camera


Next let's talk about the first free camera.These types of cameras are generally used in instant strategy games. For example, Warcraft 3 uses a free camera, which can move with the mouse, the ability to see any position in the game scene is the use of a free camera in the Cocos2d-x:

Is it very easy? In fact, we only need to assign the distance of moving the screen to the camera in the mobile function so that the camera can follow and move, it should be noted that we move up and down on the screen relative to the camera before and after, so we need to assign the y value to the camera z, in addition, the direction is reversed because we move the screen camera forward, slide down, and the camera moves backward. Then we will ask, how can we move the Y axis of a 3D camera? It is the implementation of closer and closer camera pull:

The first image is closer, and the second image is farther. In this way, we can also use Cocos2d-x to make a game like Warcraft 3.


Next let's look at the second type of camera, which is the first person camera.This kind of camera is actually very easy to understand, and it is equivalent to what we see in our eyes. This kind of camera is used in FPS games, such as the call of others, so can it be implemented in the Cocos2d-x? The answer is yes. Let's see:

In fact, it is enough to tie the camera position to the role you control in the game. We use sprite3d to move instead of sliding on the screen, that is to say, we slide on the screen not to control the camera, but to control sprite3d, and then assign the sprite3d movement to the camera. You may ask if FPS games can still rotate the angle of view? Our camera can be implemented. Let's look at the code below:

This is to rotate to the left.

This is the right rotation.

Through this setting, we can achieve the FPS game effect.


Next, let's introduce the third-person camera.This type of camera is mostly used in MMORPG games. This type of game is our most common game type. Currently, most online games use this type of camera, in fact, it sets the camera position of the first person to a certain distance behind the role we control, so that we can see the game role we control, the implementation in the Cocos2d-x is similar to the first person camera, the same part we no longer illustrate is to get the role position assigned to the camera and then add an offset to the camera position, offset is represented by offset, for example:

I will not repeat the rest of the steps like moving and rotating the first person camera so that the third person camera can be realized.


Well, today's tutorial will talk about here, through this tutorial, we can create our own camera in the Cocos2d-x, by creating a camera to make our own 3D game, the implementation and usage of the three cameras mentioned above are both very simple entry-level introduction. You may not think it is complete. It doesn't matter. You can refer to the Camera Test code in CppTest, my tutorial is based on it. I have detailed code implementation in Camera Test for your reference. I hope you can make a great game. Thank you!


This article mainly summarizes the Cocos engine Chinese site and Cocos2d-x forum on the Cocos2d-x 3D related tutorials, to help you learn more quickly in the Cocos2d-x 3D function.

  • 3D characteristics of Cocos2d-x

  • Earth Warrior 3D secrets

  • Analyze the structure of FantasyWarrior3D

  • Cocos2d-x 3D model anti-aliasing Method

  • Introduction to 3D development function of Cocos2d-x 3.3

  • Specification and precautions for 3D modeling of Cocos2d-x

  • Cocos2d-x 3.x 3D parkour game series tutorials

  • Cocos2d-x 3D "Fishing talents 3" series tutorial

  • Cocos2d-x 3D model Viewer (Mac & Win)

  • Cocos2d-x 3D fbx-conv bulk conversion script

  • Cocos2d-x tower anti-game thieves come to join 3D features

  • Cocos2d-x Sprite3D usage and related features

  • Cocos2d-x 3D FantasyWarrior3D Project Open Source

  • Cocos2d-x 3D combat game production [Module 1] -- database (1)

  • Cocos2dx 3D Combat Games [Module 1]-database (2)

  • [Cocos2d-x 3.2] Summary of Grid3DAction common 3D Effects


After reading so many 3D tutorials, do you have a better understanding of the 3D function of Cocos2d-x? Share your development experience or practical projects with you!

Http://blog.csdn.net/minsenwu/article/details/17120495

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.