摘要:libGdx是一个跨平台的2D/3D的游戏开发框架,由Java/C/C++语言编写而成,基于Apache License, Version 2.0协议,对商业使用和非商业使用均免费。本文为大家带来Android游戏引擎libgdx使用教程之相机和观察点,以及libgdx 1.4.1版本功能详解。

Android游戏引擎libgdx使用教程之相机和观察点
相机可以成为观察者,最简单的例子就是魂斗罗。玩家操作角色前进,后退,跳跃等,显示的世界(就是后面的背景)会不停变化。其实就是相机的观察点(视角)的改变引起的。

也许游戏的世界(背景地图)很大,比如10240*480,而设备的分辨率是800*480。现在游戏的角色在地图的最左侧,那么它的观察点就是(400,240)。
随着角色的向前移动,观察点也变化,通常是y值变大。
角色后退时y值变小。角色跳跃时,x值变化。(其实地图可能要稍微高一点,比如500,这样角色的跳跃才会有效果)。
可以说相机是游戏开发的一个基本组件。而Libgdx的Stage类中就默认包含了一个Camera。
Camera类按照功能而言也有很多种,最常用的是OrthographicCamera(正投影相机),Stage中默认Camera的实现类为该类。
OrthographicCamera实现以下功能:
1. 移动和旋转镜头
2. 放大和缩小
3. 改变观察点(视角)
4. 窗体和世界的点的转化
相机的最大好处就是无需手动操作矩阵就可以移动游戏世界,所有的矩阵投影和观察点运算就是对开发者隐藏的。
相机的使用一般配合着mesh。mesh绘制一个矩形区域,然后将地图贴图在其上。
接下来的例子将使用一张图:

代码如下:
01 |
package com.cnblogs.htynkn.listener;
|
02 |
import com.badlogic.gdx.ApplicationListener;
|
03 |
import com.badlogic.gdx.Gdx;
|
04 |
import com.badlogic.gdx.graphics.GL10;
|
05 |
import com.badlogic.gdx.graphics.Mesh;
|
06 |
import com.badlogic.gdx.graphics.OrthographicCamera;
|
07 |
import com.badlogic.gdx.graphics.Texture;
|
08 |
import com.badlogic.gdx.graphics.VertexAttribute;
|
09 |
import com.badlogic.gdx.graphics.VertexAttributes;
|
10 |
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
|
11 |
import com.badlogic.gdx.math.Rectangle;
|
12 |
public class FirstGame implements ApplicationListener {
|
13 |
private OrthographicCamera cam;
|
14 |
private Texture texture;
|
16 |
private Rectangle glViewport;
|
18 |
public void create() {
|
22 |
mesh = new Mesh( true , 4 , 6 , new VertexAttribute(
|
23 |
VertexAttributes.Usage.Position, 3 , "attr_Position" ),
|
24 |
new VertexAttribute(Usage.TextureCoordinates, 2 ,
|
26 |
texture = new Texture(Gdx.files.internal( "img/xk.jpg" ));
|
30 |
mesh.setVertices( new float [] { -2048f, -2048f, 0 , 0 , 1 , 2048f, -2048f,
|
31 |
0 , 1 , 1 , 2048f, 2048f, 0 , 1 , 0 , -2048f, 2048f, 0 , 0 , 0 });
|
33 |
mesh.setIndices( new short [] { 0 , 1 , 2 , 2 , 3 , 0 });
|
34 |
float WIDTH = Gdx.graphics.getWidth();
|
35 |
float HEIGHT = Gdx.graphics.getHeight();
|
36 |
cam = new OrthographicCamera(WIDTH, HEIGHT);
|
37 |
cam.position.set(WIDTH / 2 , HEIGHT / 2 , 0 );
|
38 |
glViewport = new Rectangle( 0 , 0 , WIDTH, HEIGHT);
|
41 |
public void dispose() {
|
48 |
public void render() {
|
49 |
GL10 gl = Gdx.graphics.getGL10(); |
51 |
gl.glClear(GL10.GL_COLOR_BUFFER_BIT); |
52 |
gl.glViewport(( int ) glViewport.x, ( int ) glViewport.y,
|
53 |
( int ) glViewport.width, ( int ) glViewport.height);
|
57 |
gl.glActiveTexture(GL10.GL_TEXTURE0); |
58 |
gl.glEnable(GL10.GL_TEXTURE_2D); |
60 |
mesh.render(GL10.GL_TRIANGLES); |
63 |
public void resize( int width, int height) {
|
67 |
public void resume() {
|
注意这一句:
mesh.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });
这是设置索引,我用一张图来说明。

图片分享:
0,1,2是指右上角的三角形,而2,3,0是左下角的三角形
10月10日,libgdx1.4.1发布。以下是新增的功能:
注:支持 iOS 8 和 iPhone 6、iPhone 6 plus
*如果你在使用Eclipse 4.4.x,那么升级你的Gradle插件很重要!
*升级到Intellij IDEA to 13.1.5+,因为Gradle的原因!
*升级到Gradle 2.1 和 Android build tools 20, 默认 Android version to 20。你需要通过SDK manager 安装最新的 Android build tools。
*API 更改: 废除 bounding box 方法,可查阅 https://github.com/libgdx/libgdx/pull/2408
*添加非持续性渲染到iOS backend
*Gdx-setup现在使用Gradle 2.1 默认 Android API level 20, build tools 20.0.0
*非持续性渲染在iOS的实现
*为scene2d 标签和window标题添加color markup 支持。
*API 更改:移除DecalBatch默认构造器。移除DefaultGroupStrategy
*升级到RoboVM release, 1.0.0-alpha-02,请升级你的RoboVM插件
-降低 I18NBundle 在Android的载入时间和忽略为关闭的流程。
*从libGDX repository中删除gdx-ai扩展。现在gdx-ai在libGDX下有了自己的 repository,可查阅 https://github.com/libgdx/gdx-ai
*API 添加:在MathUtils中添加randomSign and randomTriangular方法。
*API 添加: Decal添加Color的getter。
*API 添加:现在I18NBundle可以设置当无法发现key时不抛出异常。
*API 添加:在reflection层添加 annotation 支持。可查阅: https://github.com/libgdx/libgdx/pull/2215
*API 添加:添加圆形,方形等形状,现在继承 Shape2D 接口,你可以将它们放到一个collection中。https://github.com/libgdx/libgdx/pull/2178
*API 添加: bitmap 字体现在可以通过AssetManager或者BitmapFontLoader从atlas载入。可查阅: https://github.com/libgdx/libgdx/pull/2110
*API 更改: Table现在默认从它的background使用padding。可查阅: https://github.com/libgdx/libgdx/issues/2322
*Drawables现在知道了它们的名字,让调试更加简单。
*API 更改:Table填充现在涉及widget的最小尺寸。
*Texture packer,修正写入到atlas文件中的图像大小问题。
*API 更改: Cell在共用API中不再使用 primitive wrappers和boxing是缩略图。
*API 添加:TextureAttribute现在支持uv变换。
*API 更改:添加到Elastic插值的参数。
*API 更改:删除Actor#setCenterPosition, 添加setPosition(x,y,align).
*API 更改:JsonReader,继续消减。
*API 更改:Stage#cancelTouchFocus(EventListener,Actor) 改成cancelTouchFocusExcept.
API 更改:Json/JsonWriter.setQuoteLongValues() Long引用,BigDecimal和BigInteger类型防止在JavaScript和PHP中截断。
小编温馨提醒,Libgdx中文社区已于2013年5月正式上线,大家可以少为英文而发愁了。