1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.Color; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.OrthographicCamera; 23 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 24 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 25 import com.badlogic.gdx.math.Interpolation; 26 import com.badlogic.gdx.math.MathUtils; 27 import com.badlogic.gdx.math.Vector2; 28 import com.badlogic.gdx.tests.utils.GdxTest; 29 30 public class Vector2dTest extends GdxTest { 31 private static final float DURATION = 2.0f; 32 33 private ShapeRenderer renderer; 34 private OrthographicCamera camera; 35 36 private Vector2 rotating = new Vector2(Vector2.X); 37 private Vector2 scalingX = new Vector2(Vector2.Y); 38 private Vector2 scalingY = new Vector2(Vector2.X); 39 private Vector2 lerping1 = new Vector2(Vector2.X); 40 private Vector2 lerpTarget = new Vector2(Vector2.Y); 41 42 private Vector2 sum = new Vector2().add(Vector2.X).add(Vector2.Y).nor(); 43 private Vector2 mash = new Vector2(Vector2.Y); 44 45 private Interpolation interpolator = Interpolation.swing; 46 private Vector2 lerping2 = new Vector2(Vector2.X); 47 private Vector2 lerpStart2 = new Vector2(Vector2.X); 48 private Vector2 lerpTarget2 = new Vector2(Vector2.Y); 49 private float timePassed = 0; 50 51 private final long start = System.currentTimeMillis(); 52 53 @Override 54 public void create () { 55 renderer = new ShapeRenderer(); 56 } 57 58 private void renderVectorAt (float x, float y, Vector2 v) { 59 renderer.line(x, y, x + v.x, y + v.y); 60 } 61 62 @Override 63 public void render () { 64 Gdx.gl.glClearColor(0, 0, 0, 0); 65 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 66 67 renderer.setProjectionMatrix(camera.combined); 68 69 // Render the 'lerp' vector target as a circle 70 renderer.begin(ShapeType.Filled); 71 renderer.setColor(1.0f, 0, 0, 0.3f); 72 renderer.circle(-2 + lerpTarget.x, 2 + lerpTarget.y, 0.08f, 16); 73 renderer.circle(-4 + lerpTarget2.x, 0 + lerpTarget2.y, 0.08f, 16); 74 renderer.end(); 75 76 renderer.begin(ShapeType.Line); 77 78 // Render the three fixed X, Y and sum vectors: 79 renderer.setColor(Color.RED); 80 renderVectorAt(0, 0, Vector2.X); 81 renderer.setColor(Color.GREEN); 82 renderVectorAt(0, 0, Vector2.Y); 83 renderer.setColor(Color.YELLOW); 84 renderVectorAt(0, 0, sum); 85 86 final float changeRate = Gdx.graphics.getDeltaTime(); 87 renderer.setColor(Color.WHITE); 88 89 renderVectorAt(2, 2, rotating); 90 rotating.rotate(93 * changeRate); 91 92 renderVectorAt(2, -2, scalingX); 93 scalingX.set(0, MathUtils.sin((System.currentTimeMillis() - start) / 520.0f)); 94 renderVectorAt(2, -2, scalingY); 95 scalingY.set(MathUtils.cos((System.currentTimeMillis() - start) / 260.0f), 0); 96 97 renderVectorAt(-2, 2, lerping1); 98 lerping1.lerp(lerpTarget, 0.025f); 99 100 if (lerping1.epsilonEquals(lerpTarget, 0.05f)) { 101 lerpTarget.set(-1.0f + MathUtils.random(2.0f), -1.0f + MathUtils.random(2.0f)).nor(); 102 } 103 104 timePassed += Gdx.graphics.getDeltaTime(); 105 renderVectorAt(-4, 0, lerping2); 106 lerping2.set(lerpStart2); 107 lerping2.interpolate(lerpTarget2, MathUtils.clamp(timePassed/DURATION,0,1), interpolator); 108 109 if (lerping2.epsilonEquals(lerpTarget2, 0.025f)) { 110 lerpTarget2.set(-1.0f + MathUtils.random(2.0f), -1.0f + MathUtils.random(2.0f)).nor(); 111 lerpStart2.set(lerping2); 112 timePassed = 0; 113 } 114 115 116 renderVectorAt(-2, -2, mash); 117 mash.set(0, 0).add(rotating).add(scalingX).add(scalingY).add(lerping1); 118 119 renderer.end(); 120 } 121 122 @Override 123 public void resize (int width, int height) { 124 float ratio = ((float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight()); 125 int h = 10; 126 int w = (int)(h * ratio); 127 camera = new OrthographicCamera(w, h); 128 } 129 } 130