Use of android game development framework libgdx (8)-camera and observation point

Source: Internet
Author: User
Tags libgdx

The libgdx used in this article is version 0.92, which may be different from the latest version. The full text is for reference only.

The camera can also be an observer, and the simplest example is the soul.

The player moves the role forward, backward, and jump. The displayed world (that is, the background behind) will not stop changing. It is actually caused by changes in the camera's observation point (angle of view.

Maybe the game world (background map) is very large, such as 10240*480, and the device resolution is 800*480. Now the game's role is on the far left of the map, and its observation point is (400,240 ).

As the role moves forward, the observation point also changes, usually the value of Y increases.

The value of Y decreases when the role is removed.

The X value changes when the role jumps. (In fact, the map may be a little higher, such as 500, so that the jump of the role will be effective ).

Cameras are a basic component of game development. The stage class of libgdx contains a camera by default.

There are also many functions for the camera class. The most commonly used is orthographiccamera, which is the default Camera class in stage.

Orthographiccamera provides the following functions:

1. Move and rotate the lens

2. Zoom in and out

3. Change the observation point (Perspective)

4. Form and world point conversion

The biggest advantage of cameras is that they can move the game world without manual operations on matrices. All matrix projection and observation point operations are hidden from developers.

Generally, cameras work with mesh. Mesh draws a rectangular area and maps the map on it.

The following example uses an image:

The size is 2048*2048.

CodeAs follows:

Package COM. cnblogs. htynkn. listener; import COM. badlogic. GDX. applicationlistener; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. graphics. gl10; import COM. badlogic. GDX. graphics. mesh; import COM. badlogic. GDX. graphics. orthographiccamera; import COM. badlogic. GDX. graphics. texture; import COM. badlogic. GDX. graphics. vertexattribute; import COM. badlogic. GDX. graphics. vertexattributes; import COM. badlog IC. GDX. graphics. vertexattributes. usage; import COM. badlogic. GDX. math. rectangle; public class firstgame implements applicationlistener {private orthographiccamera Cam; private texture; private mesh; private rectangle glviewport; @ override public void create () {// create a static, the rectangle of the four vertices composed of two triangles is divided into 3*2. The grid has three location parameters (x, y, z). // The location attribute of the grid has two values mesh = new mesh (true, 4, 6, new vertexattribute (vertexattributes. usage. position, 3, "attr_position"), new vertexattribute (usage. texturecoordinates, 2, "attr_texcoords"); texture = new texture (GDX. files. internal ("img/xk.jpg"); // set the corresponding edge point. // take the first one as an example-2048f,-2048f, 0 is the position parameter, because it is a two-dimensional, therefore, the Z value is 0 // 0 and corresponds to the vertex coordinate mesh. setvertices (new float [] {-2048f,-2048f, 0, 0, 1, 2048f,-2048f, 0, 1, 1, 2048f, 2048f, 0, 1, 0, -2048f, 2048f, 0, 0, 0}); // set the index. This is a bit difficult to understand. Article Detailed description of mesh. setindices (new short [] {0, 1, 2, 2, 3, 0}); float width = GDX. graphics. getwidth (); float Height = GDX. graphics. getheight (); cam = new orthographiccamera (width, height); cam. position. set (width/2, height/2, 0); glviewport = new rectangle (0, 0, width, height) ;}@ override public void dispose () {}@ override public void pause () {// todo auto-generated method stub} @ override public void render () {gl10 GL = GDX. graphics. getgl10 (); // move the camera... I am so stupid here... ignore GL. glclear (gl10.gl _ color_buffer_bit); GL. glviewport (INT) glviewport. x, (INT) glviewport. y, (INT) glviewport. width, (INT) glviewport. height); cam. update (); cam. apply (GL); // map GL. glactivetexture (gl10.gl _ texture0); GL. glable (gl10.gl _ texture_2d); texture. BIND (); mesh. render (gl10.gl _ triangles) ;}@ override public void resize (INT width, int height) {// todo auto-generated method stub} @ override public void resume () {// todo auto-generated method stub }}

Note:

 
Mesh. setindices (new short [] {0, 1, 2, 2, 3, 0 });

This is to set the index. I will use a picture to illustrate it.

0, 1, 2 indicates the triangle in the upper right corner.

0 is the triangle in the lower left corner.

 

Note: Some of my friends have reported that the black screen problem may occur in this example, but the idea is correct.

Related Article

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.