1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 ******************************************************************************/
\r
17 package com.badlogic.gdx.tests;
\r
19 import com.badlogic.gdx.Gdx;
\r
20 import com.badlogic.gdx.InputAdapter;
\r
21 import com.badlogic.gdx.graphics.Camera;
\r
22 import com.badlogic.gdx.graphics.GL10;
\r
23 import com.badlogic.gdx.graphics.OrthographicCamera;
\r
24 import com.badlogic.gdx.graphics.Texture;
\r
25 import com.badlogic.gdx.graphics.g2d.Sprite;
\r
26 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
\r
27 import com.badlogic.gdx.math.Intersector;
\r
28 import com.badlogic.gdx.math.Matrix4;
\r
29 import com.badlogic.gdx.math.Plane;
\r
30 import com.badlogic.gdx.math.Vector3;
\r
31 import com.badlogic.gdx.math.collision.Ray;
\r
32 import com.badlogic.gdx.tests.utils.GdxTest;
\r
34 public class IsoCamTest extends GdxTest {
\r
35 private static final int TARGET_WIDTH = 480;
\r
36 private static final float UNIT_TO_PIXEL = TARGET_WIDTH * 0.15f;
\r
38 OrthographicCamera cam;
\r
40 final Sprite[][] sprites = new Sprite[10][10];
\r
41 final Matrix4 matrix = new Matrix4();
\r
44 public void create () {
\r
45 texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));
\r
46 float unitsOnX = (float)Math.sqrt(2) * TARGET_WIDTH / (UNIT_TO_PIXEL);
\r
47 float pixelsOnX = Gdx.graphics.getWidth() / unitsOnX;
\r
48 float unitsOnY = Gdx.graphics.getHeight() / pixelsOnX;
\r
49 cam = new OrthographicCamera(unitsOnX, unitsOnY, 25);
\r
50 cam.position.scl(30);
\r
53 matrix.setToRotation(new Vector3(1, 0, 0), 90);
\r
55 for (int z = 0; z < 10; z++) {
\r
56 for (int x = 0; x < 10; x++) {
\r
57 sprites[x][z] = new Sprite(texture);
\r
58 sprites[x][z].setPosition(x, z);
\r
59 sprites[x][z].setSize(1, 1);
\r
63 batch = new SpriteBatch();
\r
65 Gdx.input.setInputProcessor(new IsoCamController(cam));
\r
69 public void dispose () {
\r
75 public void render () {
\r
76 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
\r
79 batch.setProjectionMatrix(cam.combined);
\r
80 batch.setTransformMatrix(matrix);
\r
82 for (int z = 0; z < 10; z++) {
\r
83 for (int x = 0; x < 10; x++) {
\r
84 sprites[x][z].draw(batch);
\r
92 final Plane xzPlane = new Plane(new Vector3(0, 1, 0), 0);
\r
93 final Vector3 intersection = new Vector3();
\r
94 Sprite lastSelectedTile = null;
\r
96 private void checkTileTouched () {
\r
97 if (Gdx.input.justTouched()) {
\r
98 Ray pickRay = cam.getPickRay(Gdx.input.getX(), Gdx.input.getY());
\r
99 Intersector.intersectRayPlane(pickRay, xzPlane, intersection);
\r
100 System.out.println(intersection);
\r
101 int x = (int)intersection.x;
\r
102 int z = (int)intersection.z;
\r
103 if (x >= 0 && x < 10 && z >= 0 && z < 10) {
\r
104 if (lastSelectedTile != null) lastSelectedTile.setColor(1, 1, 1, 1);
\r
105 Sprite sprite = sprites[x][z];
\r
106 sprite.setColor(1, 0, 0, 1);
\r
107 lastSelectedTile = sprite;
\r
112 public class IsoCamController extends InputAdapter {
\r
113 final Plane xzPlane = new Plane(new Vector3(0, 1, 0), 0);
\r
114 final Vector3 intersection = new Vector3();
\r
115 final Vector3 curr = new Vector3();
\r
116 final Vector3 last = new Vector3(-1, -1, -1);
\r
117 final Vector3 delta = new Vector3();
\r
118 final Camera camera;
\r
120 public IsoCamController (Camera camera) {
\r
121 this.camera = camera;
\r
125 public boolean touchDragged (int x, int y, int pointer) {
\r
126 Ray pickRay = camera.getPickRay(x, y);
\r
127 Intersector.intersectRayPlane(pickRay, xzPlane, curr);
\r
129 if (!(last.x == -1 && last.y == -1 && last.z == -1)) {
\r
130 pickRay = camera.getPickRay(last.x, last.y);
\r
131 Intersector.intersectRayPlane(pickRay, xzPlane, delta);
\r
133 camera.position.add(delta.x, 0, delta.z);
\r
140 public boolean touchUp (int x, int y, int pointer, int button) {
\r
141 last.set(-1, -1, -1);
\r
147 public boolean needsGL20 () {
\r